gpt4 book ai didi

python - 检测不同颜色的 Blob opencv

转载 作者:太空宇宙 更新时间:2023-11-03 22:25:05 25 4
gpt4 key购买 nike

我是 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()

编辑 1: 代码更新

现在我遇到了一个问题,即未检测到我的完整圆圈。

No Blob Detection

第二版

# 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()

最佳答案

您需要计算出您的绿色的 BGR 数字是多少(为了参数的缘故,我们假设 [0, 255, 0]),然后创建一个 mask ,忽略任何超出公差范围的颜色你的绿色:

mask = cv2.inRange(image, lower, upper)

看看this一步一步的教程。

尝试使用 lower 和 upper 以获得正确的行为。然后就可以找到mask中的轮廓了:

_, contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, 
cv2.CHAIN_APPROX_NONE)

然后通过 contours 列表找到最大的一个(过滤掉任何可能的噪音):

blob = max(contours, key=lambda el: cv2.contourArea(el))

这就是您的最终“blob”。您可以通过以下方式找到中心:

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)

显然,这假设图像中只有一个绿色球,没有其他绿色。但这是一个开始。

编辑 - 对第二个帖子的回应

我认为以下应该可行。我还没有测试过它,但您至少应该能够对显示的 Canvas 和蒙版进行更多调试:

# 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()

关于python - 检测不同颜色的 Blob opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42924059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com