我是新来的opencv和学校项目,我需要用相机检测红色和绿色圆圈,所以我使用blobdetection,但它检测到我的两种颜色,我认为我的面具不好,每种颜色都与特定的动作相关联。检测不同颜色的斑点opencv
此刻,我的代码在同一页面上检测到红色和绿色圆圈,但我希望它只检测白色页面上的红色圆圈。
感谢您的帮助
# Standard imports
import cv2
import numpy as np;
# Read image
im = cv2.VideoCapture(0)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 100;
params.maxThreshold = 200;
# Filter by Area.
params.filterByArea = True
params.minArea = 200
params.maxArea = 20000
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.1
# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.1
blueLower = (0,85,170) #100,130,50
blueUpper = (140,110,255) #200,200,130
while(1):
ret, frame=im.read()
mask = cv2.inRange(frame, blueLower, blueUpper)
mask = cv2.erode(mask, None, iterations=0)
mask = cv2.dilate(mask, None, iterations=0)
frame = cv2.bitwise_and(frame,frame,mask = mask)
# Set up the detector with default parameters.
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(mask)
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(mask, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Display the resulting frame
frame = cv2.bitwise_and(frame,im_with_keypoints,mask = mask)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
im.release()
cv2.destroyAllWindows()
编辑1:代码更新
现在,我得到没有检测我的全圆一个问题。
版本二
# Standard imports
import cv2
import numpy as np;
# Read image
im = cv2.VideoCapture(0)
while(1):
ret, frame=im.read()
lower = (130,150,80) #130,150,80
upper = (250,250,120) #250,250,120
mask = cv2.inRange(frame, lower, upper)
lower, contours, upper = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
blob = max(contours, key=lambda el: cv2.contourArea(el))
M = cv2.moments(blob)
center = (int(M["m10"]/M["m00"]), int(M["m01"]/M["m00"]))
canvas = im.copy()
cv2.circle(canvas, center, 2, (0,0,255), -1)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
im.release()
cv2.destroyAllWindows()
谢谢,我的面具是好了很多,现在,我有一个很好的圈子,但有一个嘈杂的灰色背景 但我不能使用cv2.bitwise_and, 此外我的SimpleBlobDetector没有与我现在得到的红色圆圈一起工作,我是否需要反转蒙版/颜色? 非常感谢 –
在帧图像上创建一个蒙版,然后用这个帧“bitwise_and”你的蒙版来获得你的蒙版帧。然后运行你的BlobDetector。 – Aidenhjj
我有我的颜色很好的圈子,但他们不被检测为blob,因为他们不是空的,我认为。我用这个 https://www.learnopencv.com/blob-detection-using-opencv-python-c/ 但它没有与我合作 –