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