Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Vector Subscript Out of Range - what does it mean and how to solve it?

Hello, I had been using the code for single image and it had working fine. But today I want to have it to run using video. Error now vector subscript out of range. What is that mean?

include "opencv2/highgui/highgui.hpp"

include "opencv2/imgproc/imgproc.hpp"

define PI 3.14159265

include <iostream>

include <windows.h>

include <fstream>

include <time.h>

using namespace cv; using namespace std;

int edgeThresh = 1; int lowThreshold,minthreshold; int const max_lowThreshold = 100; int const max_houghlowThreshold = 100; int ratio = 3; int kernel_size = 3; Mat frame, frame_gray, dst, cdst,imHSV, frame_rgb, draw; Mat hsv_channels[3]; int flag_off=0; ofstream outputfile; double pixelwhitevalue [5]; char filename[80]; RNG rng(12345); double TOTALM00; double TOTALLength;

int main(int argc, char** argv)
{
VideoCapture cap("C:\\db\\Day3Full\\13pm.wmv"); 
Mat mask =imread("c:\\db\\maskcrop.jpg",1);

while(1)
{

   bool bSuccess = cap.read(frame); // read a new frame from video

    if (!bSuccess) //if not success, break loop
    {
                    cout << "Cannot read the frame from video file" << endl;
                    break;
    }

    imshow("MyVideo", frame); 
    draw = frame.clone();

 Rect rectangle (0,200,frame.cols,frame.rows/2);
 frame = frame(rectangle);
 bitwise_and(frame,mask,frame); 
 imshow("aftermask",frame);

cvtColor(frame, frame_gray, CV_BGR2GRAY);
threshold(frame_gray, frame_gray, 190, 255, THRESH_BINARY);

erode(frame_gray, frame_gray, Mat());
dilate(frame_gray, frame_gray, Mat());  

imshow("clean binary", frame_gray); 

vector< vector<Point> > contours;
findContours(frame_gray, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);

vector<moments> mu(contours.size() ); for( int i = 0; i < contours.size(); i++ ) { mu[i] = moments( contours[i], false ); TOTALM00 = TOTALM00 + mu[i].m00; }

Moments mom = cv::moments(contours[0]); double hu[7]; HuMoments(mom, hu);

vector<point2f> mc( contours.size() );

for( int i = 0; i < contours.size(); i++ ) { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); std::cout << "x1 " << mc[i] << std::endl; }

Point2f a = mc[0]; Point2f b = mc[1]; double res = cv::norm(a-b); std::cout << "Res" <<res &lt;&lt;="" std::endl;<="" p="">

drawContours(frame, contours, -1, Scalar(0,0,255), 2);

printf("\t Info: Area and Contour Length \n"); for( int i = 0; i< contours.size(); i++ ) { printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f - Hu:%.2f\n", i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], false ),hu[5]); TOTALLength = TOTALLength + arcLength( contours[i], false ); }

switch(waitKey(1)){

case 27:
    return 0;

}

} }

click to hide/show revision 2
No.2 Revision

updated 2015-09-17 04:40:38 -0600

berak gravatar image

Vector Subscript Out of Range - what does it mean and how to solve it?

Hello, I had been using the code for single image and it had working fine. But today I want to have it to run using video. Error now vector subscript out of range. What is that mean?

include "opencv2/highgui/highgui.hpp"

include "opencv2/imgproc/imgproc.hpp"

define
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#define PI 3.14159265

include <iostream>

include <windows.h>

include <fstream>

include <time.h>

3.14159265 #include <iostream> #include <windows.h> #include <fstream> #include <time.h> using namespace cv; using namespace std;

std; int edgeThresh = 1; int lowThreshold,minthreshold; int const max_lowThreshold = 100; int const max_houghlowThreshold = 100; int ratio = 3; int kernel_size = 3; Mat frame, frame_gray, dst, cdst,imHSV, frame_rgb, draw; Mat hsv_channels[3]; int flag_off=0; ofstream outputfile; double pixelwhitevalue [5]; char filename[80]; RNG rng(12345); double TOTALM00; double TOTALLength;

TOTALLength;



    int main(int argc, char** argv)
 {
 VideoCapture cap("C:\\db\\Day3Full\\13pm.wmv"); 
 Mat mask =imread("c:\\db\\maskcrop.jpg",1);

 while(1)
 {

    bool bSuccess = cap.read(frame); // read a new frame from video

     if (!bSuccess) //if not success, break loop
     {
                     cout << "Cannot read the frame from video file" << endl;
                     break;
     }

     imshow("MyVideo", frame); 
     draw = frame.clone();

  Rect rectangle (0,200,frame.cols,frame.rows/2);
  frame = frame(rectangle);
  bitwise_and(frame,mask,frame); 
  imshow("aftermask",frame);

 cvtColor(frame, frame_gray, CV_BGR2GRAY);
 threshold(frame_gray, frame_gray, 190, 255, THRESH_BINARY);

 erode(frame_gray, frame_gray, Mat());
 dilate(frame_gray, frame_gray, Mat());  

 imshow("clean binary", frame_gray); 

 vector< vector<Point> > contours;
 findContours(frame_gray, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);

vector<moments> vector<Moments> mu(contours.size() ); for( int i = 0; i < contours.size(); i++ ) { mu[i] = moments( contours[i], false ); TOTALM00 = TOTALM00 + mu[i].m00; }

} Moments mom = cv::moments(contours[0]); double hu[7]; HuMoments(mom, hu);

vector<point2f> vector<Point2f> mc( contours.size() );

); for( int i = 0; i < contours.size(); i++ ) { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); std::cout << "x1 " << mc[i] << std::endl; }

} Point2f a = mc[0]; Point2f b = mc[1]; double res = cv::norm(a-b); std::cout << "Res" <<res &lt;&lt;="" std::endl;<="" p="">

<< std::endl;

    drawContours(frame, contours, -1, Scalar(0,0,255), 2);

printf("\t Info: Area and Contour Length \n"); for( int i = 0; i< contours.size(); i++ ) { printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f - Hu:%.2f\n", i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], false ),hu[5]); TOTALLength = TOTALLength + arcLength( contours[i], false ); }

switch(waitKey(1)){

}

switch(waitKey(1)){

    case 27:
     return 0;

}

}
}

}

} }

click to hide/show revision 3
No.3 Revision

Vector Subscript Out of Range - what does it mean and how to solve it?

Hello, I had been using the code for single image and it had working fine. But today I want to have it to run using video. Error now vector subscript out of range. What is that mean?

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#define PI 3.14159265
#include <iostream>
#include <windows.h>
#include <fstream>
#include <time.h>

using namespace cv;
using namespace std;

int edgeThresh = 1;
int lowThreshold,minthreshold;
int const max_lowThreshold = 100;
int const max_houghlowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
Mat frame, frame_gray, dst, cdst,imHSV, frame_rgb, draw;
Mat hsv_channels[3];
int flag_off=0;
ofstream outputfile;
double pixelwhitevalue [5];
char filename[80];
RNG rng(12345);
double TOTALM00;
double TOTALLength;

TOTALLength;   

    int main(int argc, char** argv)
    {
    VideoCapture cap("C:\\db\\Day3Full\\13pm.wmv"); 
    Mat mask =imread("c:\\db\\maskcrop.jpg",1);

    while(1)
    {

       bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
                        cout << "Cannot read the frame from video file" << endl;
                        break;
        }

       imshow("MyVideo", frame); 
       draw = frame.clone();

      Rect rectangle (0,200,frame.cols,frame.rows/2);
      frame = frame(rectangle);
      bitwise_and(frame,mask,frame);        imshow("aftermask",frame);

     cvtColor(frame, frame_gray, CV_BGR2GRAY);
     threshold(frame_gray, frame_gray, 190, 255, THRESH_BINARY);

     erode(frame_gray, frame_gray, Mat());
     dilate(frame_gray, frame_gray, Mat());        imshow("clean binary", frame_gray);       vector< vector<Point> > contours;
     findContours(frame_gray, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);

    vector<Moments> mu(contours.size() );
   for( int i = 0; i < contours.size(); i++ )
     {  mu[i] = moments( contours[i], false ); 
        TOTALM00 = TOTALM00 + mu[i].m00;
     }

    Moments mom = cv::moments(contours[0]); 
   double hu[7];
   HuMoments(mom, hu); 

    vector<Point2f> mc( contours.size() );

   for( int i = 0; i < contours.size(); i++ )
     { {
         mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); 
         std::cout << "x1 " << mc[i] << std::endl; 
     }

    Point2f a = mc[0];
   Point2f b = mc[1];
   double res = cv::norm(a-b);
   std::cout << "Res" <<res << std::endl;

    drawContours(frame, contours, -1, Scalar(0,0,255), 2);

   printf("\t Info: Area and Contour Length \n");
   for( int i = 0; i< contours.size(); i++ )
    {
       printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f - Hu:%.2f\n", i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], false ),hu[5]);
       TOTALLength = TOTALLength + arcLength( contours[i], false );
    }

 switch(waitKey(1)){

    case 27:
        return 0;

 }
  }
 }

Vector Subscript Out of Range - what does it mean and how to solve it?

Hello, I had been using the code for single image and it had working fine. But today I want to have it to run using video. Error now vector subscript out of range. What is that mean?

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#define PI 3.14159265
#include <iostream>
#include <windows.h>
#include <fstream>
#include <time.h>

using namespace cv;
using namespace std;

int edgeThresh = 1;
int lowThreshold,minthreshold;
int const max_lowThreshold = 100;
int const max_houghlowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
Mat frame, frame_gray, dst, cdst,imHSV, frame_rgb, draw;
Mat hsv_channels[3];
int flag_off=0;
ofstream outputfile;
double pixelwhitevalue [5];
char filename[80];
RNG rng(12345);
double TOTALM00;
double TOTALLength;   

    int main(int argc, char** argv)
    {
    VideoCapture cap("C:\\db\\Day3Full\\13pm.wmv"); 
    Mat mask =imread("c:\\db\\maskcrop.jpg",1);

    while(1)
    {

       bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
                        cout << "Cannot read the frame from video file" << endl;
                        break;
        }

      imshow("MyVideo", frame); 
      draw = frame.clone();

      Rect rectangle (0,200,frame.cols,frame.rows/2);
      frame = frame(rectangle);
      bitwise_and(frame,mask,frame);    
      imshow("aftermask",frame);

      cvtColor(frame, frame_gray, CV_BGR2GRAY);
      threshold(frame_gray, frame_gray, 190, 255, THRESH_BINARY);

      erode(frame_gray, frame_gray, Mat());
      dilate(frame_gray, frame_gray, Mat());    

      imshow("clean binary", frame_gray);   

      vector< vector<Point> > contours;
      findContours(frame_gray, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);

     vector<Moments> mu(contours.size() );
     for( int i = 0; i < contours.size(); i++ )
     { 
         mu[i] = moments( contours[i], false ); 
        TOTALM00 = TOTALM00 + mu[i].m00;
     }

    Moments mom = cv::moments(contours[0]); 
    double hu[7];
    HuMoments(mom, hu); 

    vector<Point2f> mc( contours.size() );

    for( int i = 0; i < contours.size(); i++ )
    {
         mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); 
         std::cout << "x1 " << mc[i] << std::endl; 
    }

   Point2f a = mc[0];
   Point2f b = mc[1];
   double res = cv::norm(a-b);
   std::cout << "Res" <<res << std::endl;

   drawContours(frame, contours, -1, Scalar(0,0,255), 2);

   printf("\t Info: Area and Contour Length \n");
   for( int i = 0; i< contours.size(); i++ )
   {
       printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f - Hu:%.2f\n", i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], false ),hu[5]);
       TOTALLength = TOTALLength + arcLength( contours[i], false );
   }

  switch(waitKey(1)){

    case 27:
        return 0;

   }
  }
  }

image description