여기서는 OpenCV에서 눈을 추적하는 방법을 배웁니다. 눈을 감지한 후 추적은 쉽고 간단한 작업입니다. 감지된 눈을 둘러싸기 위해 원을 사용했습니다. 원의 중심을 추적한다는 것은 눈의 중심을 추적하는 것을 의미합니다. 원의 중심을 추적하려면 두 개의 정수 변수가 필요합니다. 이것은 처음 두 줄(9 번째 그리고 10 line) main() 함수 안에 있습니다. 정수 변수의 이름은 'x_axis'와 'y_axis'입니다.
42행과 43행에서는 중심의 가로 세로 좌표 값이 'x_axis' 및 'y_axis' 변수에 복사되어 원의 중심을 가집니다. 44 라인에서 'cout' 문을 사용하여 중심 값을 표시했습니다. 이것이 눈의 위치를 추적하는 방법입니다.
C++를 사용하여 OpenCV에서 눈 위치를 추적하는 다음 코드.
예
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/objdetect/objdetect.hpp>
using namespace cv;
using namespace std;
int main() {
int x_axis;//Declaring integer variables to store co-ordinate values//
int y_axis;//Declaring integer variables to store co-ordinate values//
Mat frame;//Declaring a matrix to video frame in it//
namedWindow("Detect");//Declaring a window to show our work//
VideoCapture image(0);//capturing video from default camera//
if (!image.isOpened()){ //Error message if video source is not found//
cout << "Couldn't load video from the source.Make sure your camera is working properly." << endl;
system("pause");
return 0;
}
double height = image.set(CAP_PROP_FRAME_HEIGHT, 480);//setting up height of each frame//
double width = image.set(CAP_PROP_FRAME_WIDTH, 640);//setting up width of each frame//
CascadeClassifier face_cascade, eyes_cascade;//declaring a CascadeClassifier object//
face_cascade.load("C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");//loading the cascade classifier//
eyes_cascade.load("C:/opencv/sources/data/haarcascades/haarcascade_eye.xml");
while (true) {
bool temp = image.read(frame);//loading video frames from source to our matrix named frame//
std::vector<Rect>faces;//Declaring a vector named faces//
face_cascade.detectMultiScale(frame, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(100, 100));//detecting the face
for (int i = 0; i < faces.size(); i++){ //for locating the face
Point center(faces[i].x + faces[i].width * 0.5, faces[i].y + faces[i].height * 0.5);//getting the center of the face//
ellipse(frame, center, Size(faces[i].width * 0.5, faces[i].height * 0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);//draw an ellipse on the face//
Mat faceROI = frame(faces[i]);//Taking area of the face as Region of Interest for eyes//
std::vectoreyes;//declaring a vector named eyes//
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(5, 5));//detect eyes in every face//
for (size_t j = 0; j < eyes.size(); j++){ //for locating eyes//
Point center(faces[i].x + eyes[j].x + eyes[j].width * 0.5, faces[i].y + eyes[j].y + eyes[j].height * 0.5);//getting the centers of both eyes//
int radius = cvRound((eyes[j].width + eyes[j].height) * 0.25);//declaring radius of the eye enclosing circles//
circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0);//drawing circle around both eyes//
x_axis = eyes[j].x;//storing x axis location of eyes in x_axis//
y_axis = eyes[j].y;//storing y axis location of eyes in y_axis//
cout << "Position of the eyes is:" << "(" << x_axis << "," << y_axis << ")" << endl;//showing co-ordinate values//
}
}
imshow("Detect", frame);//showing result in window named 'Detect'//
if (waitKey(30) == 27){ //wait time for each frame is 30 milliseconds//
break;
}
}
return 0;
} 출력

