0

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

Reine_Ran_
  • 662
  • 7
  • 25

1 Answers1

2

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

Community
  • 1
  • 1
parthagar
  • 880
  • 1
  • 7
  • 18
  • Thanks a lot for your reply! I'll go try it out tomorrow and accept the answer if it works! :) – Reine_Ran_ Sep 23 '18 at 01:27
  • With reference to the image above, if I want to apply a function (not mean) to the ROI, what should I do? Basically, is there a way to apply a self written function to the masked region via taking in the two arguments? – Reine_Ran_ Sep 24 '18 at 07:56
  • 1
    I don't think there is any in-built opencv function which allow you to do that, you'll have to write your own loops, one advice would be to use threads (https://docs.python.org/3/library/threading.html) to use parallel processing. But opencv has most of the commonly applied operations and as it's opencv, they are more optimized. – parthagar Sep 24 '18 at 10:36
  • Thanks for your advice! :) – Reine_Ran_ Sep 24 '18 at 15:43