我如何检测带颜色的圆圈

How can i detect circles with color

我要找红色圆圈。我使用了 opencv2,Simple Blob Detector。当我在照片上应用蒙版时,找不到圆圈。它没有错误消息,只是找不到任何圈子。 这就是我得到的 -> Founded_blobs。 我的测试照片 -> BlobTest4.jpg。我的代码如下。

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

我认为斑点检测会检测到白色斑点上的黑色。这就是为什么面具需要被扭转。

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

这是一个工作代码。

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