How can i detect circles with color

I want to find red circles. I used opencv2, Simple Blob Detector. When i apply mask on a photo it can't find circles. It has no error messages, it just can't find any circles. This is what i get -> Founded_blobs. My test photo -> BlobTest4.jpg. My code is below.

import cv2
import numpy as np

img=cv2.imread("BlobTest4.jpg") 
rgb=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)

lower_red = np.array([95,0,0])
upper_red = np.array([255,90,90])

mask=cv2.inRange(rgb,lower_red,upper_red)

params = cv2.SimpleBlobDetector_Params()

params.filterByCircularity = True
params.minCircularity = 0.85

params.minThreshold = 1
params.maxThreshold = 2000

params.filterByArea = True
params.minArea = 33

params.filterByConvexity = True
params.minConvexity = 0.8

params.filterByInertia = True
params.minInertiaRatio = 0.01

blob=cv2.SimpleBlobDetector_create(params)
blob_coordinate=blob.detect(mask)
blob_coordinate_without_mask=blob.detect(img)

founded_blobs=cv2.drawKeypoints(mask,blob_coordinate,np.array([]),(0,255,0),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
founded_blobs_without_mask=cv2.drawKeypoints(img,blob_coordinate2,np.array([]),(0,255,0),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

cv2.namedWindow("mask",cv2.WINDOW_NORMAL)
cv2.namedWindow("founded_blobs_with_mask",cv2.WINDOW_NORMAL)
cv2.namedWindow("founded_blobs_without_mask",cv2.WINDOW_NORMAL)

cv2.imshow("founded_blobs_with_mask",founded_blobs)
cv2.imshow("founded_blobs_without_mask",founded_blobs_without_mask)
cv2.imshow("mask",mask)

cv2.waitKey(0)
cv2.destroyAllWindows

1 answer

  • answered 2022-05-05 21:44 kozmoonot

    I think blob detection detects black on white ones. That's why the mask needs to be reversed.

    reversemask=255-mask
    blob=cv2.SimpleBlobDetector_create(params)
    blob_coordinate=blob.detect(reversemask)
    

    Here is a working code.

    import cv2
    import numpy as np
    
    img=cv2.imread("BlobTest4.jpg")
    rgb=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    
    lower_red = np.array([95,0,0])
    upper_red = np.array([255,90,90])
    
    hsvFrame = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    
    mask=cv2.inRange(rgb,lower_red,upper_red)
    kernal = np.ones((5, 5), "uint8")
    
    red_mask = cv2.dilate(mask, kernal)
    
    params = cv2.SimpleBlobDetector_Params()
    
    params.filterByCircularity = True
    params.minCircularity = 0.85
    
    params.minThreshold = 1
    params.maxThreshold = 2000
    
    params.filterByArea = True
    params.minArea = 33
    
    params.filterByConvexity = True
    params.minConvexity = 0.8
    
    params.filterByInertia = True
    params.minInertiaRatio = 0.01
    
    reversemask=255-mask
    
    blob=cv2.SimpleBlobDetector_create(params)
    blob_coordinate=blob.detect(reversemask)
    
    blob_coordinate_without_mask=blob.detect(img)
    print(blob_coordinate_without_mask)
    print(blob_coordinate)
    founded_blobs=cv2.drawKeypoints(red_mask,blob_coordinate,np.array([]),(0,255,0),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    founded_blobs_without_mask=cv2.drawKeypoints(img,blob_coordinate_without_mask,np.array([]),(0,255,0),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    
    cv2.namedWindow("mask",cv2.WINDOW_NORMAL)
    cv2.namedWindow("founded_blobs_with_mask",cv2.WINDOW_NORMAL)
    cv2.namedWindow("founded_blobs_without_mask",cv2.WINDOW_NORMAL)
    
    cv2.imshow("founded_blobs_with_mask",founded_blobs)
    cv2.imshow("founded_blobs_without_mask",founded_blobs_without_mask)
    cv2.imshow("mask",mask)
    
    cv2.waitKey(0)
    cv2.destroyAllWindows
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum