Ask Your Question
0

Error! Debug Assestion Failed! Expression: vector subscript out of range??

asked 2013-05-07 22:27:53 -0600

Shaban gravatar image

I've an issue here.. When I try to run this program I get an error that halts the program and says, "Debug Assertion Failed! Expression: vector subscript out of range"

Any idea what I'm doing wrong??

#include"stdafx.h"
#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>

int main(int argc, char *argv[])
{
    cv::Mat frame;                                              
    cv::Mat bgmodel;                                            
    cv::Mat fg;                                                 
    cv::VideoCapture cap(0);                                    

    cv::BackgroundSubtractorMOG2 bg;                            
    bg.nmixtures = 3;                                           
    bg.bShadowDetection = true;                                 
    bg.nShadowDetection = 0;                                    
    bg.fTau = 0.5;                                              

    std::vector<std::vector<cv::Point>> contours;
    std::vector<cv::Vec4i> hierarchy;                           

    cv::namedWindow("Frame");                                   
    cv::namedWindow("Background Model");                    

    for(;;)                                                     
    {
        cap >> frame;                                           

        bg.operator()(frame,fg);                                
        bg.getBackgroundImage(bgmodel);                         

        cv::erode(fg,fg,cv::Mat());                             
        cv::dilate(fg,fg,cv::Mat());                            


        float radius;
        cv::Point2f center;
        cv::minEnclosingCircle(cv::Mat(contours[1]),center,radius);

        cv::findContours(                                       
            fg,                                                 
            contours,                                           
            hierarchy,
            CV_RETR_CCOMP,                                      
            CV_CHAIN_APPROX_SIMPLE);                            

        int idx = 0;
        for(;idx >= 0; idx = hierarchy[idx][0])
        {
                cv::drawContours(
                    frame,                                              
                    contours,                                           
                    idx,                                                
                    cv::Scalar( 0, 0, 255 ),                                                
                    CV_FILLED,                                          
                    8,                                                  
                    hierarchy);                                         
        }

        cv::imshow("Frame",frame);                              
        cv::imshow("Background Model",bgmodel);                 

        if(cv::waitKey(30) >= 0) break;
    }
    return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-05-08 01:30:27 -0600

Vladislav Vinogradov gravatar image

The problem is here:

float radius;
cv::Point2f center;
cv::minEnclosingCircle(cv::Mat(contours[1]),center,radius);

cv::findContours(                                       
        fg,                                                 
        contours,                                           
        hierarchy,
        CV_RETR_CCOMP,                                      
        CV_CHAIN_APPROX_SIMPLE);

You use contours in minEnclosingCircle function before it will be computed in findContours. Swap minEnclosingCircle and findContours calls.

edit flag offensive delete link more

Comments

wow thanks, but there's another issue:

show the same error expression on

for(;idx >= 0; idx = hierarchy[idx][0]) { cv::drawContours( frame,
contours,
idx,
cv::Scalar( 0, 0, 255 ),
CV_FILLED,
8,
hierarchy);
}

But when I delete the iteration, my program can running well.

Shaban gravatar imageShaban ( 2013-05-08 01:53:03 -0600 )edit

Question Tools

Stats

Asked: 2013-05-07 22:27:53 -0600

Seen: 1,598 times

Last updated: May 08 '13