Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // OpenCVApplication.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "common.h"
- //--------------FACE DETECTION ---------------
- void faceDetection()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- CascadeClassifier face_cascade;
- CascadeClassifier eye_cascade;
- CascadeClassifier mouth_cascade;
- CascadeClassifier nose_cascade;
- // cale absoluta pt fisierele XML
- String face_cascade_name = "E:/PI/OpenCvPractica/haarcascade_frontalface_alt.xml";
- String eye_cascade_name = "E:/PI/OpenCvPractica/haarcascade_eye.xml";
- String mouth_cascade_name = "E:/PI/OpenCvPractica/haarcascade_mcs_mouth.xml";
- String nose_cascade_name = "E:/PI/OpenCvPractica/haarcascade_mcs_nose.xml";
- if (!face_cascade.load(face_cascade_name))
- {
- printf("Error loading face cascades !\n");
- return;
- }
- if (!eye_cascade.load(eye_cascade_name))
- {
- printf("Error loading eyes cascades !\n");
- return;
- }
- if (!mouth_cascade.load(mouth_cascade_name))
- {
- printf("Error loading mouth cascades !\n");
- return;
- }
- if (!nose_cascade.load(nose_cascade_name))
- {
- printf("Error loading nose cascades !\n");
- return;
- }
- Mat src = imread(fname, CV_LOAD_IMAGE_COLOR);
- Mat dst = src.clone();
- int minFaceSize = 30;
- int compsSize = minFaceSize / 5;
- imshow("Input", src);
- Mat frame_gray;
- cvtColor(dst, frame_gray, CV_RGB2GRAY);
- equalizeHist(frame_gray, frame_gray);
- std::vector<Rect> faces;
- face_cascade.detectMultiScale(dst, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(minFaceSize, minFaceSize));
- for (int i = 0; (unsigned)i < (unsigned)(faces.size()); i++)
- {
- //calc punct central al fetei
- Point center((int)(faces[i].x + faces[i].width*0.5), (int)(faces[i].y + faces[i].height*0.5));
- //se deseneaza un cerc in jurul fetei
- ellipse(dst, center, Size((int)(faces[i].width*0.5), (int)(faces[i].height*0.5)), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
- //GURA
- Rect mouth_rect;
- mouth_rect.x = faces[i].x;
- mouth_rect.y = (int)(faces[i].y + 0.7*faces[i].height); //gura intre 70-99
- mouth_rect.width = faces[i].width;
- mouth_rect.height = (int)(0.29*faces[i].height); //99-70
- Mat mouth_ROI = frame_gray(mouth_rect);
- std::vector<Rect> mouth;
- mouth_cascade.detectMultiScale(mouth_ROI, mouth, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(compsSize, compsSize));
- for (int j = 0; (unsigned)j < (unsigned)(mouth.size()); j++)
- {
- Point center((int)(faces[i].x + mouth[j].x + mouth[j].width*0.5),
- (int)(mouth[j].y + mouth[j].height*0.5 + mouth_rect.y));
- int radius = cvRound((mouth[j].width + mouth[j].height)*0.25);
- circle(dst, center, radius, Scalar(125, 215, 152), 4, 8, 0);
- }
- //NAS
- Rect nose_rect;
- nose_rect.x = faces[i].x;
- nose_rect.y = (int)(faces[i].y + 0.4*faces[i].height); //nas intre 40-75%
- nose_rect.width = faces[i].width;
- nose_rect.height = (int)(0.35*faces[i].height);
- Mat nose_ROI = frame_gray(nose_rect);
- std::vector<Rect> nose;
- nose_cascade.detectMultiScale(nose_ROI, nose, 1.1, 1, 0 | CV_HAAR_SCALE_IMAGE, Size(compsSize, compsSize));
- for (int j = 0; (unsigned)j < (unsigned)(nose.size()); j++)
- {
- Point center((int)(faces[i].x + nose[j].x + nose[j].width*0.5),
- (int)(nose[j].y + nose[j].height*0.5 + nose_rect.y));
- int radius = cvRound((nose[j].width + nose[j].height)*0.25);
- circle(dst, center, radius, Scalar(255, 175, 125), 4, 8, 0);
- }
- //OCHII
- //rigth eye
- Rect eyes_rect; //ochii intre 20-55%
- eyes_rect.x = faces[i].x;
- eyes_rect.y = (int)(faces[i].y + 0.2*faces[i].height);
- eyes_rect.width = faces[i].width; //supraf pe care cauta
- eyes_rect.height = (int)(0.35*faces[i].height);
- Mat eyes_ROI = frame_gray(eyes_rect);
- std::vector<Rect> eyes;
- eye_cascade.detectMultiScale(eyes_ROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(compsSize, compsSize));
- for (int j = 0; (unsigned)j < (unsigned)(eyes.size()); j++)
- {
- Point center((int)(faces[i].x + eyes[j].x + eyes[j].width*0.5),
- (int)(eyes[j].y + eyes[j].height*0.5 + eyes_rect.y));
- int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
- // draw circle around the eye
- circle(dst, center, radius, Scalar(219, 175, 236), 4, 8, 0);
- }
- }
- imshow("Output", dst);
- waitKey(0);
- }
- }
- void detect()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat image;
- image = imread(fname, CV_LOAD_IMAGE_COLOR);
- imshow("Input", image);
- // Load Face cascade (.xml file)
- CascadeClassifier face_cascade;
- if (!face_cascade.load("E:/PI/OpenCvPractica/haarcascade_frontalface_alt.xml"))
- {
- printf("Error! Don`t panic!"); return;
- }
- // Detect faces
- std::vector<Rect> faces;
- face_cascade.detectMultiScale(image, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
- // Draw circles on the detected faces
- for (int i = 0; i < faces.size(); i++)
- {
- Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
- ellipse(image, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
- }
- printf("\nIn imagine sunt %d persoane!\n", faces.size());
- imshow("Output", image);
- waitKey(0);
- }
- }
- int main()
- {
- int op;
- do
- {
- system("cls");
- destroyAllWindows();
- printf("Menu:\n");
- printf(" 1- Detectare a fetei, ochilor, nasului si a gurii\n");
- printf(" 2- Detectarea numarului de persoane dintr-o imagine si incercuirea lor\n");
- printf(" 0 - Exit\n\n");
- printf("Option: ");
- scanf("%d",&op);
- switch (op)
- {
- case 1:
- faceDetection();
- break;
- case 2:
- detect();
- break;
- }
- }
- while (op!=0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement