2017-03-21 209 views
1

我是新来的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:代码更新

现在,我得到没有检测我的全圆一个问题。

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

回答

1

你需要的工作出了什么BGR号码你的绿的(比方说为了讨论[0, 255, 0]),然后创建一个忽略外的任何颜色的面具公差围绕您的绿色:

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

请参阅this教程一步一步。

玩弄低位和高位以获得正确的行为。然后,你可以找到在面具的轮廓:

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

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

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) 

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

编辑 - 响应的第二个职位

我认为下面应该工作。我没有测试它,但你应该能够至少多做一点调试与画布和掩码显示:

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

谢谢,我的面具是好了很多,现在,我有一个很好的圈子,但有一个嘈杂的灰色背景 但我不能使用cv2.bitwise_and, 此外我的SimpleBlobDetector没有与我现在得到的红色圆圈一起工作,我是否需要反转蒙版/颜色? 非常感谢 –

+1

在帧图像上创建一个蒙版,然后用这个帧“bitwise_and”你的蒙版来获得你的蒙版帧。然后运行你的BlobDetector。 – Aidenhjj

+0

我有我的颜色很好的圈子,但他们不被检测为blob,因为他们不是空的,我认为。我用这个 https://www.learnopencv.com/blob-detection-using-opencv-python-c/ 但它没有与我合作 –

0

,如果你想要做的彩色滤光片,您应该使用HSV色彩空间争取更好的成绩。

ret, frame=im.read() 

帧= cv2.cvtColor(帧,CV2。COLOR_BGR2HSV)添加到您的代码

mask = cv2.inRange(frame, blueLower, blueUpper) 
+0

过滤器没问题,但SimpleBlobDetector没有使用全圆 –