如何使用python检测矩形形状对象

时间:2017-11-22 05:58:57

标签: python opencv

我在python中做一个项目。我想通过使用python打开网络摄像头来检测矩形形状对象。我试过但我没有得到准确的答案。我在网络摄像头前显示对象如果有的话手指碰到了我们的物体它不会识别我们的物体。请任何人都可以帮助我。谢谢你提前:) 这是我的代码: 吡啶:

import math
import numpy as np
import cv2

#dictionary of all contours
contours = {}
#array of edges of polygon
approx = []
#scale of the text
scale = 2
#camera
cap = cv2.VideoCapture(0)
print("press q to exit")

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

#calculate angle
def angle(pt1,pt2,pt0):
    dx1 = pt1[0][0] - pt0[0][0]
    dy1 = pt1[0][1] - pt0[0][1]
    dx2 = pt2[0][0] - pt0[0][0]
    dy2 = pt2[0][1] - pt0[0][1]
    return float((dx1*dx2 + dy1*dy2))/math.sqrt(float((dx1*dx1 + dy1*dy1))*(dx2*dx2 + dy2*dy2) + 1e-10)

while(cap.isOpened()):
    #Capture frame-by-frame
    ret, frame = cap.read()
    if ret==True:
        #grayscale
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        #Canny
        canny = cv2.Canny(frame,80,240,3)

        #contours
        canny2, contours, hierarchy = cv2.findContours(canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        for i in range(0,len(contours)):
            #approximate the contour with accuracy proportional to
            #the contour perimeter
            approx = cv2.approxPolyDP(contours[i],cv2.arcLength(contours[i],True)*0.02,True)

            #Skip small or non-convex objects
            if(abs(cv2.contourArea(contours[i]))<100 or not(cv2.isContourConvex(approx))):
                continue

            x,y,w,h = cv2.boundingRect(contours[i])
            vtc = len(approx)
            if(vtc==4):
                cv2.putText(frame,'RECTANGLE',(x,y),cv2.FONT_HERSHEY_SIMPLEX,scale,(255,255,255),2,cv2.LINE_AA)

        #Display the resulting frame
        out.write(frame)
        cv2.imshow('frame',frame)
        cv2.imshow('canny',canny)
        if cv2.waitKey(1) == 1048689: #if q is pressed
            break

#When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

这是我的输出:

非工作输出

first object output

工作输出

Another object Output

1 个答案:

答案 0 :(得分:1)

您目前在代码中有此部分:

        #Skip small or non-convex objects
        if(abs(cv2.contourArea(contours[i]))<100 or not(cv2.isContourConvex(approx))):
            continue

        x,y,w,h = cv2.boundingRect(contours[i])
        vtc = len(approx)
        if(vtc==4):
            cv2.putText(frame,'RECTANGLE',(x,y),cv2.FONT_HERSHEY_SIMPLEX,scale,(255,255,255),2,cv2.LINE_AA)

这里可以在轮廓周围创建一个矩形并比较这些区域。为此可以使用boundingRect,但是,您的手机可能会略微倾斜,因此minAreaRect更适合这种情况。由于区域为((x,y), (w,h), angle),它将返回您关注的w*h(w,h)部分。你已经知道了获得轮廓的实际区域的热点,因为它在你的代码中。

最后代码应如下所示:

        #Skip small or non-convex objects
        if(abs(cv2.contourArea(contours[i]))<100 or not(cv2.isContourConvex(approx))):
            continue

        x,y,w,h = cv2.boundingRect(contours[i])
        vtc = len(approx)
        rect = cv2.minAreaRect(contours[i])
        rectArea = rect[1][0] * rect[1][1]
        contourArea = cv2.contourArea(contours[i])
        # now it will check if the difference is less than 10% of the rect area
        if vtc==4 or abs(rectArea - contourArea) < rectArea * .10:
            cv2.putText(frame,'RECTANGLE',(x,y),cv2.FONT_HERSHEY_SIMPLEX,scale,(255,255,255),2,cv2.LINE_AA)

可能这会起作用,但您可能需要调整阈值(我使用了rectArea的10%)。即使是4点检查也可以省略,如果它是一个矩形,它将完美契合(rectarea-contourarea)= 0.

我希望这会有所帮助,但这是一种简单的方法。更多可能的答案也适用于此。你甚至可以用机器学习算法或矩形拟合算法来考虑它。