首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python检测矩形形状对象

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

Stack Overflow用户
提问于 2017-11-22 05:58:56
回答 1查看 4.4K关注 0票数 1

我正在用python.I做一个项目。我想通过使用python.I打开网络摄像头来检测矩形物体。我试过了,但没有得到准确的答复。如果有任何手指碰到我们的对象,它会在摄像头前面显示对象,它不认识我们的object.please,任何人都可以预先帮助me.Thanks:)这是我的代码: py:

代码语言:javascript
复制
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()

这是我的输出:

非工作输出

工作输出

EN

回答 1

Stack Overflow用户

发布于 2017-11-22 13:12:37

您当前的代码中有此部分:

代码语言:javascript
复制
        #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。您已经知道热得到的轮廓的实际面积,因为它在您的代码。

最后,代码应该如下所示:

代码语言:javascript
复制
        #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)

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

我希望这会有所帮助,但这是一个简单的方法。更多可能的答案也对此有效。你甚至可以用机器学习算法或者矩形拟合算法来思考这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47427767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档