问题
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