- 假如我们有一张图片,里面有很多红色的不规则多边形,我们想要获取这些多边形的边缘坐标
- 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)
网友评论