简体   繁体   English

python opencv形状检测

[英]python opencv shape detection

For a school project I'm trying to recognize a hexagon shape in a video capture using python and opencv. 对于一个学校项目,我试图在使用python和opencv的视频捕获中识别六边形。 The problem is that when I use this code, it never finds a hexagon. 问题是,当我使用这个代码时,它永远不会找到六边形。 I haven't found a reason why it isn't working so I hope someone can help me. 我没有找到它不工作的原因所以我希望有人可以帮助我。

The code: 编码:

import numpy as np
import cv2

stop_cascade = cv2.CascadeClassifier('cascade.xml')

cap = cv2.VideoCapture(0)

while(True):
    ret, img = cap.read()
    lower = np.array([0,0,0])
    upper = np.array([20,20,20])
    mask = cv2.inRange(img, lower, upper)
    contours, h = cv2.findContours(mask, 1, 2)
    contours.sort(key = len)

    for contour in contours[-3:]:
        approx = cv2.approxPolyDP(contour,0.1*cv2.arcLength(contour,True),True)
        if len(approx) == 6:
            print "hexagon"

    cv2.imshow('image', mask)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Thanks in advance 提前致谢

Edit: To be a little more specific. 编辑:更具体一点。 I've tried mulitple different hexagons but when I print the len(approx) it is never higher then 4. Is this a fault in my object or is it that I have an error in finding the contours i don't know about? 我尝试了多种不同的六边形,但是当我打印len(大约)时,它永远不会高于4.这是我的对象中的错误还是我找不到我不知道的轮廓时出错?

Looks like your program is simplifying the contour beyond what you would like. 看起来你的程序简化了你想要的轮廓。 So approxPolyDP is further simplifying your hexagon to a square or triangle. 因此, aboutPolyDP进一步将六边形简化为正方形或三角形。 This would also explain why len(approx) never returns a value greater than 4. Try changing the value of epsilon that you pass to approxPolyDP . 这也可以解释为什么len(约)永远不会返回大于4的值。尝试更改传递给approxPolyDPepsilon的值。 These are the changes I suggest: 这些是我建议的变化:

  1. Try a range of values for epsilon in approxPolyDP . approxPolyDP中尝试一系列epsilon值。 Change the multiplication factor in 0.1*cv2.arcLength(contour,True) from 0.01 to 0.1 0.1*cv2.arcLength(contour,True)的倍增因子从0.01更改为0.1
  2. Use drawContours to actually display the approx curve. 使用drawContours实际显示近似曲线。 This will help you choose the best multiplication factor 这将帮助您选择最佳的倍增因子

Another approach is using the convex hull of the hexagon rather than the contour and then apply the approxPolyDP function. 另一种方法是使用六边形的凸包而不是轮廓,然后应用approxPolyDP函数。 Ideally, if your image is sharp enough just checking the number of points in the convex hull should be enough to tell you whether the shape is a hexagon or not. 理想情况下,如果你的图像足够锐利,只需检查凸包中的点数就足以告诉你形状是否是六边形。 Simplifying the hull further should help you be more robust to noisy images. 进一步简化船体应该可以帮助您对噪声图像更加稳健。 But again choosing the correct multiplication factor will be important. 但是,再次选择正确的乘法因子将非常重要。

Use this: 用这个:

approx = cv2.approxPolyDP(contour,0.03*cv2.arcLength(contour,True),True) 

you will get the desired result. 你会得到理想的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM