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;
}
}
}
Where does the error arise?
sorry I forgot to put the error message
That is not really helpful. The error indicates you're trying to access a vector element that doesn't exist. You should carefully debug your program and see what line is throwing the error. I'd dare to say that it's caused by
Moments mom = cv::moments(contours[0]);
in some frame where no contours are found, but it's just a risky guess...Hello, I had debug the code one by one and found out the culprit is from this statement. Some of the images I have thresholded would have more than 3 centroids. THis statement below is to calculate the eucladian distance from 2 centroids. Now would it cause a problems if there are more points of centroid detected?
U r CORRECT!! :) I just realized some of my images do not return any centroid.. Thanks so much everyone!!