cv2.approxPolyDP() , cv2.arcLength() 这些是如何工作的

Adi*_*and 8 python opencv image-manipulation computer-vision data-science

这些功能是如何工作的?我正在使用 Python3.7 和 OpenCv 4.2.0。提前致谢。

approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
Run Code Online (Sandbox Code Playgroud)

amr*_*ras 7

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

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
Run Code Online (Sandbox Code Playgroud)

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

  • ```cv2.arcLength()``` 用于计算轮廓的周长。如果第二个参数是“True”,那么它认为轮廓是闭合的。然后,使用该周长来计算“cv2.approxPolyDP()”函数的 epsilon 值,并使用近似形状的精度因子。您可以查看链接:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html 其中有解释。还有 cv2.approxPolyDP() 方法背后的 Douglas-Peucker 算法的 wiki 链接。 (4认同)