面积筛选

#筛选出大于特定大小的轮廓
import cv2
import numpy as np
o = cv2.imread('21result07.bmp')
#cv2.imshow("original",o)
gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)

ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
newImg = np.zeros(o.shape,np.uint8)
image,contours, hierarchy = cv2.findContours(binary,
                                             cv2.RETR_LIST,
                                             cv2.CHAIN_APPROX_SIMPLE)
n=len(contours)
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),2)
    if cv2.contourArea(contours[i])>1000:
        cv2.imshow("contours[" + str(i)+"]",contoursImg[i])
        newImg = cv2.add(newImg,contoursImg[i])
        #cv2.imwrite("contours[" + str(i) + "].bmp", contoursImg[i])
cv2.imwrite('newImg.bmp',newImg)
cv2.waitKey()
cv2.destroyAllWindows()
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐