Ask Your Question
1

Assertion eror “ Vector subscript out of range ”Line 1140

asked 2016-04-18 08:21:43 -0500

Dasein gravatar image

updated 2020-10-21 08:53:17 -0500

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;
}
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2016-04-18 15:33:02 -0500

i tried to do some corrections and improvements on your code. i hope it will help.

#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++)
{
rectangle(frame, eyepair[i], Scalar(0, 255, 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++)
{
bool isRightEye = reye[t].x > eyepair[i].width / 2;
reye[t].x += eyepair[i].x;
reye[t].y += eyepair[i].y;
rectangle(frame, reye[t], Scalar(0, 255, 0));

if(isRightEye)
frame3 = grires(reye[t]);
}
for(int z = 0; z < leye.size(); z++)
{
bool isLeftEye = leye[z].x < eyepair[i].width / 2;
leye[z].x += eyepair[i].x;
leye[z].y += eyepair[i].y;
rectangle(frame, leye[z], Scalar(0, 255, 0));

if(isLeftEye)
frame2 = grires(leye[z]);
}
}

imshow("algilanan", frame);
if(!frame2.empty())
    imshow("algilanan2", frame2);

if(!frame3.empty())
    imshow("algilanan3", frame3);
waitKey(33);
}
return 0;
}
edit flag offensive delete link more

Comments

Thank you so much , it works perfect !

Dasein gravatar imageDasein ( 2016-04-18 15:53:18 -0500 )edit

glad it helped. what is your ultimate goal with detecting pupils?

sturkmen gravatar imagesturkmen ( 2016-04-18 16:06:17 -0500 )edit

My main purpose is creating eye tracking and gaze estimation program.If you have further suggestions , It would pleasure for me to listen.

Dasein gravatar imageDasein ( 2016-04-18 16:09:07 -0500 )edit

take a look at this

sturkmen gravatar imagesturkmen ( 2016-04-18 16:21:00 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-04-18 08:21:43 -0500

Seen: 219 times

Last updated: Apr 18 '16