Search code examples
pythonopencvcomputer-vision

Can't detect image with blob


Just tried using blobs to detect my image, using the example here: https://www.learnopencv.com/blob-detection-using-opencv-python-c/, but it just does not detect anything.

https://i.sstatic.net/yaw5P.jpg

I tried using the original Image, grey image, and thresholding it to only black and white, but none of them detect any blobs, and the keypoints always remain 0.

import numpy as np
import cv2


im_width = 320
im_height = 240

img = cv2.imread("D:\\20190822\\racket.bmp")
GreyImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh=cv2.threshold(GreyImage,50,255,cv2.THRESH_BINARY)
detector = cv2.SimpleBlobDetector_create()
keypoints = detector.detect(thresh)
blobs = cv2.drawKeypoints(thresh, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
print(len(keypoints))
cv2.imshow("Keypoints", blobs)
cv2.waitKey(0)
cv2.destroyAllWindows()

Solution

  • I am not an expert on this, but it appears from the documentation that the default is to find circular blobs. You do not have circles except for some small dots. So you have to relax all the arguments to catch every shape. See

    https://docs.opencv.org/3.4/d0/d7a/classcv_1_1SimpleBlobDetector.html

    https://docs.opencv.org/3.4/d2/d29/classcv_1_1KeyPoint.html#a308006c9f963547a8cff61548ddd2ef2

    https://craftofcoding.wordpress.com/tag/cv2/

    So try this:

    Input:

    enter image description here

    import numpy as np
    import cv2
    import math
    
    img = cv2.imread("racket.png")
    GreyImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret,thresh=cv2.threshold(GreyImage,50,255,cv2.THRESH_BINARY)
    
    # erode to one large blob
    #thresh = cv2.erode(thresh, None, iterations=4)
    
    cv2.imshow("Threshold", thresh)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    # Set up the SimpleBlobdetector with default parameters.
    params = cv2.SimpleBlobDetector_Params()
    
    # Change thresholds
    params.minThreshold = 0
    params.maxThreshold = 256
    
    # Filter by Area.
    params.filterByArea = True
    params.minArea = 0
    params.maxArea = 100000000000
    
    # Filter by Color (black)
    params.filterByColor = True
    params.blobColor = 0
    
    # Filter by Circularity
    params.filterByCircularity = True
    params.minCircularity = 0
    params.maxCircularity = 100000000
    
    # Filter by Convexity
    params.filterByConvexity = True
    params.minConvexity = 0
    params.maxConvexity = 100000000
    
    # Filter by InertiaRatio
    params.filterByInertia = True
    params.minInertiaRatio = 0
    params.maxInertiaRatio = 100000000
    
    # Distance Between Blobs
    params.minDistBetweenBlobs = 0
    
    # Do detecting
    detector = cv2.SimpleBlobDetector_create(params)
    
    # Get keypoints
    keypoints = detector.detect(thresh)
    
    print(len(keypoints))
    print('')
    
    # Get keypoint locations and radius
    for keypoint in keypoints:
       x = int(keypoint.pt[0])
       y = int(keypoint.pt[1])
       s = keypoint.size
       r = int(math.floor(s/2))
       print (x,y,r)
       #cv2.circle(img, (x, y), r, (0, 0, 255), 2)
    
    # Draw blobs
    blobs = cv2.drawKeypoints(thresh, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    cv2.imshow("Keypoints", blobs)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    # Save result
    cv2.imwrite("racket_blobs.jpg", blobs)
    


    enter image description here

    42
    
    136 226 35
    138 225 1
    136 225 1
    134 225 1
    140 223 1
    122 223 1
    137 222 1
    144 221 1
    114 221 1
    83 232 9
    144 219 1
    150 217 1
    114 215 1
    164 209 1
    158 209 1
    163 206 1
    118 203 1
    128 195 1
    175 194 1
    134 189 1
    185 184 1
    154 175 1
    197 174 1
    159 174 1
    157 172 1
    196 171 1
    162 171 1
    200 169 1
    198 167 1
    204 165 1
    200 165 1
    200 163 1
    211 162 1
    179 160 1
    208 159 1
    210 157 1
    204 157 1
    135 227 1
    203 156 1
    214 155 1
    204 155 1
    200 155 1
    

    If you want to see the shapes, then you might be better off using contours rather than blobs.