我使用下面的代码查找图像中最大正方形的角。我想做同样的事情,但如果正方形恰好弯曲(像梯形),它仍然可以找到形状的角。如何使用Python的OpenCV模块实现这一点
importedImage = 'shapes.png'
originalImg = cv.imread(importedImage)
#filters image bilaterally and displays it
bilatImg = cv.bilateralFilter(originalImg, 5, 175, 175)
#finds edges of bilaterally filtered image and displays it
edgeImg = cv.Canny(bilatImg, 75, 200)
#gets contours (outlines) for shapes and sorts from largest area to smallest area
contours, hierarchy = cv.findContours(edgeImg, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv.contourArea, reverse=True)
#boxes in largest rectangle
rectangle = cv.minAreaRect(contours[0])
corners = cv.boxPoints(rectangle).astype(np.int32)
再见
为了解决您的问题,您可以使用此代码段。我对新的部分留下了评论,以便更容易理解
额外好处:如果您想检查哪个顶点是哪个顶点,您只需根据this answer添加最后一部分,其中
(x, y)
是上面为每个顶点提供的坐标祝你度过愉快的一天,
安东尼诺
相关问题 更多 >
编程相关推荐