0

My blob detector is not coloring my binary image. Can somone explain the problem?

code:

## mask of yellow color
mask_yellow = cv2.inRange(Img, (0, 180, 240), (20, 255, 255))

#define kernel size
kernel = np.ones((15,15), np.uint8)
# Remove unnecessary noise from mask
mask_yellow = cv2.morphologyEx(mask_yellow, cv2.MORPH_CLOSE, kernel)
mask_yellow = cv2.morphologyEx(mask_yellow, cv2.MORPH_OPEN, kernel)

#scale window size
mask_yellow_view = cv2.resize(mask_yellow, dsize)

#show yellow colors
cv2.imshow('Yellow mask', mask_yellow_view)

mask_yellow = cv2.bitwise_not(mask_yellow)

mask_yellow = cv2.cvtColor(mask_yellow, cv2.COLOR_GRAY2RGB)

params = cv2.SimpleBlobDetector_Params()

params.filterByArea = True
params.minArea = 10000

detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(mask_yellow)

blank = np.zeros((20, 20))
blobs = cv2.drawKeypoints(mask_yellow, keypoints, blank, (0, 0, 255),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)


# Show keypoints
cv2.imshow("Keypoints", blobs)
3
  • 1
    If you image is binary/grayscale (one channel), then OpenCV will not be able to color it. You have to convert the image to 3 (equal) channels first. See cv2.merge(). Commented Jun 9, 2022 at 18:57
  • @fmw42 Doesn't cv2.COLOR_GRAY2RGB already do that? Commented Jun 9, 2022 at 19:28
  • Yes, that is another equally good way. Commented Jun 9, 2022 at 20:10

1 Answer 1

1

For me your code works fine. Here is the simplified code I used and my sample input and output image. The params.minArea threshold was too high in my case, so I had to lower it. Please check if your filter criteria fits to your input!

import cv2

img = cv2.imread("img.png")

# mask of yellow color
mask_yellow = cv2.inRange(img, (0, 180, 240), (20, 255, 255))
mask_yellow = cv2.bitwise_not(mask_yellow)
mask_yellow = cv2.cvtColor(mask_yellow, cv2.COLOR_GRAY2RGB)

params = cv2.SimpleBlobDetector_Params()

params.filterByArea = True
params.minArea = 1000

detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(mask_yellow)

blobs = cv2.drawKeypoints(mask_yellow, keypoints, 0, (0, 0, 255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

cv2.imwrite("out.png", blobs)

Input image:

Input image

Output image:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

The problem is solved the default value of the params.AreaMax was too low. And thanks for showing it was possible to detect a binary image!

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.