1

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

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;
}
Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
Tomazi
  • 781
  • 9
  • 27
  • 46
  • 2
    What is the exact error that you're getting? – Niko Jan 22 '13 at 22:05
  • i was gonna post an image but since its 10 rep points required to do so i will try type it in: Debug Error! R6010 -aboard() has been called – Tomazi Jan 22 '13 at 22:10
  • Ps. The program works fine until it attempts to FindCountours & DrawCountours so the problem must be lying within these parts – Tomazi Jan 22 '13 at 22:14
  • 1
    OWWWWW i found it.... the problem was with the actual result that i wanted to DISPLAY i gave the wrong parameter within imshow: WRONG imshow(DisplayWindow, **contours**); CORRECT `imshow(DisplayWindow, **result**);` Thx for ur time can you give me up vote to gain some extra rep points thx – Tomazi Jan 22 '13 at 22:23
  • 1
    Glad that you found the error. Reputation needs to be gained by asking good questions or providing helpful answers. However, you can answer your own question, which might help future visitors with similar problems. – Niko Jan 22 '13 at 22:28
  • my question is very gd :) with an answer hehe – Tomazi Jan 22 '13 at 22:35
  • @Tomazi You should post your own answer below. Others will make the same error in the future and it may help them. – Alex Chamberlain Jan 22 '13 at 22:43

1 Answers1

1

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:

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.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426