0

After segmentation, I have labels and gray images. I would like to compute average intensity for each label, i.e. mean of intensity values where label pixels have a particular value. Something like this:

// both are 1-channel
cv::Mat gray, labels
// ...
float avg=cv::mean(gray(labels==1));

cv::MatExpr does not convert (at least not automatically) to cv::Range which could be used as ROI for the gray image, so this example does not compile.

Is there an easy way for this, except of writing the loop explicitly?

eudoxos
  • 18,545
  • 10
  • 61
  • 110
  • 2
    since cv::mean allows the usage of masks, you can just use that optional parameter: C++: Scalar mean(InputArray src, InputArray mask=noArray()) – Micka Sep 02 '18 at 11:34

1 Answers1

3

use a mask:

float avg=cv::mean(gray, (labels==1));

"labels" must have same dimensions as "gray"

Micka
  • 19,585
  • 4
  • 56
  • 74