# Having error on cv::threshold while using it in a class but on a normal "main entry point" works fine

**URL:** <https://forum.opencv.org/t/having-error-on-cv-threshold-while-using-it-in-a-class-but-on-a-normal-main-entry-point-works-fine/12094>\
**Category:** C++\
**Tags:** programming\
**Created:** [February 23, 2023, 10:41am UTC](https://forum.opencv.org/t/having-error-on-cv-threshold-while-using-it-in-a-class-but-on-a-normal-main-entry-point-works-fine/12094 "2023-02-23T10:41:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CDevJoud](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@CDevJoud](https://forum.opencv.org/u/CDevJoud)\
**Post date:** [February 23, 2023, 10:41am UTC](https://forum.opencv.org/t/having-error-on-cv-threshold-while-using-it-in-a-class-but-on-a-normal-main-entry-point-works-fine/12094/1 "2023-02-23T10:41:59Z")

</div>

Below is my class Function that use cv::threshold. the functionality of it is to check if there any diffreneces of two images 1) it loads the images into cv::Mat 2)find diffrenence 3)apply threshold 4)apply dilate 5) find contours 6) get all contours and store them in a map.  
But as soon as it reach to the function “cv::threshold” the cassert throws an error says buffer\_size % 2 == 0

```auto
BOOL AI::detectDifferences(std::string firstFileName, std::string secondFileName)
			{
				//MatrixImage is just typedef of cv::Mat
				MatrixImage img0 = cv::imread("Assets/Image/popcorn0.jpg", cv::IMREAD_GRAYSCALE);
				MatrixImage img1 = cv::imread("Assets/Image/popcorn1.jpg", cv::IMREAD_GRAYSCALE);

				MatrixImage destination;
				cv::absdiff(img0, img1, destination);

				cv::Mat threshold;
				//cv::threshold(destination, threshold, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
				cv::threshold(destination, threshold, 0, 255, cv::THRESH_BINARY_INV | cv::THRESH_OTSU);
				
				MatrixImage dilate;
				MatrixImage kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(2, 2), cv::Point(-1, -1));
				cv::dilate(threshold, dilate, kernel, cv::Point(-1, -1), -1);

				std::vector<std::vector<cv::Point>> contours;
				std::vector<cv::Vec4i> hierarchy;

				cv::findContours(dilate, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

				this->mapVecRect.insert(std::pair<std::string, OpenCVVectorRect*>(secondFileName, new OpenCVVectorRect()));
				for (auto& i : contours)
				{
					if (cv::contourArea(i) > 100)
					{
						cv::Rect rect = cv::boundingRect(i);
						mapVecRect[secondFileName]->push_back(rect);
					}
				}
				return TRUE;
			}

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [February 23, 2023, 12:10pm UTC](https://forum.opencv.org/t/having-error-on-cv-threshold-while-using-it-in-a-class-but-on-a-normal-main-entry-point-works-fine/12094/2 "2023-02-23T12:10:22Z")

</div>

> [@CDevJoud](#):
>
> the cassert throws an error says buffer\_size % 2 == 0

please post **exact** error msg, thank you.

> [@CDevJoud](#):
>
> `MatrixImage`

what is this ?

---

<div class="post-metadata">

**Author:** ![CDevJoud](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@CDevJoud](https://forum.opencv.org/u/CDevJoud)\
**Post date:** [February 23, 2023, 12:16pm UTC](https://forum.opencv.org/t/having-error-on-cv-threshold-while-using-it-in-a-class-but-on-a-normal-main-entry-point-works-fine/12094/3 "2023-02-23T12:16:30Z")

</div>

(post deleted by author)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [February 23, 2023, 12:28pm UTC](https://forum.opencv.org/t/having-error-on-cv-threshold-while-using-it-in-a-class-but-on-a-normal-main-entry-point-works-fine/12094/4 "2023-02-23T12:28:16Z")

</div>

what now ? please be \*\*\* 100% exact\*\*\* !  
(since neither of it comes from opencv !)  
cassert is not used here

also: opencv version / os / compiler ?

> but on a normal “main entry point” works fine

how would we know, what you do there ?  
explain, why you need classes at all, here  
(or typedef’s for cv::Mat, see, this all has some ‘noob going overboard on design’ smell … )

how do we know, the err is from threshold() ?

---

<div class="post-metadata">

**Author:** ![CDevJoud](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@CDevJoud](https://forum.opencv.org/u/CDevJoud)\
**Post date:** [February 28, 2023, 7:10pm UTC](https://forum.opencv.org/t/having-error-on-cv-threshold-while-using-it-in-a-class-but-on-a-normal-main-entry-point-works-fine/12094/5 "2023-02-28T19:10:08Z")

</div>

sorry my bad the error was that i am using UTF-16 while the library doesn’t support that!
