简体   繁体   English

将功能套用至遮罩区域

[英]Apply function to masked region

I have an image like that: 我有这样的图像:

应用蒙版的图像

I have both the mask and the original image. 我既有面具也有原始图像。 I would like to calculate the colour temperature of ONLY the ducks region. 我只想计算鸭子区域的色温。

Right now, I'm iterating through each row and column of the image below and getting pixels where their values are not zero. 现在,我正在遍历下面图像的每一行和每一列,并获得其值不为零的像素。 But I think this isn't the right way to do this. 但是我认为这不是正确的方法。 Any suggestions? 有什么建议么?

What I did was: 我所做的是:

xyzImg = cv2.cvtColor(resImage, cv2.COLOR_BGR2XYZ)
x,y,z = cv2.split(xyzImg)
xList=[]
yList=[]
zList=[]

rows=x.shape[0]
cols=x.shape[1]

for i in range(rows):
    for j in range(cols):
        if (x[i][j]!=0) and (y[i][j]!=0) and (z[i][j]!=0):
            xList.append(x[i][j])
            yList.append(y[i][j])
            zList.append(z[i][j])

xAvg = np.mean(xList)
yAvg = np.mean(yList)
zAvg = np.mean(zList)

xs = xAvg / (xAvg + yAvg + zAvg)
ys = yAvg / (xAvg + yAvg + zAvg)

xyChrome = np.array([xs,ys])

But this is very slow and I don't think its right... 但这很慢,我不认为这是正确的...

The simplest way would be to use cv2.mean() function. 最简单的方法是使用cv2.mean()函数。

It takes two arguments src (having 1 to 4 channels) and mask and returns a vector with mean values for individual channels. 它使用两个参数src (具有1至4个通道)和mask并返回一个带有各个通道平均值的向量。

Refer to cv2::mask 参考cv2 :: mask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM