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

编程入门 行业动态 更新时间:2024-10-24 20:14:07
本文介绍了图像轮廓检测错误:OpenCV,C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am trying to write a program to detect contours within an image using OpenCV in the C++ environment.

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

The problem with it is that I don't get a compile error, but instead a runtime error. I have no idea why; I followed the book and OpenCV documentation sources to build the code below and it should work fine but it doesn't... any ideas on what might be wrong...?

#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。他们去:

I bet that you are using the MSVC IDE. Anyway, your code has a lot of problems and I've covered most of them on Stackoverflow. Here they go:

  • 转义斜杠
  • 安全地检查代码并检查调用返回
  • Visual Studio在运行时加载文件
  • Escape the slashes
  • Code safely and check the return of the calls
  • How Visual Studio loads files at runtime

我怀疑你的问题是 imread()失败,因为它没有找到该文件。上述连结可协助您解决这个问题。

I suspect that your problem is that imread() is failing because it didn't found the file. The links above will help you fix that.

更多推荐

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

本文发布于:2023-05-26 20:21:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/265261.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:轮廓   图像   错误   OpenCV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!