我是 opencv 的新手,对于一个学校项目,我需要用相机检测红色和绿色圆圈,所以我使用了 blobdetection,但它检测到了我的两种颜色,我认为我的掩模不好,每种颜色与特定操作相关联。
目前,我的代码在同一页面上检测到红色和绿色圆圈,但我希望它只检测白色页面上的红色圆圈。
感谢您的帮助
# 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()
Run Code Online (Sandbox Code Playgroud)
编辑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()
Run Code Online (Sandbox Code Playgroud)
您需要计算出绿色的 BGR 数字是多少(为了论证起见[0, 255, 0]),然后创建一个蒙版,忽略绿色周围容差之外的任何颜色:
mask = cv2.inRange(image, lower, upper)
Run Code Online (Sandbox Code Playgroud)
逐步查看本教程。
尝试使用下层和上层以获得正确的行为。然后你可以找到蒙版中的轮廓:
_, contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
Run Code Online (Sandbox Code Playgroud)
然后遍历contours列表找到最大的一个(过滤掉任何可能的噪音):
blob = max(contours, key=lambda el: cv2.contourArea(el))
Run Code Online (Sandbox Code Playgroud)
这就是你最后的“斑点”。您可以通过执行以下操作找到中心:
M = cv2.moments(blob)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
Run Code Online (Sandbox Code Playgroud)
您可以将该中心绘制到图像的副本上,以进行检查:
canvas = im.copy()
cv2.circle(canvas, center, 2, (0,0,255), -1)
Run Code Online (Sandbox Code Playgroud)
显然,这假设图像中只有一个绿色球,没有其他绿色球。但这是一个开始。
编辑 - 对第二篇文章的回复
我认为以下应该有效。我还没有测试过它,但您至少应该能够对显示的画布和蒙版进行更多调试:
# Standard imports
import cv2
import numpy as np;
# Read image
cam = cv2.VideoCapture(0)
while(1):
ret, frame = cam.read()
if not ret:
break
canvas = frame.copy()
lower = (130,150,80) #130,150,80
upper = (250,250,120) #250,250,120
mask = cv2.inRange(frame, lower, upper)
try:
# NB: using _ as the variable name for two of the outputs, as they're not used
_, contours, _ = 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"]))
cv2.circle(canvas, center, 2, (0,0,255), -1)
except (ValueError, ZeroDivisionError):
pass
cv2.imshow('frame',frame)
cv2.imshow('canvas',canvas)
cv2.imshow('mask',mask)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
im.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15457 次 |
| 最近记录: |