Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
face detection sample code for OpenCV
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv/ml.h>
void doMosaic(IplImage* in, int x, int y,
int width, int height, int size);
int main (int argc, char **argv)
{
int i, c;
IplImage *src_img = 0, *src_gray = 0;
const char *cascade_name = "/opt/local/share/opencv/haarcascades/haarcascade_frontalface_default.xml";
CvHaarClassifierCascade *cascade = 0;
CvMemStorage *storage = 0;
CvSeq *faces;
cascade = (CvHaarClassifierCascade *) cvLoad (cascade_name, 0, 0, 0);
cvNamedWindow ("Capture", CV_WINDOW_AUTOSIZE);
CvCapture *capture = cvCreateCameraCapture(0);
assert(capture != NULL);
while (1) {
src_img = cvQueryFrame (capture);
src_gray = cvCreateImage (cvGetSize(src_img), IPL_DEPTH_8U, 1);
storage = cvCreateMemStorage (0);
cvClearMemStorage (storage);
cvCvtColor (src_img, src_gray, CV_BGR2GRAY);
cvEqualizeHist (src_gray, src_gray);
faces = cvHaarDetectObjects (src_gray, cascade, storage,
1.11, 4, 0, cvSize (40, 40));
for (i = 0; i < (faces ? faces->total : 0); i++) {
CvRect *r = (CvRect *) cvGetSeqElem (faces, i);
doMosaic(src_img, r->x, r->y, r->width, r->height, 20);
}
cvShowImage("Capture", src_img);
cvReleaseImage(&src_gray);
c = cvWaitKey (2);
if (c == '\x1b')
break;
}
cvReleaseCapture (&capture);
cvDestroyWindow ("Capture");
return 0;
}
void doMosaic(IplImage* in, int x0, int y0,
int width, int height, int size)
{
int b, g, r, col, row;
int xMin = size*(int)floor((double)x0/size);
int yMin = size*(int)floor((double)y0/size);
int xMax = size*(int)ceil((double)(x0+width)/size);
int yMax = size*(int)ceil((double)(y0+height)/size);
for(int y=yMin; y<yMax; y+=size){
for(int x=xMin; x<xMax; x+=size){
b = g = r = 0;
for(int i=0; i<size; i++){
if( y+i > in->height ){
break;
}
row = i;
for(int j=0; j<size; j++){
if( x+j > in->width ){
break;
}
b += (unsigned char)in->imageData[in->widthStep*(y+i)+(x+j)*3];
g += (unsigned char)in->imageData[in->widthStep*(y+i)+(x+j)*3+1];
r += (unsigned char)in->imageData[in->widthStep*(y+i)+(x+j)*3+2];
col = j;
}
}
row++;
col++;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
in->imageData[in->widthStep*(y+i)+(x+j)*3] = cvRound((double)b/(row*col));
in->imageData[in->widthStep*(y+i)+(x+j)*3+1] = cvRound((double)g/(row*col));
in->imageData[in->widthStep*(y+i)+(x+j)*3+2] = cvRound((double)r/(row*col));
}
}
}
}
}
@skuby

This comment has been minimized.

Copy link

commented Apr 22, 2014

You are the best!!

@skuby

This comment has been minimized.

Copy link

commented Apr 22, 2014

You are the best!!

@khaled1990khaled

This comment has been minimized.

Copy link

commented Jun 16, 2015

Hi,

why it's not work with opencv2410 ?

it show me this error

First-chance exception at 0x74C44598 in Project3.exe: Microsoft C++ exception: cv::Exception at memory location 0x006EEDE8.

If there is a handler for this exception, the program may be safely continued.

wkernelbase.pdn not loaded

what I have to do to run this code ?

Thnaks

@chfakht

This comment has been minimized.

Copy link

commented Jul 25, 2015

when compiling i get these errors
main_face.c:32:34: error: too few arguments to function ‘cvHaarDetectObjects’
1.11, 4, 0, cvSize (40, 40));
^
In file included from /usr/local/include/opencv/cv.h:71:0,
from main_face.c:1:
/usr/local/include/opencv2/objdetect/objdetect.hpp:140:15: note: declared here
CVAPI(CvSeq_) cvHaarDetectObjects( const CvArr_ image,
^
main_face.c: In function ‘doMosaic’:
main_face.c:62:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int y=yMin; y<yMax; y+=size){
^
main_face.c:62:3: note: use option -std=c99 or -std=gnu99 to compile your code
main_face.c:63:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int x=xMin; x<xMax; x+=size){
^
main_face.c:65:7: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int i=0; i<size; i++){
^
main_face.c:70:9: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int j=0; j<size; j++){
^
main_face.c:82:15: error: redefinition of ‘i’
for(int i=0;i<row;i++){
^
main_face.c:65:15: note: previous definition of ‘i’ was here
for(int i=0; i<size; i++){
^
main_face.c:82:7: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int i=0;i<row;i++){
^
main_face.c:83:9: error: ‘for’ loop initial declarations are only allowed in C99 mode
for(int j=0;j<col;j++){
^

@Fatima16

This comment has been minimized.

Copy link

commented Aug 17, 2015

I have this error while using visual 2010 ,, anybody please help
Unhandled exception at 0x75f6812f in facedetection.exe: Microsoft C++ exception: cv::Exception at memory location 0x001cf7b8..

@Fatima16

This comment has been minimized.

Copy link

commented Aug 17, 2015

error

@cloudlakecho

This comment has been minimized.

Copy link

commented Nov 23, 2015

For chfakht;

(1) The C99 mode probably requires variable declaration at the front, so you may consider int i at the front of code and use it at for loop like (i=0...

(2) cvHaarDetectObjects, I used faces = cvHaarDetectObjects ( src_gray, cascade, storage, 1.11, 4, 0, cvSize (40, 40), cvSize (100, 100) );. According to OpenCV document, we need the maxSize parameter.

@bskander

This comment has been minimized.

Copy link

commented Feb 15, 2016

hi;
when i runned this code i have an assertion error at
assert(capture != NULL);
please help me to fix this problem

@ValeronMEN

This comment has been minimized.

Copy link

commented Mar 29, 2016

Nice!

@anujtripathi04

This comment has been minimized.

Copy link

commented Apr 10, 2016

Can anyone tell me how to compile and execute it?

i'm able to compile it using this command:

g++ facerecognition.cpp -I/usr/local/include/opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_legacy -lopencv_objdetect -o face
but while running the exe it failes giving this error:

VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
OpenCV Error: Null pointer (Invalid classifier cascade) in cvHaarDetectObjectsForROC, file /build/buildd/opencv-2.4.8+dfsg1/modules/objdetect/src/haar.cpp, line 1514
terminate called after throwing an instance of 'cv::Exception'
what(): /build/buildd/opencv-2.4.8+dfsg1/modules/objdetect/src/haar.cpp:1514: error: (-27) Invalid classifier cascade in function cvHaarDetectObjectsForROC

Aborted (core dumped)

@renard00

This comment has been minimized.

Copy link

commented Jun 21, 2016

Hey fatima me too i have the same problem , can you help me if you have resolved the problem !!!

@mehdino

This comment has been minimized.

Copy link

commented Aug 13, 2016

thanks, very very good.

@simar0511

This comment has been minimized.

Copy link

commented Jul 19, 2018

Hi anujtripathi04,

I am getting the same issue , can you please tell how you resolved this issue.

OpenCV Error: Null pointer (Invalid classifier cascade) in cvHaarDetectObjectsForROC, file /build/buildd/opencv-2.4.8+dfsg1/modules/objdetect/src/haar.cpp, line 1514
terminate called after throwing an instance of 'cv::Exception'
what(): /build/buildd/opencv-2.4.8+dfsg1/modules/objdetect/src/haar.cpp:1514: error: (-27) Invalid classifier cascade in function cvHaarDetectObjectsForROC
Regards
Simarjeet Singh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.