contour.exe 中的 Opencv 未处理异常
Opencv Unhandled exception in contour.exe
我在 OpenCV 中的代码运行良好,直到我想找到 contours:
findContours(src, contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
然后我不断收到以下错误:
"Unhandled exception at 0x773e3e28 in Contour.exe: Microsoft C++
exception: cv::Exception at memory location 0x002ff3ac.."
你知道这个错误吗?
我的完整代码如下。
谢谢
Mat src=Mat(100,200,CV_64F),newimg;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
for (int i=25;i<80;i++)
for(int j=25;j<80;j++)
src.at<double>(i,j)=1;
imshow("img",src);
waitKey(0);
findContours(src, contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
从 OpenCV 文档中引用 findContours
image – Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. Zero pixels remain 0’s, so the image is treated as binary . You can use compare() , inRange() , threshold() , adaptiveThreshold() , Canny() , and others to create a binary image out of a grayscale or color one. The function modifies the image while extracting the contours. If mode equals to CV_RETR_CCOMP or CV_RETR_FLOODFILL, the input can also be a 32-bit integer image of labels (CV_32SC1).
您可以调整代码,将 CV_64FC1
图像转换为 CV_8UC1
,例如:
...
Mat1b img8u;
src.convertTo(img8u, CV_8U);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(img8u, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
...
此外,从评论中可以看出您使用的是 Visual Studio 2010,但 linking OpenCV 是用 msvc11 (Visual Studio 2012) 构建的。
您需要使用 Visual Studio 2012,或者使用 msvc10 (Visual Studio 2010) 重新编译 OpenCV。如果你决定升级VS,你可以直接转到VS2013(和link到vc12),或者转到VS2015(但你也需要重新编译OpenCV)。
您的问题是您给 "findContours" 一张 CV_64F 图片,而它需要 CV_8UC1 图片。您通常将边缘检测器的输出传递给 findContours。 (例如 Canny)。
如果您将代码修改为以下内容,您可以在通过 Canny 过滤图像后找到图像中的轮廓。
Mat src=Mat(100,200,CV_64F),newimg;
Mat tCannyMat;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
for (int i=25;i<80;i++)
for(int j=25;j<80;j++)
src.at<double>(i,j)=1;
imshow("img",src);
waitKey(0);
int lowThreshold = 0x3f;//This is the single value threshold
int ratio = 3;//This is the ratio to apply for the entire pixel
int kernel_size = 3;//This is the canny kernel size
//You can use your own edge detector/a different one here if you wish.
//Canny merely serves to give a working example.
cv::Canny( src, tCannyMat, lowThreshold, lowThreshold*ratio, kernel_size );
findContours(tCannyMat, contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
我在 OpenCV 中的代码运行良好,直到我想找到 contours:
findContours(src, contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
然后我不断收到以下错误:
"Unhandled exception at 0x773e3e28 in Contour.exe: Microsoft C++ exception: cv::Exception at memory location 0x002ff3ac.."
你知道这个错误吗?
我的完整代码如下。
谢谢
Mat src=Mat(100,200,CV_64F),newimg;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
for (int i=25;i<80;i++)
for(int j=25;j<80;j++)
src.at<double>(i,j)=1;
imshow("img",src);
waitKey(0);
findContours(src, contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
从 OpenCV 文档中引用 findContours
image – Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. Zero pixels remain 0’s, so the image is treated as binary . You can use compare() , inRange() , threshold() , adaptiveThreshold() , Canny() , and others to create a binary image out of a grayscale or color one. The function modifies the image while extracting the contours. If mode equals to CV_RETR_CCOMP or CV_RETR_FLOODFILL, the input can also be a 32-bit integer image of labels (CV_32SC1).
您可以调整代码,将 CV_64FC1
图像转换为 CV_8UC1
,例如:
...
Mat1b img8u;
src.convertTo(img8u, CV_8U);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(img8u, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
...
此外,从评论中可以看出您使用的是 Visual Studio 2010,但 linking OpenCV 是用 msvc11 (Visual Studio 2012) 构建的。
您需要使用 Visual Studio 2012,或者使用 msvc10 (Visual Studio 2010) 重新编译 OpenCV。如果你决定升级VS,你可以直接转到VS2013(和link到vc12),或者转到VS2015(但你也需要重新编译OpenCV)。
您的问题是您给 "findContours" 一张 CV_64F 图片,而它需要 CV_8UC1 图片。您通常将边缘检测器的输出传递给 findContours。 (例如 Canny)。
如果您将代码修改为以下内容,您可以在通过 Canny 过滤图像后找到图像中的轮廓。
Mat src=Mat(100,200,CV_64F),newimg;
Mat tCannyMat;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
for (int i=25;i<80;i++)
for(int j=25;j<80;j++)
src.at<double>(i,j)=1;
imshow("img",src);
waitKey(0);
int lowThreshold = 0x3f;//This is the single value threshold
int ratio = 3;//This is the ratio to apply for the entire pixel
int kernel_size = 3;//This is the canny kernel size
//You can use your own edge detector/a different one here if you wish.
//Canny merely serves to give a working example.
cv::Canny( src, tCannyMat, lowThreshold, lowThreshold*ratio, kernel_size );
findContours(tCannyMat, contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);