we_both_are_lost
we_both_are_lost

Reputation: 13

Why won't OpenCV SimpleBlobDetector detect all the blobs?

I tried changing everything, and nothing seems to work. Even if it doesn't work for second image, I would expect it to work for first. Any ideas on why it doesn't work?

binary image 1

binary image 2

detected blobs for image 1

detected blobs for image 2

Code

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)

Upvotes: 1

Views: 927

Answers (1)

Abhi25t
Abhi25t

Reputation: 4643

It is happening because your blobs are connected. Do an opening operation (erosion followed by dilation) before detecting blobs.

import numpy as np
kernel = np.ones((5,5),np.uint8)
binary = cv2.erode(binary,kernel,iterations = 1)

Upvotes: 1

Related Questions