Hello guys, I am beginner at opencv.The thing is I am trying to do that is cropping eye region from main frame and focusing on pupil.But I got error like this "Assertion eror " Vector subscript out of range "Line 1140."Could you guys have any idea about how to fix that ?
Here is my code :
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, const char** argv)
{
CascadeClassifier eye;
CascadeClassifier righteye;
CascadeClassifier lefteye;
eye.load("haarcascade_mcs_eyepair_small.xml");
righteye.load("haarcascade_mcs_lefteye.xml");
lefteye.load("haarcascade_mcs_righteye.xml");
VideoCapture vid;
vid.open(0);
if (!vid.isOpened())
{
cout<<"webcam yuklenemedi"<<endl;
system("Pause");
return -1;
}
Mat frame;
Mat grires;
Mat frame2;
Mat frame3;
namedWindow("algilanan", WINDOW_AUTOSIZE);
namedWindow("algilanan2", WINDOW_AUTOSIZE);
namedWindow("algilanan3", WINDOW_AUTOSIZE);
while(true)
{
vid>>frame;
cvtColor(frame, grires, CV_BGR2GRAY); //resmi gri renk uzayına çevirir.
//equalizeHist(grires, grires); //istenirse histogram eşitlenir.
vector<Rect> eyepair;
eye.detectMultiScale(grires, eyepair, 1.1, 3, 0, Size(30,30));
for(int i = 0; i < eyepair.size(); i++)
{
Point pt1(eyepair[i].x + eyepair[i].width,eyepair[i].y + eyepair[i].height);
Point pt2(eyepair[i].x, eyepair[i].y);
rectangle(frame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
Mat ROI =grires(eyepair[i]);
vector<Rect> reye;
vector<Rect> leye;
righteye.detectMultiScale(ROI, reye, 1.1, 3, 0, Size(30,30));
lefteye.detectMultiScale(ROI, leye, 1.1, 3, 0, Size(30,30));
for(int t = 0; t < reye.size(); t++)
{
Point pt1(eyepair[i].x + reye[t].x, eyepair[i].y + reye[t].y + reye[t].height);
Point pt2(reye[t].x,reye[t].y);
rectangle(frame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
Rect r1 = reye[t];
frame3=ROI(r1);
}
for(int z = 0; z < leye.size(); z++)
{
Point pt1(eyepair[i].x + leye[z].x, eyepair[i].y + leye[z].y + leye[z].height);
Point pt2(leye[z].x,leye[z].y);
rectangle(frame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
Rect r2 = reye[z];
frame2=ROI(r2);
}
}
imshow("algilanan2", frame);
imshow("algilanan1",frame2);
waitKey(33);
}
return 0;
}