Reine_Ran_
Reine_Ran_

Reputation: 682

Apply function to masked region

I have an image like that:

Image with mask applied

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...

Upvotes: 0

Views: 252

Answers (1)

parthagar
parthagar

Reputation: 941

The simplest way would be to use cv2.mean() function.

It takes two arguments src (having 1 to 4 channels) and mask and returns a vector with mean values for individual channels.

Refer to cv2::mask

Upvotes: 2

Related Questions