opencv轮廓检测和多边形拟合

2019-11-28  本文已影响0人  jxc1
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)
上一篇下一篇

猜你喜欢

热点阅读