Ask Your Question
2

vector out of range

asked 2013-06-07 08:23:48 -0600

william13200 gravatar image

updated 2013-06-07 08:49:33 -0600

Vladislav Vinogradov gravatar image

Hi there, working on a robotics application.

My program is crashing at this exact point ( i'm proceeding the edges and findcontours operations) it used to work but it seems it doesn't anymore since i moved the Mat and all the declarations out of the function to put it in the whole file, so i can access it in other functions.

void Edges() {

imread then cvtcolor .. blur .. canny.. findcontours.. etc.. it all works.

now i'm proceeding:

for (unsigned int i = 0; i<contours.size(); i++){
drawContours(drawing,contours,i,(255,255,255),1,8,hierarchy,0,point() );

this works again and now:

for (unsigned int i =0; i<contours.size(); i++){
muCircle[i] = moments (contours[i],false);

once this is reached it crashes and i get:

Debug assertion failed
vector subscript out of range.

on msdn i can read that this kind of error is due to accessing a vector value which doesn't exist. But thanks to the debug flag, i can see that the program does crash at this very exact point. and until then i never use vector.

any ideas??

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2013-06-07 08:51:09 -0600

Vladislav Vinogradov gravatar image

Maybe you forgot to allocate memory for muCircle vector?

muCircle.resize(contours.size());

for (unsigned int i =0; i<contours.size(); i++){
    muCircle[i] = moments (contours[i],false);
edit flag offensive delete link more

Comments

1

thanks for your help mate!

the problem was:

i can declare the muCircle, contours, matrix and all variables i need outside the function so i can use them in other functions. BUT, vector<moments> muCircle ( for instance ) , vector<points> etc.. NEED: muCircle.resize(contours.size()); inside the function

william13200 gravatar imagewilliam13200 ( 2013-06-07 09:01:03 -0600 )edit

Question Tools

Stats

Asked: 2013-06-07 08:23:48 -0600

Seen: 709 times

Last updated: Jun 07 '13