在OpenCV_core310.dll中使用findContours函数时,OpenCV引发未处理的异常

OpenCV throwing Unhandled Exception when using the findContours function in opencv_core310.dll

本文关键字:OpenCV 未处理 异常 函数 findContours core310 dll      更新时间:2023-10-16

我正在编写一个函数,该函数可以查找并返回给系统的任何图像的中心(它们大多是圆形对象(

当使用OpenCV3.10运行findContours方法时,该函数会在向量类中引发错误。这是我的代码:

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命中findContours(特别是在它使用的矢量文件中(时,抛出Exception Unhandled,消息显示

Laser_Tracking.exe中0x5825AF78(opencv_core310.dll(处未处理的异常:0xC0000005:读取位置0xDDDDDDD9的访问冲突。

我目前正在使用Visual Studios 2019运行OpenCV。

我测试并发现了您的问题。这似乎是OpenCV配置的一个问题。我的环境也是VS2019和OpenCV 3.1.0

在Linker的配置中,我添加了opencv.world310.lib和opencv_world310d.lib。也就是说,我添加的是该库的发布和调试版本,并且我是在调试模式下运行的,这可能对其他函数没有影响,但findContours((函数不同,所以我更改了这两个库的顺序,或者我们可以删除其中一个库,这取决于您自己的模式。

这是配置Visual Studio时留下的问题。由于没有错误报告,因此没有发现问题。在这种情况下,我们需要养成良好的配置习惯。

我真的不知道发生了什么,但在我的情况下,我只是将函数进一步移动了几行,未处理的异常已经消失,当我返回时,错误再次发生。