Detecting trapezium, rhombus, square, quadrilateral, parallelogram by Opencv in Python

為{幸葍}努か 提交于 2020-12-13 03:56:49

问题


I've written a code like this to detect the shapes present in a png image. I get correct output for detecting a square and rectangle but for other shapes like Trapezium, rhombus, quadrilateral and parallelogram I am not able to detect it correctly. What is the correct code to detect these different types of polygon?

_,thrash=cv2.threshold(imgGray,240,255,cv2.THRESH_BINARY)

contours,_=cv2.findContours(thrash,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

for contour in contours:

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

    cv2.drawContours(img,[approx],0,(0,0,0),5)

    x=approx.ravel()[0]

    y=approx.ravel()[1]

    if len(approx)==4:

        x1,y1,w,h=cv2.boundingRect(approx)

        aspectRatio=float(w)/h

        print(aspectRatio)

        if aspectRatio>=0.95 and aspectRatio<=1.05:

            cv2.putText(img,"Square",(x,y),cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,0))

        else:

            cv2.putText(img,"Rectangle",(x,y),cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,0))

来源:https://stackoverflow.com/questions/64569361/detecting-trapezium-rhombus-square-quadrilateral-parallelogram-by-opencv-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!