Ask Your Question

Strange Behaviour of OpenCV Functions (Dilate&Erode operations) out of IDE

asked 2017-05-08 10:17:09 -0500

horizon1710 gravatar image

updated 2017-05-08 10:20:06 -0500

I have encountered a bizarre situation. In my project, I am using some morphological operations of OpenCV and they work well when I run the application by Visual Studio (in both Debug and Release modes) but whenever I run the application out of VS (how a release application supposed to be run), directly from a directory, the Erode and Dilate functions produce weird results. I followed the code by attaching the release app as a process to visual studio and debugged it, finally catched what causes this weirdness.

If I use erode and dilate functions like below:

cv::erode(l_Foreground, l_Foreground, cv::Mat(7, 7, CV_8U));

This works as expected in VS, but weird out of VS. With weird, I mean, the value "7", the size of cv::Mat parameters that given to cv::erode is ignored and it uses some default value or something itself and does not produce required results.

The problem is solved when I use this functions as below:

cv::Mat elementErode = cv::getStructuringElement(cv::MorphShapes::MORPH_RECT, cv::Size(7, 7));
cv::erode(l_Foreground, l_Foreground, elementErode);

It works correct everywhere, both in VS and out of VS. I followed the dll calls and both by VS and out of VS runs call the same dlls.

I am using VS2010 and OpenCV 3.1.0.

Any idea about this strange situation?

Thanks in advance.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-05-08 10:25:02 -0500

berak gravatar image

updated 2017-05-08 10:28:44 -0500

your second example is the only "correct" one.

in your first example, cv::Mat(7, 7, CV_8U) produces a Mat of the correct size, but the content (the "pixels", if you want) are left uninitialized, there can be anything in it, you simply get, whatever was in that part of memory before (and yes, that may differ, if you run it from the ide or not)

an important difference between release and debug mode with VS is, that memory gets initialized to a consistent debug pattern in debug (something like cfcfcf, i forgot), but not so in release.

edit flag offensive delete link more


Thanks for the information @berak.

I overlooked that a cv::Mat created in that way may contain uninitialized values. Yes, I know the difference between Debug and Release mode but this problem does not occur in debug or release mode in VS, only if you run it from outside(both debug and release). That was the thing I wonder why.

Thanks again.

horizon1710 gravatar imagehorizon1710 ( 2017-05-09 02:02:16 -0500 )edit

Question Tools

1 follower


Asked: 2017-05-08 10:17:09 -0500

Seen: 304 times

Last updated: May 08 '17