問題描述
這些功能是如何工作的?我正在使用 Python3.7 和 OpenCv 4.2.0.提前致謝.
How do these function works? I am using Python3.7 and OpenCv 4.2.0. Thanks in Advance.
approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
推薦答案
如果您正在尋找示例代碼段,以下是一個:
If you are looking for a example snippet, below is one:
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
函數(shù)來平滑和逼近四邊形.cv2.approxPolyDP
適用于文檔邊界等輪廓中有尖銳邊緣的情況.
In the above snippet, first it finds the contours from a edge detected image, then it sorts the contours to find the five largest contours. Finally it loop over the contours and used cv2.approxPolyDP
function to smooth and approximate the quadrilateral. cv2.approxPolyDP
works for the cases where there are sharp edges in the contours like a document boundary.
這篇關(guān)于cv2.approxPolyDP() , cv2.arcLength() 這些是如何工作的的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!