cv2.approxPolyDP(),cv2.arcLength()运作方式

问题描述 投票:0回答:1

这些功能如何工作?我正在使用Python3.7和OpenCv 4.2.0。在此先感谢。

approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
python opencv computer-vision data-science image-manipulation
1个回答
0
投票

[如果您正在寻找示例片段,下面是一个:

import cv2
import imutils

# edged is the edge detected image
cnts = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
# loop over the contours
for c in cnts:
    # approximate the contour
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    # if our approximated contour has four points, then we
    # can assume that we have found our screen
    if len(approx) == 4:
        screenCnt = approx
        break

在以上代码段中,首先从边缘检测的图像中找到轮廓,然后对轮廓进行排序以找到五个最大轮廓。最后,它在轮廓上循环并使用cv2.approxPolyDP函数平滑和逼近四边形。 cv2.approxPolyDP适用于轮廓中有尖锐边缘(如文档边界)的情况。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.