这将获得每个轮廓内的平均颜色,并将具有该颜色的轮廓绘制到最终图像。
import cv2import numpy as npim = cv2.imread('/home/zawlin/test.png')gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)contours,h = cv2.findContours(gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)final = np.zeros(im.shape,np.uint8)mask = np.zeros(gray.shape,np.uint8)for i in xrange(0,len(contours)): mask[...]=0 cv2.drawContours(mask,contours,i,255,-1) cv2.drawContours(final,contours,i,cv2.mean(im,mask),-1)cv2.imshow('im',im)cv2.imshow('final',final)cv2.waitKey(0)


