- 假如我們有一張圖片,里面有很多紅色的不規(guī)則多邊形侦铜,我們想要獲取這些多邊形的邊緣坐標
- cv2.threshold
- cv2.findContours
- cv2.approxPolyDP
import cv2
import numpy as np
import imutils
from PIL import Image,ImageDraw
lower_red = np.array([0, 255, 255])
upper_red = np.array([1, 255, 255])
area_least = 400 #此處的值根據實際情況設置
img = cv2.imread(filename)
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv,lower_red,upper_red)
#閾值分割(二值化)
ret,th=cv2.threshold(mask,127,255,cv2.THRESH_BINARY)
#獲取輪廓所有點
cnts = cv2.findContours(th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
for c in cnts:
area = cv2.contourArea(c)
if(area<area_least):
continue;
else:
points = cv2.approxPolyDP(c,1,True)
cv2.drawContours(newimg,points,-1,(0,0,255),3)
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者