为什么 OpenCV SimpleBlobDetector 不能检测到所有的斑点?
Why won't OpenCV SimpleBlobDetector detect all the blobs?
我尝试更改所有内容,但似乎没有任何效果。即使它不适用于第二张图片,我也希望它能用于第一张图片。关于为什么它不起作用的任何想法?
binary image 1
binary image 2
detected blobs for image 1
detected blobs for image 2
代码
import numpy as np
import cv2
binary = binary_images[0]
k_max_area=0.07; k_min_area=0.003
min_area = np.shape(img)[0]*np.shape(binary)[1]*k_min_area
max_area = np.shape(img)[0]*np.shape(binary)[1]*k_max_area
params = cv2.SimpleBlobDetector_Params()
params.minThreshold=0
params.maxThreshold=230
params.filterByArea = True
params.minArea=min_area
params.maxArea=max_area
params.filterByCircularity = False
params.filterByInertia = False
params.filterByConvexity = False
params.filterByColor=True
params.blobColor=255
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
detector.empty()
keypoints = detector.detect(binary)
im_with_keypoints = cv2.drawKeypoints(binary, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
plt.imshow(im_with_keypoints)
这是因为您的 blob 已连接。在检测斑点之前进行开运算(先腐蚀后膨胀)。
import numpy as np
kernel = np.ones((5,5),np.uint8)
binary = cv2.erode(binary,kernel,iterations = 1)
我尝试更改所有内容,但似乎没有任何效果。即使它不适用于第二张图片,我也希望它能用于第一张图片。关于为什么它不起作用的任何想法?
binary image 1
binary image 2
detected blobs for image 1
detected blobs for image 2
代码
import numpy as np
import cv2
binary = binary_images[0]
k_max_area=0.07; k_min_area=0.003
min_area = np.shape(img)[0]*np.shape(binary)[1]*k_min_area
max_area = np.shape(img)[0]*np.shape(binary)[1]*k_max_area
params = cv2.SimpleBlobDetector_Params()
params.minThreshold=0
params.maxThreshold=230
params.filterByArea = True
params.minArea=min_area
params.maxArea=max_area
params.filterByCircularity = False
params.filterByInertia = False
params.filterByConvexity = False
params.filterByColor=True
params.blobColor=255
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
detector.empty()
keypoints = detector.detect(binary)
im_with_keypoints = cv2.drawKeypoints(binary, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
plt.imshow(im_with_keypoints)
这是因为您的 blob 已连接。在检测斑点之前进行开运算(先腐蚀后膨胀)。
import numpy as np
kernel = np.ones((5,5),np.uint8)
binary = cv2.erode(binary,kernel,iterations = 1)