c++ - 图像轮廓检测错误: OpenCV, C++

标签 c++ opencv

我正在尝试编写一个程序来在 C++ 环境中使用 OpenCV 检测图像中的轮廓。

问题是我没有得到编译错误,而是运行时错误。我不知道为什么;我按照这本书和 OpenCV 文档源来构建下面的代码,它应该可以正常工作,但它没有……关于可能出错的任何想法……?

#include "iostream"
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv\ml.h>
#include<opencv\cxcore.h>
#include <iostream> 
#include <string> 
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat)
#include <opencv2/highgui/highgui.hpp> // Video write

using namespace cv;
using namespace std;

Mat image; Mat image_gray; Mat image_gray2; Mat threshold_output;
int thresh=100, max_thresh=255;

int main(int argc, char** argv) {

//Load Image
image =imread("C:/Users/Tomazi/Pictures/Opencv/ayo.bmp");

//Convert Image to gray & blur it
cvtColor( image, 
    image_gray, 
    CV_BGR2GRAY );

blur( image_gray, 
    image_gray2,
    Size(3,3) );
//Threshold Gray&Blur Image
threshold(image_gray2, 
    threshold_output, 
    thresh, 
    max_thresh, 
    THRESH_BINARY);

//2D Container
vector<vector<Point>> contours;

//Fnd Countours Points, (Imput Image, Storage, Mode1, Mode2, Offset??)
findContours(threshold_output,
    contours, // a vector of contours
    CV_RETR_EXTERNAL, // retrieve the external contours
    CV_CHAIN_APPROX_NONE,
    Point(0, 0)); // all pixels of each contours    

// Draw black contours on a white image
Mat result(threshold_output.size(),CV_8U,Scalar(255));

drawContours(result,contours,
    -1, // draw all contours
    Scalar(0), // in black
    2); // with a thickness of 2


    //Create Window
char* DisplayWindow = "Source";
namedWindow(DisplayWindow, CV_WINDOW_AUTOSIZE);
imshow(DisplayWindow, contours);


waitKey(0);
return 1;
}

最佳答案

我打赌您使用的是 MSVC IDE。无论如何,您的代码有很多问题,我已经在 Stackoverflow 上介绍了其中的大部分问题。他们开始了:

我怀疑您的问题是 imread() 失败,因为它没有找到文件。上面的链接将帮助您解决这个问题。

关于c++ - 图像轮廓检测错误: OpenCV, C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468875/

相关文章:

c++ - 代码块 wxWidgets 没有那个文件或目录

c++ - 场景输出在第二次循环时给出错误答案

c++ - 奇怪的 std::list 迭代行为

c++ - 如何在 opencv 中访问 Mat 的第 n 个 channel ?

opencv - 在 opencv 中对齐图像

c++ - 无法通过引用传递值 C++

c++ - 加速简单 Eigen 程序的基本方法

c++ - 使用 for 循环访问随机图像像素

opencv - 投影仪和 OpenCV

c++ - 如何在opencv中将图像转换为浮点图像?