1

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)

3
  • Your blobs are not isolated. Many touch each other and so will be considered one region. Other blobs have gaps in them. See docs.opencv.org/4.1.1/d0/d7a/classcv_1_1SimpleBlobDetector.html Commented Nov 7, 2020 at 2:54
  • @fmw42 Is there any other way to detect shapes like '8' as 2 circles, or do I have to separate them first? I'd like to be able to specify blob(or circle) parameters, and get blobs(or circles) such that maximum amount of white and minimum amount of black area is enclosed. Commented Nov 7, 2020 at 15:16
  • You could try hough transform or watershed. Commented Nov 7, 2020 at 18:27

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.