Migrate halcon Intensity function to opencv

Greeting all! i would like to imgrate halcon code as follows to opencv c++

Threshold(OutImg, &Ref_Region, lThresMin, lThresMax);
Connection(Ref_Region, &Ref_Region);
Intensity(Ref_Region, OutImg, &hv_Mean, &hv_Deviation);

  1. i found meanStdDev in opencv but how can i only calculate mean& std dev of certain range of threshold in the image? eg. i only want to calculate mean and std dev of threshold 155-200 in the image and ignore rest of the pixels. if i use mask(to assign rest of the pixels to 0 which is black), meanStdDev still take the whole image as first parameters.

  2. i noticed that in halcon there is a Connection function before Intensity function. so the intensity is calculated based on the connected region but not the whole image. How can i implement in opencv to achieve same result of mean or std dev in halcon?

I am a newbie to opencv and had been searching for answer for few days but no progress…Hope someone can give me some guidance on this! Thanks in advance!

please consult the documentation. meanStdDev can take a mask argument.

https://docs.opencv.org/master/d2/de8/group__core__array.html#ga846c858f4004d59493d7c6a4354b301d

thanks for the reply. if i use mask argument in meanStdDev, pixels with 0(black) are taken into calculation of meanStdDev? how can i avoid that as those pixels are unwanted? i want to calculate mean and std dev of desired pixels

I understand what you’ve asked before. trust me to give you the right advice.

I realize the documentation is somewhat bare but mask parameters are all over OpenCV’s basic operations and they always work the same way: the mask determines which pixels are operated on, and which aren’t. there is no assignment happening. the pixels that are not in the mask are simply ignored.

how about you pass the mask that you already have, as the mask argument?

//create the mask
Mat source;//the source image
Mat test(1024,800,CV_8UC1);
cv::inRange(source, 150, 200, test);

cv::Scalar try_mean, try_stdev;
cv::meanStdDev(source, try_mean, try_stdev, test);

these are how i do the get the mask and pass it as mask argument. Do i create the mask correctly? but still the mean and std dev is not the same as halcon’s

what do you consider “same”? what values do you get?

please provide some data (picture) to reproduce this.

Sorry for the late reply.
I had got the correct result.
Thanks for the useful information for the usage of mask!

I have the same problem, the parameter “mask” in “meanstddev” does not seem to work. How did you solve this problem? Thanks for your answer!!!