Ask Your Question

Access Violation error with findContours

asked 2012-11-26 12:35:56 -0500

juliaashk gravatar image

updated 2012-11-26 13:27:32 -0500


I am new to C++ and OpenCV. I installed OpenCV on Windows 7 following the instructions from the tutorial on this site as best as I could.

I have been using Microsoft Visual C++ as my IDE to run through many of the tutorials and to get an idea of how the OpenCV functions work.

Recently I tried doing the "Finding Contours in Your Image" tutorial from the following link.

When I run the script I get the following error message:

"Unhandled exception at 0x00000000 in Contours.exe: 0xC0000005: Access violation."

It seems like something is going wrong with the vector definitions (contours and hierarchy) when findContours tries to use them.

Any ideas how to fix this?

Is there possibly some linking issue with DLL's or an incorrect installation? So far everything else has been working.

Thanks for your help,


PS: Also would someone mind explaining to me how these vector definitions work? vector<vector<point> > contours; vector<vec4i> hierarchy;

what is Vec4i?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-11-27 06:26:15 -0500

Michael Burdinov gravatar image

Are you sure that image was read properly and was converted to gray? It is the only input of findContours that may be corrupted so it worth checking (store the image in file just before call to findContours).

About your other question: Contour is stored as array (vector) of Point. So to keep multiple contours you need vector< vector< Point> >. In some application you are interested not only in contours but also in their hierarchy, i.e. which contours are inside another contours. If you are new to OpenCV, start from simpler call to findContours, the one without hierarchy.

findContours( image, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

Here you can see other flags that may be used by findContours.

Here you can see description of Vec4i, if you are still curious.

edit flag offensive delete link more

answered 2014-04-19 15:57:20 -0500

Y Simson gravatar image

I also had a segmentation fault using findContours

Like you I used:

findContours(Segmented, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

which failed at runtime as well as the following:

findContours(Segmented, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);

edit flag offensive delete link more


how can u say that, it exactly write or not

govardhana padavala gravatar imagegovardhana padavala ( 2014-09-26 05:30:04 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2012-11-26 12:35:56 -0500

Seen: 3,529 times

Last updated: Apr 19 '14