图像轮廓检测错误:OpenCV、C++

Posted

技术标签:

【中文标题】图像轮廓检测错误:OpenCV、C++【英文标题】:Image Contour Detection Error: OpenCV, C++ 【发布时间】:2013-01-22 22:02:46 【问题描述】:

我正在尝试编写一个程序,以在 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;

【问题讨论】:

你得到的确切错误是什么? 我本来想发布一张图片,但由于它需要 10 个代表点,所以我会尝试输入:调试错误! R6010 -aboard() 已被调用 附言。该程序运行良好,直到它尝试 FindCountours 和 DrawCountours,所以问题一定出在这些部分 OWWWWW 我找到了....问题出在我想要显示的实际结果上,我在 imshow 中给出了错误的参数: WRONG imshow(DisplayWindow, contours) ;正确的imshow(DisplayWindow, **result**);谢谢你的时间你能不能给我投票以获得一些额外的代表点thx 很高兴您发现了错误。需要通过提出好的问题或提供有用的答案来获得声誉。不过,您可以回答自己的问题,这可能会对未来遇到类似问题的访问者有所帮助。 【参考方案1】:

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

Escape the slashes Code safely and check the return of the calls How Visual Studio loads files at runtime

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

【讨论】:

如果你能看到这个问题会很高兴 那是什么问题?

以上是关于图像轮廓检测错误:OpenCV、C++的主要内容,如果未能解决你的问题,请参考以下文章

OPENCV图像轮廓检测

OpenCV 图像轮廓检测

opencv查找轮廓后如何改变轮廓内的颜色C++

利用OpenCV进行图像的轮廓检测

opencv——轮廓发现与轮廓分析

教你如何使用 OpenCV检测图像中的轮廓