Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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;
}