0

I am trying to do some white blob detection using OpenCV. But my script failed to detect the big white block which is my goal while some small blobs are detected. I am new to OpenCV, and am i doing something wrong when using simpleblobdetection in OpenCV? [Solved partially, please read below]

And here is the script:

#!/usr/bin/python

# Standard imports
import cv2
import numpy as np;

from matplotlib import pyplot as plt

# Read image
im = cv2.imread('whiteborder.jpg', cv2.IMREAD_GRAYSCALE)
imfiltered = cv2.inRange(im,255,255)

#OPENING
kernel = np.ones((5,5))

opening = cv2.morphologyEx(imfiltered,cv2.MORPH_OPEN,kernel)

#write out the filtered image

cv2.imwrite('colorfiltered.jpg',opening)


# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

params.blobColor= 255
params.filterByColor = True


# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
    detector = cv2.SimpleBlobDetector(params)
else : 
    detector = cv2.SimpleBlobDetector_create(params)


# Detect blobs.
keypoints = detector.detect(opening)

# Draw detected blobs as green circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob

print str(keypoints)

im_with_keypoints = cv2.drawKeypoints(opening, keypoints, np.array([]), (0,255,0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show blobs
##cv2.imshow("Keypoints", im_with_keypoints)

cv2.imwrite('Keypoints.jpg',im_with_keypoints)

cv2.waitKey(0)

EDIT:

By adding a bigger value of area maximum value, i am able to identify a big blob but my end goal is to identify the big white rectangle exist or not. And the white blob detection i did returns not only the rectangle but also the surrounding areas as well. [This part solved]

EDIT 2:

Based on the answer from @PSchn, i update my code to apply the logic, first set the color filter to only get the white pixels and then remove the noise point using opening. It works for the sample data and i can successfully get the keypoint after blob detection. enter image description here

2 Answers 2

2

If you just want to detect the white rectangle you can try to set a higher threshold, e.g. 253, erase small object with an opening and take the biggest blob. I first smoothed your image, then thresholding it:

enter image description here

and the opening:

enter image description here

now you just have to use findContours and take the boundingRect. If your rectangle is always that white it should work. If you get lower then 251 with your threshold the other small blobs will appear and your region merges with them, like here:

enter image description here

Then you could still do an opening several times and you get this: enter image description here

But i dont think that it is the fastest idea ;)

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

8 Comments

Awesome explanation. That is what i am looking for. Appreciate that!
You're welcome. But i dont know if it works on all of your images. Hope it does ;)
Do you mind to share the code how you do smoothing and the other steps? Also what is the best way to tuning the parameters to get the best filter for this kind of task? Thanks again!
I have no code right now, i did it in a hurry with ImageJ. But if you want, i can add some code for each step. What do you mean with best filter? smoothing filter?
That would be great! And yes, how did you come up with the 253 would be good for threshold for this image and how did you do the smoothing filter as well? I am not sure how can i set the parameters appropriately, so that i can apply the logic to other images that i have automatically (they may be different and they are a lot)
|
1

You could try setting params.maxArea to something obnoxiously large (somewhere in the tens of thousands): the default may be something lower than the area of the rectangle you're trying to detect. Also, I don't know how true this is or not, but I've heard that detection by color is bugged with a logic error, so it may be worth a try disabling it just in case that is causing problems (this has probably been fixed in later versions, but it could still be worth a try)

1 Comment

By setting a larger maxArea parameter for the filter i can detect the big blobs. Thanks for your answer. But i run into another problem that is i only want to identify the blobs which pixels in same color value and adjacent to each other (not be separated by other pixels in other value). Like the example i posted earlier, i try to identify the big white rectangle block and what i get is huge blob of the rectangle and its surrendering white pixels which are a lot.

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.