Search code examples
pythonopencvthreshold

minimum blob size for opencv threshold in python


is there a way to threshold only if the blob size is more than (height,width)?

Example

import cv2

img_1 = cv2.imread('my_image_1.jpg')
thresh = cv2.threshold(img_1, 200, 255, cv2.THRESH_BINARY)[1]

For the purposes of thresholding, I want to ignore all pixels that are not inside a blob of say, 6 x 6 pixels that also meet the thresholding condition.

What is the best way to do this?


Solution

  • Please check this tutorial. You can implement this by adding the following lines to your code,

    params = cv2.SimpleBlobDetector_Params()
    
    params.filterByArea = True
    params.minArea = 20 #define minimum area
    
    ver = (cv2.__version__).split('.')
    
    if int(ver[0]) < 3 :
        detector = cv2.SimpleBlobDetector(params)
    else : 
        detector = cv2.SimpleBlobDetector_create(params)
    
    keypoints = detector.detect(thresh)
    
    im_with_keypoints = cv2.drawKeypoints(thresh, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)