/final.c
http://empty7.codeplex.com · C · 137 lines · 76 code · 41 blank · 20 comment · 4 complexity · 105fc149aaf8af4083578ea459dcfca5 MD5 · raw file
- #include "cv.h"
- #include "highgui.h"
- #include <stdlib.h>
- #ifdef _EiC
- #define WIN32
- #endif
-
- const char* nomeArquivo = "haarcascade_frontalface_alt2.xml";
- const char* nomeArquivoOlho = "haarcascade_eye.xml";
-
- void detectaOlho(char* nomeImagem)
- {
- // Load classification data from xml file.
- CvHaarClassifierCascade* cascata = (CvHaarClassifierCascade*)cvLoad( nomeArquivo, 0, 0, 0);
-
- CvHaarClassifierCascade* cascataOlho = (CvHaarClassifierCascade*)cvLoad( nomeArquivoOlho, 0, 0, 0);
-
- //Read the input image
- IplImage* imagem = cvLoadImage(nomeImagem, 1);
-
- //Displays the original image
- cvNamedWindow("Original", 1);
- cvShowImage("Original", imagem );
-
- // Creates a memory storage that will be used by cvHaarDetectObjects
- CvMemStorage* memoria = cvCreateMemStorage(0);
- CvMemStorage* memoriaOlho = cvCreateMemStorage(0);
-
- // Detection time
- double t = (double)cvGetTickCount();
-
- // Using cvHaarDetectObjects
- CvSeq* rostos = cvHaarDetectObjects(
- imagem, cascata, memoria,
- 1.15, 3, 0, cvSize(25, 15));
-
- // Traverses all faces detected in the image
- int i, totalRostos = 0, totalOlhos = 0;
- int j;
- char nomeJanela[32];
-
- for (i = 0; i < (rostos ? rostos->total : 0); i++)
- {
- // Location of the face
- CvRect *r = (CvRect*)cvGetSeqElem( rostos, i );
-
- //Scale
- long larguraRosto = r->width;
- double escala;
- if (larguraRosto > 111)
- escala = ((larguraRosto / 50) - 1) * 0.02 + 1;
- else
- escala = 1.02;
-
- // Clears the memory of eye
- cvClearMemStorage(memoriaOlho);
-
- // Delimita a regi?o do rosto
- cvSetImageROI(imagem, cvRect(r->x, r->y, r->width, r->height));
-
- // Delimits the region of face
- sprintf(nomeJanela,"%s %d", "Rosto ", i);
-
- //Displays face image
- cvNamedWindow(nomeJanela, 1);
- cvShowImage(nomeJanela, imagem );
-
- cvResetImageROI(imagem);
-
- // Delimits the eye region
- cvSetImageROI(imagem, cvRect(r->x, r->y + (r->height/5.5), r->width, r->height/8*3));
-
- // Eye detection
- CvSeq* olhos = cvHaarDetectObjects(
- imagem, cascataOlho, memoriaOlho,
- escala, 3, 0, cvSize(25, 15));
-
- // Draw a rectangle for each eye detected
- for( j = 0; j < (olhos ? olhos->total : 0); j++ ) {
- r = (CvRect*)cvGetSeqElem( olhos, j );
-
- cvRectangle(imagem,
- cvPoint(r->x, r->y),
- cvPoint(r->x + r->width, r->y + r->height),
- CV_RGB(0, 255, 0), 1, 8, 0);
-
- totalOlhos++;
-
- }
-
- sprintf(nomeJanela,"%s %d", "Olho ", i);
-
- // Shows the image of the eye
- cvNamedWindow(nomeJanela, 1);
- cvShowImage(nomeJanela, imagem );
-
- cvResetImageROI(imagem);
-
- totalRostos++;
- }
-
- // Displays the name of the image, the total number of eyes met and time of detection of eye
- t = (double)cvGetTickCount() - t;
- printf("Nome da imagem: %s\n", nomeImagem);
- printf("Total de Olhos: %d\n", totalOlhos);
- printf("Tempo Total = %gms\n\n", t/((double)cvGetTickFrequency()*1000.));
-
- cvWaitKey(0);
-
- //Free resources
- for(i = 0; i < totalRostos; i++){
-
- sprintf(nomeJanela,"%s %d", "Olho ", i);
- cvDestroyWindow(nomeJanela);
-
-
- sprintf(nomeJanela,"%s %d", "Rosto ", i);
- cvDestroyWindow(nomeJanela);
-
- }
-
- cvReleaseImage( &imagem );
-
- cvDestroyWindow("Original");
-
- }
-
- int main ( int argc, const char* argv[] )
- {
-
- /* Resultados fixos*/
- detectaOlho("alba1.jpg");
-
- return 0;
- }