/final.c

http://empty7.codeplex.com · C · 137 lines · 76 code · 41 blank · 20 comment · 4 complexity · 105fc149aaf8af4083578ea459dcfca5 MD5 · raw file

  1. #include "cv.h"
  2. #include "highgui.h"
  3. #include <stdlib.h>
  4. #ifdef _EiC
  5. #define WIN32
  6. #endif
  7. const char* nomeArquivo = "haarcascade_frontalface_alt2.xml";
  8. const char* nomeArquivoOlho = "haarcascade_eye.xml";
  9. void detectaOlho(char* nomeImagem)
  10. {
  11. // Load classification data from xml file.
  12. CvHaarClassifierCascade* cascata = (CvHaarClassifierCascade*)cvLoad( nomeArquivo, 0, 0, 0);
  13. CvHaarClassifierCascade* cascataOlho = (CvHaarClassifierCascade*)cvLoad( nomeArquivoOlho, 0, 0, 0);
  14. //Read the input image
  15. IplImage* imagem = cvLoadImage(nomeImagem, 1);
  16. //Displays the original image
  17. cvNamedWindow("Original", 1);
  18. cvShowImage("Original", imagem );
  19. // Creates a memory storage that will be used by cvHaarDetectObjects
  20. CvMemStorage* memoria = cvCreateMemStorage(0);
  21. CvMemStorage* memoriaOlho = cvCreateMemStorage(0);
  22. // Detection time
  23. double t = (double)cvGetTickCount();
  24. // Using cvHaarDetectObjects
  25. CvSeq* rostos = cvHaarDetectObjects(
  26. imagem, cascata, memoria,
  27. 1.15, 3, 0, cvSize(25, 15));
  28. // Traverses all faces detected in the image
  29. int i, totalRostos = 0, totalOlhos = 0;
  30. int j;
  31. char nomeJanela[32];
  32. for (i = 0; i < (rostos ? rostos->total : 0); i++)
  33. {
  34. // Location of the face
  35. CvRect *r = (CvRect*)cvGetSeqElem( rostos, i );
  36. //Scale
  37. long larguraRosto = r->width;
  38. double escala;
  39. if (larguraRosto > 111)
  40. escala = ((larguraRosto / 50) - 1) * 0.02 + 1;
  41. else
  42. escala = 1.02;
  43. // Clears the memory of eye
  44. cvClearMemStorage(memoriaOlho);
  45. // Delimita a regi?o do rosto
  46. cvSetImageROI(imagem, cvRect(r->x, r->y, r->width, r->height));
  47. // Delimits the region of face
  48. sprintf(nomeJanela,"%s %d", "Rosto ", i);
  49. //Displays face image
  50. cvNamedWindow(nomeJanela, 1);
  51. cvShowImage(nomeJanela, imagem );
  52. cvResetImageROI(imagem);
  53. // Delimits the eye region
  54. cvSetImageROI(imagem, cvRect(r->x, r->y + (r->height/5.5), r->width, r->height/8*3));
  55. // Eye detection
  56. CvSeq* olhos = cvHaarDetectObjects(
  57. imagem, cascataOlho, memoriaOlho,
  58. escala, 3, 0, cvSize(25, 15));
  59. // Draw a rectangle for each eye detected
  60. for( j = 0; j < (olhos ? olhos->total : 0); j++ ) {
  61. r = (CvRect*)cvGetSeqElem( olhos, j );
  62. cvRectangle(imagem,
  63. cvPoint(r->x, r->y),
  64. cvPoint(r->x + r->width, r->y + r->height),
  65. CV_RGB(0, 255, 0), 1, 8, 0);
  66. totalOlhos++;
  67. }
  68. sprintf(nomeJanela,"%s %d", "Olho ", i);
  69. // Shows the image of the eye
  70. cvNamedWindow(nomeJanela, 1);
  71. cvShowImage(nomeJanela, imagem );
  72. cvResetImageROI(imagem);
  73. totalRostos++;
  74. }
  75. // Displays the name of the image, the total number of eyes met and time of detection of eye
  76. t = (double)cvGetTickCount() - t;
  77. printf("Nome da imagem: %s\n", nomeImagem);
  78. printf("Total de Olhos: %d\n", totalOlhos);
  79. printf("Tempo Total = %gms\n\n", t/((double)cvGetTickFrequency()*1000.));
  80. cvWaitKey(0);
  81. //Free resources
  82. for(i = 0; i < totalRostos; i++){
  83. sprintf(nomeJanela,"%s %d", "Olho ", i);
  84. cvDestroyWindow(nomeJanela);
  85. sprintf(nomeJanela,"%s %d", "Rosto ", i);
  86. cvDestroyWindow(nomeJanela);
  87. }
  88. cvReleaseImage( &imagem );
  89. cvDestroyWindow("Original");
  90. }
  91. int main ( int argc, const char* argv[] )
  92. {
  93. /* Resultados fixos*/
  94. detectaOlho("alba1.jpg");
  95. return 0;
  96. }