1
votes

I am writing a function that finds and returns the center of any image given to the system (they are mostly circular objects.)

When running the findContours method using OpenCV3.10, the function throws an error in the vector class. Here is my code:

cv::Mat image = next_image(cam);
cv::Mat gray; cv::Mat thresh; cv::Mat conv;

cv::Mat canny_output; cv::Mat nImg;
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
//threshold and contour the image

cv::cvtColor(image, conv, cv::COLOR_GRAY2RGB);

cv::cvtColor(conv, gray, CV_BGR2GRAY);
cv::blur(gray, gray, cv::Size(5, 5));
cv::threshold(gray, thresh, 60, 355, cv::THRESH_BINARY);
cv::findContours(thresh, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

Visual Studio throws the Exception Unhandled when it hits findContours (and specifically in the vector file that it uses) and the message says

Unhandled exception at 0x5825AF78 (opencv_core310.dll) in Laser_Tracking.exe: 0xC0000005: Access violation reading location 0xDDDDDDD9.

I am currently using Visual Studios 2019 to run OpenCV.

2
Debug or release build? Which version of visual studio was that OpenCV binary built with?Dan Mašek
Unfortunately, I don't have a running OpenCV 3.1.0 dev environment right now, but just because it's the line directly before, and I have seen some really stupid errors with OpenCV: In cv::threshold(gray, thresh, 60, 355, cv::THRESH_BINARY), I guess 355 is a typo and it should be 255!? As I said, seems like a stupid idea, but just for sanity, I would fix that and test again. Can't reproduce the error with OpenCV 3.4.0 (Debug), 4.0.0 or 4.1.0 (Debug/Release) by the way.HansHirse
@HansHirse I changed it to this value, nothing really changed.Ryanator13

2 Answers

1
votes

I tested and found your problem. This seems to be a problem with OpenCV configuration. My environment is also VS2019 and OpenCV 3.1.0

In the configuration of the Linker, I added opencv_world310.lib and opencv_world310d.lib. That is to say, I added the release and debug versions of the library, and I was running in debug mode, which may have no impact on other functions, but it is different for findContours() function, so I changed the order of the two libraries, or we can delete one of the libraries, which depends on your own mode.

This is a problem left when we configure Visual Studio. Since there is no error reported, the problem has not been found. In this case, we need to develop good configuration habits.

0
votes

I literally don't know what happend but in my case i just moved the function couple lines further and unhandled exception has gone, when i go back the error occurs once more.