这是我所拥有的
im = cv2.imread('luffy.jpg')
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,0)
contours,h = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
// return color inside of the contour here
mask = np.zeros(cnt.shape[:2],np.uint8)
mean = cv2.mean(cant,mask) // I think this is promising but so far it returns arrays with just zeros. I think its because I used np.zeros above to find the mask....
moment = cv2.moments(cnt) //maybe this will help?
我找不到这样的内置 openCV 函数。我想也许你可以用时刻来做到这一点?我怎样才能实现这个目标?
编辑:根据 Zaw Lin 给出的建议解决方案,我有这个输入图像:
和这个输出图像:
这将获取每个轮廓内的平均颜色,并使用该颜色将轮廓绘制到最终图像。
import cv2
import numpy as np
im = 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)