我在使用 opencv 的 findContour()
函数时遇到问题。它崩溃并输出以下错误:
这是我的代码:
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
/// Function header
void thresh_callback(int, void*);
/** @function main */
int main(int argc, char** argv)
{
src = imread("test.png");
/// Load source image and convert it to gray
//src = imread(argv[1], 1);
/// Convert image to gray and blur it
cvtColor(src, src_gray, CV_BGR2GRAY);
blur(src_gray, src_gray, Size(3, 3));
/// Create Window
char* source_window = "Source";
namedWindow(source_window, CV_WINDOW_AUTOSIZE);
imshow(source_window, src);
createTrackbar(" Canny thresh:", "Source", &thresh, max_thresh, thresh_callback);
thresh_callback(0, 0);
waitKey(0);
return(0);
}
/** @function thresh_callback */
void thresh_callback(int, void*)
{
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Detect edges using canny
Canny(src_gray, canny_output, thresh, thresh * 2, 3);
/// Find contours
findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
/// Draw contours
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
for (int i = 0; i< contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
}
/// Show in a window
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", drawing);
}
所有设置都是正确的,例如属性表,因为程序可以运行,但是一旦完成 thresh_callback 函数就会崩溃。 我使用带有 opencv 3.0 的 visual studio 2015。我曾尝试过 visual studio 2012 或尝试其他版本的 opencv,如 2.4.9。不幸的是,它仍然不起作用。希望大家能帮帮我
我能够运行其他图像处理函数,例如 cv::imread
。只有 findContour()
有错误。
更新
最佳答案
您链接了错误的库。
您正在链接:
C:\opencv\build\x64\ vc12 \lib
这意味着您使用的是通过 vc12
编译器 (Visual Studio 2013) 编译的 OpenCV。但是您使用的是 Visual Studio 2015,因此您需要链接到使用 vc14
编译的 OpenCV。
那么,看看你是否有文件夹:
C:\opencv\build\x64\ vc14 \lib
可能不是,因为 OpenCV 3.0 没有 vc14 的预构建。在这种情况下,您可以:
- 用vc14重新编译OpenCV 3.0
- 下载 OpenCV 3.2,它具有针对 x64、vc14 的预构建二进制文件。这是推荐的方法,因为 OpenCV 3.2 添加了一些不错的功能,并修复了几个错误。
- 将 Visual Studio 2013 与您当前的库一起使用
关于c++ - 查找轮廓 OpenCV C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42120724/