# Migrate halcon Intensity function to opencv

**URL:** <https://forum.opencv.org/t/migrate-halcon-intensity-function-to-opencv/2232>\
**Category:** C++\
**Created:** [March 18, 2021, 2:34am UTC](https://forum.opencv.org/t/migrate-halcon-intensity-function-to-opencv/2232 "2021-03-18T02:34:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![albeetea](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/albeetea/32/597_2.png) [@albeetea](https://forum.opencv.org/u/albeetea)\
**Post date:** [March 18, 2021, 2:34am UTC](https://forum.opencv.org/t/migrate-halcon-intensity-function-to-opencv/2232/1 "2021-03-18T02:34:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 18, 2021, 7:42am UTC](https://forum.opencv.org/t/migrate-halcon-intensity-function-to-opencv/2232/2 "2021-03-18T07:42:22Z")

</div>

> [@albeetea](#):
>
> if i use mask(to assign rest of the pixels to 0 which is black)

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

[https://docs.opencv.org/master/d2/de8/group\_\_core\_\_array.html#ga846c858f4004d59493d7c6a4354b301d](https://docs.opencv.org/master/d2/de8/group __core__ array.html#ga846c858f4004d59493d7c6a4354b301d)

---

<div class="post-metadata">

**Author:** ![albeetea](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/albeetea/32/597_2.png) [@albeetea](https://forum.opencv.org/u/albeetea)\
**Post date:** [March 18, 2021, 7:54am UTC](https://forum.opencv.org/t/migrate-halcon-intensity-function-to-opencv/2232/3 "2021-03-18T07:54:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 18, 2021, 8:00am UTC](https://forum.opencv.org/t/migrate-halcon-intensity-function-to-opencv/2232/4 "2021-03-18T08:00:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![albeetea](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/albeetea/32/597_2.png) [@albeetea](https://forum.opencv.org/u/albeetea)\
**Post date:** [March 18, 2021, 8:17am UTC](https://forum.opencv.org/t/migrate-halcon-intensity-function-to-opencv/2232/5 "2021-03-18T08:17:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 18, 2021, 12:13pm UTC](https://forum.opencv.org/t/migrate-halcon-intensity-function-to-opencv/2232/6 "2021-03-18T12:13:16Z")

</div>

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

please provide some data (picture) to reproduce this.

---

<div class="post-metadata">

**Author:** ![albeetea](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/albeetea/32/597_2.png) [@albeetea](https://forum.opencv.org/u/albeetea)\
**Post date:** [May 4, 2021, 1:48am UTC](https://forum.opencv.org/t/migrate-halcon-intensity-function-to-opencv/2232/7 "2021-05-04T01:48:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mxmxmx](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@mxmxmx](https://forum.opencv.org/u/mxmxmx)\
**Post date:** [December 13, 2021, 11:24am UTC](https://forum.opencv.org/t/migrate-halcon-intensity-function-to-opencv/2232/8 "2021-12-13T11:24:52Z")

</div>

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

 ![1639394668(1)](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/e/ec31741b1af9724f61da4fc2a87218c7afafc6f3.png)
