Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. Point detectFace(Mat frame, Point priorCenter) {
  2.  
  3. std::vector<Rect> faces;
  4. Mat frame_gray, frame_lab, output, temp;
  5. int h = frame.size().height - 1;
  6. int w = frame.size().width - 1;
  7. int minNeighbors = 2;
  8. bool faceNotFound = false;
  9.  
  10. //CascadeClassifier eyeCascade1;
  11. //CascadeClassifier eyeCascade2;
  12.  
  13.  
  14.  
  15.  
  16. cvtColor(frame, frame_gray, COLOR_BGR2GRAY); // Convert to gray
  17. equalizeHist(frame_gray, frame_gray); // Equalize histogram
  18.  
  19. // Detect face with open source cascade
  20. face_cascade.detectMultiScale(frame_gray, faces,
  21. 1.1, minNeighbors,
  22. 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
  23.  
  24. // iterate over faces
  25. for (size_t i = 0; i < faces.size(); i++) {
  26.  
  27. // Find center of face
  28. Point center(faces[i].x + faces[i].width / 2,
  29. faces[i].y + faces[i].height / 2);
  30.  
  31. // Generate width and height of face, round to closest 1/4 of frame height
  32. h = roundUp(faces[i].height, frame.size().height / 4);
  33. w = 3 * h / 5;
  34.  
  35. // If priorCenter not yet initialized, initialize
  36. if (priorCenter.x == 0) {
  37. priorCenter = center;
  38. temp = outputFrame(frame, center, w, h);
  39. break;
  40. }
  41.  
  42. // Check to see if it's probably the same user
  43. if (abs(center.x - priorCenter.x) < frame.size().width / 6 &&
  44. abs(center.y - priorCenter.y) < frame.size().height / 6) {
  45.  
  46. // Check to see if the user moved enough to update position
  47. if (abs(center.x - priorCenter.x) < 7 &&
  48. abs(center.y - priorCenter.y) < 7){
  49. center = priorCenter;
  50. }
  51.  
  52. // Smooth new center compared to old center
  53. center.x = (center.x + 2 * priorCenter.x) / 3;
  54. center.y = (center.y + 2 * priorCenter.y) / 3;
  55. priorCenter = center;
  56.  
  57. // output frame of only face
  58. temp = outputFrame(frame, center, w, h);
  59.  
  60. break; // exit, primary users face probably found
  61.  
  62. }
  63. else {
  64. faceNotFound = true;
  65. }
  66. }
  67.  
  68. if (faceNotFound)
  69. {
  70.  
  71. // Findface from eyes
  72. Rect r(priorCenter.x, priorCenter.y, w, h);
  73. if (priorCenter.x + w > frame_gray.size().width - 2 &&
  74. priorCenter.y + h > frame_gray.size().height - 2)
  75. {
  76.  
  77. priorCenter = faceFromEyes(priorCenter, frame_gray(r));
  78.  
  79. // Generate temporary face location
  80. temp = outputFrame(frame, priorCenter, w, h);
  81. }
  82. }
  83.  
  84. // Check to see if new face found
  85. if (temp.size().width > 2)
  86.  
  87. {
  88.  
  89. output = temp;
  90. }
  91. else
  92. {
  93.  
  94. output = frame;
  95. }
  96. // Display only face
  97.  
  98.  
  99. imshow("Face ", output);
  100.  
  101. if (output.size().width > 2)
  102.  
  103. // Draw ellipse around face
  104. //ellipse(frame, priorCenter, Size(w / 2, h / 2),0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
  105.  
  106. {
  107.  
  108. //Point pt(priorCenter.x + w, priorCenter.y + h);
  109. // Point pt2(priorCenter.x, priorCenter.y);
  110. //rectangle(output, pt2, pt, CV_RGB(255, 0, 0), 2, CV_AA);
  111. ellipse(frame, priorCenter, Size(w / 2, h / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
  112.  
  113.  
  114. //rectangle(frame, priorCenter,priorCenter CV_RGB(255, 0, 0), 2, CV_AA);
  115.  
  116. // Display output
  117. imshow("output ", frame);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement