【问题标题】:how to use OpenCV and Python to find corners of a trapezoid similar to finding corners of a square?如何使用 OpenCV 和 Python 找到梯形的角类似于寻找正方形的角?
【发布时间】:2020-09-30 22:34:06
【问题描述】:

我正在使用以下代码来查找图像中最大正方形的角。我想做同样的事情,但如果正方形恰好是翘曲的(如梯形),它仍然可以找到形状的角。如何使用 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)

【问题讨论】:

    标签: python opencv shapes


    【解决方案1】:

    喏,

    为了解决您的问题,您可以使用这个 sn-p。我将 cmets 留在了新部件上以使其更易于理解

    import cv2
    import numpy as np
    
    importedImage = 'shapes.png'
    originalImg = cv2.imread(importedImage)
    
    #filters image bilaterally and displays it
    bilatImg = cv2.bilateralFilter(originalImg, 5, 175, 175)
    
    #finds edges of bilaterally filtered image and displays it
    edgeImg = cv2.Canny(bilatImg, 75, 200)
    
    #gets contours (outlines) for shapes and sorts from largest area to smallest area
    contours, hierarchy = cv2.findContours(edgeImg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)
    
    # drawing red contours on the image
    for con in contours:
        cv2.drawContours(originalImg, con, -1, (0, 0, 255), 3)
    
    # and double-checking the outcome
    cv2.imshow("Contours check",originalImg)
    cv2.waitKey()
    cv2.destroyWindow("Contours check")
    
    # find the perimeter of the first closed contour
    perim = cv2.arcLength(contours[0], True)
    # setting the precision
    epsilon = 0.02*perim
    # approximating the contour with a polygon
    approxCorners = cv2.approxPolyDP(contours[0], epsilon, True)
    # check how many vertices has the approximate polygon
    approxCornersNumber = len(approxCorners)
    print("Number of approximated corners: ", approxCornersNumber)
    
    # can also be used to filter before moving on [if needed]
    # i.e. if approxCornersNumber== 4:
    
    # printing the position of the calculated corners
    print("Coordinates of approximated corners:\n", approxCorners)
    

    奖励:如果您想检查哪个顶点是哪个顶点,您可以简单地根据this answer 附加最后一部分,其中(x, y) 是上面为每个顶点提供的坐标

    originalImg = cv2.circle(originalImg, (x,y), radius=10, color=(255, 255, 255), thickness=-1)
    cv2.imshow("Vertex position",originalImg)
    cv2.waitKey()
    cv2.destroyWindow("Vertex position")
    

    祝你有美好的一天,
    安东尼诺

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 2019-05-20
      相关资源
      最近更新 更多