#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

//fungsi deteksi wajah
void detect_and_draw(IplImage* img){
 //inisialisasi
 CvHaarClassifierCascade *cascade=0;
 CvMemStorage *storage=0;

 IplImage* gray=img;

 //baca file xml u/ image detection
 if(!cascade){
  char * file="C:/OpenCV2.1/data/haarcascades/haarcascade_frontalface_alt.xml";
  cascade=(CvHaarClassifierCascade*) cvLoad(file, 0, 0, 0);
  storage=cvCreateMemStorage(0);
 }

 //deteksi wajah
 CvSeq* faces=cvHaarDetectObjects(
  gray,
  cascade,
  storage,
  1.1,
  3,
  CV_HAAR_DO_CANNY_PRUNING,
  cvSize(10, 10));

 int i;

 //menggambar kotak merah pd bagian wajah yg ditemukan
 for(i=0; i<(faces ? faces->total : 0) ; i++){
  CvRect* r=(CvRect*) cvGetSeqElem(faces, i);
  cvRectangle(img,
   cvPoint(r->x, r->y),
   cvPoint(r->x + r->width, r->y + r->height),
   CV_RGB(255, 0, 0),
   1, 8, 0
   );
 }

 //menampilkan gambar hasil deteksi
 cvNamedWindow("hasil");
 cvShowImage("hasil", img);

 cvWaitKey(0);
}

int main(array<System::String ^> ^args){
 //load image asli
 const char* filename="dank.jpg";
 IplImage* img=cvLoadImage(filename);

 //panggil fungsi
 detect_and_draw(img);
}

0 comments:

Post a Comment

 
Cyber Code © 2014. All Rights Reserved. Powered by Blogger
Top