Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

c++ OpenCV approxPolyDP causes Unhandled exception

Hi i am working on my school project regarding contours. Everything worked fine until i tried to use approxPolyDP which causes my VS2012 to throw this: Unhandled exception at at 0x000007FEFD0E940D in hugh.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000000002DEC40.

I think that code should be fine, so thats why i am kinda lost.

Here is my code, all help is appreciated:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <sstream>
#include <string>
#include <iostream>
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <opencv\ml.h>
#include <math.h>


using namespace cv;
using namespace std;

  vector<vector<Point> > contours0; 
  vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;


int main( int argc, char** argv )
{
  const char* filename = argc >= 2 ? argv[1] : "pic1.jpg";

 Mat src = imread(filename, 0);
 if(src.empty())
 {
     cout << "can not open " << filename << endl;
     return -1;
 }
 Mat out;
  Canny(src, out, 100,400, 3);


    out = out > 1;
    namedWindow( "Source", 1 );
    imshow( "Source", out );





    findContours( out, contours0, hierarchy,
        CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );


    contours.resize(contours0.size());
    for( size_t k = 0; k < contours0.size(); k++ ){
         approxPolyDP(Mat(contours0[k]), contours[k], 3, true);
    }


    int idx = 0;
    for( ; idx >= 0; idx = hierarchy[idx][0] )
    {
        drawContours( src, contours0, idx, Scalar(128,255,255), 5, 8, hierarchy );
    }

    namedWindow( "Components", 1 );
    imshow( "Components", src );
    waitKey(0);
}