如何不显示特定大小的水滴?(OpenCV、Python)

2024-05-16 22:35:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要OpenCV中的一个函数,该函数允许我不显示特定大小的blob,如图中用红色圆圈标记的blob,我希望它们不显示在图像中

这是我使用的代码:

    img = cv.imread('Fotos/1.jpg')
    params = cv.SimpleBlobDetector_Params()
    params.filterByArea = True
    params.minArea = 8200
    params.maxArea = 15500
    detector = cv.SimpleBlobDetector_create(params)
    keypoints = detector.detect(img)
    #draw blobs
    img_with_blobs = cv.drawKeypoints(img, keypoints, np.array([]), (0, 0, 255), cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    plt.imshow(img_with_blobs)
    cv.imshow("Keypoints", img_with_blobs)

Blobs detected


Tags: 函数imgwithparamsdetectoropencvblobcv
1条回答
网友
1楼 · 发布于 2024-05-16 22:35:08
import cv2
img = cv2.imread('input/1.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 51, 9)
cnts = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    if 8200 < cv2.contourArea(c) < 12500:
        cv2.drawContours(img, [c], -1, (255, 255, 255), -1)
cv2.imshow("1", img)

erased result

相关问题 更多 >