Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. // OpenCVApplication.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "common.h"
  6.  
  7.  
  8.  
  9. //--------------FACE DETECTION ---------------
  10.  
  11.  
  12. void faceDetection()
  13. {
  14. char fname[MAX_PATH];
  15. while (openFileDlg(fname))
  16. {
  17.  
  18. CascadeClassifier face_cascade;
  19. CascadeClassifier eye_cascade;
  20. CascadeClassifier mouth_cascade;
  21. CascadeClassifier nose_cascade;
  22.  
  23. // cale absoluta pt fisierele XML
  24. String face_cascade_name = "E:/PI/OpenCvPractica/haarcascade_frontalface_alt.xml";
  25. String eye_cascade_name = "E:/PI/OpenCvPractica/haarcascade_eye.xml";
  26. String mouth_cascade_name = "E:/PI/OpenCvPractica/haarcascade_mcs_mouth.xml";
  27. String nose_cascade_name = "E:/PI/OpenCvPractica/haarcascade_mcs_nose.xml";
  28.  
  29. if (!face_cascade.load(face_cascade_name))
  30. {
  31. printf("Error loading face cascades !\n");
  32. return;
  33. }
  34. if (!eye_cascade.load(eye_cascade_name))
  35. {
  36. printf("Error loading eyes cascades !\n");
  37. return;
  38. }
  39.  
  40. if (!mouth_cascade.load(mouth_cascade_name))
  41. {
  42. printf("Error loading mouth cascades !\n");
  43. return;
  44. }
  45.  
  46. if (!nose_cascade.load(nose_cascade_name))
  47. {
  48. printf("Error loading nose cascades !\n");
  49. return;
  50. }
  51.  
  52.  
  53. Mat src = imread(fname, CV_LOAD_IMAGE_COLOR);
  54. Mat dst = src.clone();
  55.  
  56. int minFaceSize = 30;
  57. int compsSize = minFaceSize / 5;
  58.  
  59.  
  60. imshow("Input", src);
  61.  
  62. Mat frame_gray;
  63. cvtColor(dst, frame_gray, CV_RGB2GRAY);
  64. equalizeHist(frame_gray, frame_gray);
  65.  
  66. std::vector<Rect> faces;
  67. face_cascade.detectMultiScale(dst, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(minFaceSize, minFaceSize));
  68.  
  69. for (int i = 0; (unsigned)i < (unsigned)(faces.size()); i++)
  70. {
  71. //calc punct central al fetei
  72. Point center((int)(faces[i].x + faces[i].width*0.5), (int)(faces[i].y + faces[i].height*0.5));
  73. //se deseneaza un cerc in jurul fetei
  74. 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);
  75.  
  76. //GURA
  77.  
  78. Rect mouth_rect;
  79. mouth_rect.x = faces[i].x;
  80. mouth_rect.y = (int)(faces[i].y + 0.7*faces[i].height); //gura intre 70-99
  81. mouth_rect.width = faces[i].width;
  82. mouth_rect.height = (int)(0.29*faces[i].height); //99-70
  83. Mat mouth_ROI = frame_gray(mouth_rect);
  84.  
  85. std::vector<Rect> mouth;
  86. mouth_cascade.detectMultiScale(mouth_ROI, mouth, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(compsSize, compsSize));
  87.  
  88. for (int j = 0; (unsigned)j < (unsigned)(mouth.size()); j++)
  89. {
  90. Point center((int)(faces[i].x + mouth[j].x + mouth[j].width*0.5),
  91. (int)(mouth[j].y + mouth[j].height*0.5 + mouth_rect.y));
  92. int radius = cvRound((mouth[j].width + mouth[j].height)*0.25);
  93. circle(dst, center, radius, Scalar(125, 215, 152), 4, 8, 0);
  94. }
  95.  
  96. //NAS
  97. Rect nose_rect;
  98. nose_rect.x = faces[i].x;
  99. nose_rect.y = (int)(faces[i].y + 0.4*faces[i].height); //nas intre 40-75%
  100. nose_rect.width = faces[i].width;
  101. nose_rect.height = (int)(0.35*faces[i].height);
  102. Mat nose_ROI = frame_gray(nose_rect);
  103.  
  104. std::vector<Rect> nose;
  105. nose_cascade.detectMultiScale(nose_ROI, nose, 1.1, 1, 0 | CV_HAAR_SCALE_IMAGE, Size(compsSize, compsSize));
  106.  
  107. for (int j = 0; (unsigned)j < (unsigned)(nose.size()); j++)
  108. {
  109. Point center((int)(faces[i].x + nose[j].x + nose[j].width*0.5),
  110. (int)(nose[j].y + nose[j].height*0.5 + nose_rect.y));
  111. int radius = cvRound((nose[j].width + nose[j].height)*0.25);
  112. circle(dst, center, radius, Scalar(255, 175, 125), 4, 8, 0);
  113. }
  114.  
  115. //OCHII
  116. //rigth eye
  117. Rect eyes_rect; //ochii intre 20-55%
  118. eyes_rect.x = faces[i].x;
  119. eyes_rect.y = (int)(faces[i].y + 0.2*faces[i].height);
  120. eyes_rect.width = faces[i].width; //supraf pe care cauta
  121. eyes_rect.height = (int)(0.35*faces[i].height);
  122. Mat eyes_ROI = frame_gray(eyes_rect);
  123.  
  124. std::vector<Rect> eyes;
  125. eye_cascade.detectMultiScale(eyes_ROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(compsSize, compsSize));
  126.  
  127. for (int j = 0; (unsigned)j < (unsigned)(eyes.size()); j++)
  128. {
  129.  
  130. Point center((int)(faces[i].x + eyes[j].x + eyes[j].width*0.5),
  131. (int)(eyes[j].y + eyes[j].height*0.5 + eyes_rect.y));
  132. int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
  133.  
  134. // draw circle around the eye
  135. circle(dst, center, radius, Scalar(219, 175, 236), 4, 8, 0);
  136. }
  137.  
  138.  
  139.  
  140. }
  141. imshow("Output", dst);
  142. waitKey(0);
  143. }
  144. }
  145.  
  146. void detect()
  147. {
  148.  
  149. char fname[MAX_PATH];
  150. while (openFileDlg(fname))
  151. {
  152. Mat image;
  153. image = imread(fname, CV_LOAD_IMAGE_COLOR);
  154. imshow("Input", image);
  155.  
  156. // Load Face cascade (.xml file)
  157. CascadeClassifier face_cascade;
  158. if (!face_cascade.load("E:/PI/OpenCvPractica/haarcascade_frontalface_alt.xml"))
  159. {
  160. printf("Error! Don`t panic!"); return;
  161.  
  162. }
  163.  
  164. // Detect faces
  165. std::vector<Rect> faces;
  166. face_cascade.detectMultiScale(image, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
  167.  
  168. // Draw circles on the detected faces
  169. for (int i = 0; i < faces.size(); i++)
  170. {
  171. Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
  172. ellipse(image, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
  173. }
  174.  
  175. printf("\nIn imagine sunt %d persoane!\n", faces.size());
  176.  
  177. imshow("Output", image);
  178.  
  179. waitKey(0);
  180. }
  181. }
  182.  
  183.  
  184. int main()
  185. {
  186. int op;
  187. do
  188. {
  189. system("cls");
  190. destroyAllWindows();
  191. printf("Menu:\n");
  192. printf(" 1- Detectare a fetei, ochilor, nasului si a gurii\n");
  193. printf(" 2- Detectarea numarului de persoane dintr-o imagine si incercuirea lor\n");
  194. printf(" 0 - Exit\n\n");
  195. printf("Option: ");
  196. scanf("%d",&op);
  197. switch (op)
  198. {
  199. case 1:
  200. faceDetection();
  201. break;
  202. case 2:
  203. detect();
  204. break;
  205.  
  206. }
  207. }
  208. while (op!=0);
  209. return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement