輪廓逼近原理與OpenCV應用

輪廓逼近原理與OpenCV應用

輪廓逼近原理與OpenCV應用


資料來源: https://mp.weixin.qq.com/s/54krLSRCRX7yTUyq0MnsUg

https://pyimagesearch.com/2021/10/06/opencv-contour-approximation/ 

https://docs.opencv.org/4.x/dd/d49/tutorial_py_contour_features.html



Code

import cv2
import numpy as np

src = cv2.imread('1.png')
cv2.imshow("src", src)

gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
_, thres = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)
cv2.imshow("thresh", thres)

cnts,_ = cv2.findContours(thres, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

output = src.copy()

cv2.drawContours(output, cnts[0], -1, (0, 255, 0), 2)
cv2.imshow("src-contour", output)

res = src.copy()
epsilon = 0.05 * cv2.arcLength(cnts[0], True)
approx = cv2.approxPolyDP(cnts[0],epsilon,True)
cv2.drawContours(res,[approx],-1,(255,0,0),2)

cv2.imshow("approxPolyDP", res)
cv2.waitKey(0)
cv2.destroyAllWindows()

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *