1 回答

TA貢獻1828條經驗 獲得超13個贊
如果您正在尋找一個示例片段,下面是一個:
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適用于文檔邊界等輪廓中有銳邊的情況。
添加回答
舉報