长度筛选
import cv2
import numpy as np
o = cv2.imread('newImg.bmp')
gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)
newImg2 = np.zeros(o.shape,np.uint8)
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
image,contours, hierarchy = cv2.findContours(binary,
cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)
n=len(contours)
print(n)
contoursImg=[]
for i in range(n):
temp=np.zeros(o.shape,np.uint8)
contoursImg.append(temp)
contoursImg[i]=cv2.drawContours(contoursImg[i],
contours,i,(255,255,255),3)
if cv2.arcLength(contours[i],True) > 200:
cv2.imshow("contours[" + str(i)+"]",contoursImg[i])
newImg2 = cv2.add(newImg2, contoursImg[i])
cv2.imwrite('newImg2.bmp',newImg2)
cv2.waitKey()
cv2.destroyAllWindows()