Click here to Skip to main content
15,888,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my name is budi. i am a newbie in image processing, recently i've been trying to learn about opencv and visual studio. i already succeed to detect the face then draw the circle around the face that i've detected thanks to the example in some DIY website. my question is, how to detect a circle that encircling the face ? so i can use it for "if" condition, for example

if ( condition )//there is at least one circle in the frame { bla bla bla }

here is the code i use :
C++
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/ocl/ocl.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/opencv_modules.hpp>
#include <opencv2/videostab/deblurring.hpp>

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <sstream>
#include <string>

using namespace std;
using namespace cv;


const static Scalar colors[] =  { CV_RGB(0,0,255),
                                  CV_RGB(0,128,255),
                                  CV_RGB(0,255,255),
                                  CV_RGB(0,255,0),
                                  CV_RGB(255,128,0),
                                  CV_RGB(255,255,0),
                                  CV_RGB(255,0,0),
                                  CV_RGB(255,0,255)
                                } ;



void Draw(Mat& img, vector<Rect>& faces, double scale);


int main(int argc, const char** argv)
{
    //buka com8 untuk serial ke arduino
    HANDLE hSerial = CreateFile(L"COM8", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

    if (hSerial !=INVALID_HANDLE_VALUE)
    {
        printf("Tersambung ke port! \n");

        DCB dcbSerialParams;
        GetCommState(hSerial,&dcbSerialParams);

        dcbSerialParams.BaudRate = CBR_9600;
        dcbSerialParams.ByteSize = 8;
        dcbSerialParams.Parity = NOPARITY;
        dcbSerialParams.StopBits = ONESTOPBIT;


        char incomingData[256] = "";//alokasi memori
    //printf("%s\n",incomingData);
    int dataLength = 256;
    int readResult = 0;

        SetCommState(hSerial, &dcbSerialParams);
    }
    else
    {
        if (GetLastError() == ERROR_FILE_NOT_FOUND)
        {
            printf("Tidak ada port yang terhubung! \n");
        }

        printf("Koneksi ke port gagal! \n");
    }
    char outputChars[] ="c" ;
    DWORD btsIO;

 //void Draw(Mat& img, vector<Rect>& faces, double scale);

Mat frame, frameCopy, image;

    //create the cascade classifier object used for the face detection
    CascadeClassifier face_cascade;
    //use the haarcascade_frontalface_alt.xml library
    face_cascade.load("haarcascade_frontalface_alt.xml");

    //setup video capture device and link it to the first capture device
    VideoCapture captureDevice;
    captureDevice.open(0);

    if(captureDevice.open(0) == NULL)
    {                                              // if capture was not successful . . .
        printf("error: capture error \n");  // error message to standard out . . .
        getchar();                                  // getchar() to pause for user see message . . .
    return(-1);
}


        //setup image files used in the capture process
    Mat captureFrame;
    Mat grayscaleFrame;

    //create a window to present the results
    namedWindow("FaceDetection", 1);


    while(true)
    {
        //capture a new image frame
        captureDevice>>captureFrame;

        //convert captured image to gray scale and equalize
        cvtColor(captureFrame, grayscaleFrame, CV_BGR2GRAY);
        imshow("Grayscale", grayscaleFrame);
        equalizeHist(grayscaleFrame, grayscaleFrame);

        //p_strStorage = cvCreateMemStorage(0);

        //create a vector array to store the face found
        std::vector<Rect> faces;

        //find faces and store them in the vector array
        face_cascade.detectMultiScale(grayscaleFrame, faces, 1.1, 3, CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, Size(30,30));

        //draw a circle for all found faces in the vector array on the original image

        //int i = 0;

         for( int i = 0; i < faces.size(); i++ )
        //for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
    {
            Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
            Scalar color = colors[i%8];
            //center.x = cvRound((r->x + r->width*0.5));
            //center.y = cvRound((r->y + r->height*0.5));
            Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
            Point pt2(faces[i].x, faces[i].y);
            int radius;
            int X = faces[i].x;
            int Y = faces[i].y;
            radius = cvRound((faces[i].width + faces[i].height)*0.25);
            //ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 2, 8, 0 );
            rectangle(captureFrame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
            //circle(captureFrame,center,radius,cvScalar(0, 255, 0, 0), 1, 8, 0);
            //cout << "X:" << faces[i].x  <<  "  Y:" << faces[i].y  << endl;


             if (faces.size() > 0)
            {
                    if (radius >= 85)
                {
                outputChars[0] = 'a';
                WriteFile(hSerial, outputChars, strlen(outputChars), &btsIO, NULL);
                cout << "face detected" << endl;
                }
            }

            else
            {
                outputChars[0] = 'b';
                WriteFile(hSerial, outputChars, strlen(outputChars), &btsIO, NULL);
                cout << "no face detected, searching" << endl;
            }


        }

        //print the output
        imshow("FaceDetection", captureFrame);

        //pause for 200ms
        waitKey(60);
    }

    cvDestroyWindow("FaceDetection");
    cvDestroyWindow("Grayscale");

    FlushFileBuffers(hSerial);

    // This closes the Serial Port
    CloseHandle(hSerial);


    return 0;
}


honestly i am making a robot, of course the face is important. first I want this program to detect the face. there is a circle in the frame if there at is at least one face detected. so if there is no circle, it means there is no face detected. and i want to use the circle to make a condition like this,
C++
if (circle->total=1) { bla bla bla ...}


please help me, thank you all for your attention.
Posted
Updated 1-Apr-15 4:19am
v3
Comments
nv3 31-Mar-15 16:37pm    
Why don't you simply ask the result array "faces" for the number of detected faces?
budi permana 1-Apr-15 9:50am    
yes sir. that's also what i mean, indeed.
here is the code i wrote :
if (faces.size() > 0)
{

cout << "face detected" << endl;
}
//no face
else
{
cout << "no face detected, searching" << endl;
}
if there is at least one face detected, the "face detected" will appear. but if there is no face, the "no face detected" is not appear. is there any mistake in my code ? what should i do sir ? thank you
nv3 1-Apr-15 9:57am    
Normally, either the true or the else branch of the if-statement should be executed. If that is not the case, there is probably some syntactical error in your code, like a semicolon at the wrong place. Could you please post your exact code of that if-statement by using the green Improve question button.
budi permana 1-Apr-15 10:23am    
done. i already update my question with my exact code. thanks a lot,sir.
enhzflep 31-Mar-15 18:19pm    
Unless I'm missing something, you've already got a vector of detected faces - it's what you're using to draw the circle(s) in the first place.

The number of faces is clearly given by faces.size()

This number is available after your call to face_cascade.detectMultiScale(grayscaleFrame, faces, 1.1, 3, CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, Size(30,30));

1 solution

The answer is simple: The if (faces.size() > 0) test is nested within a for-loop that is also dependent on faces.size(). If there have been no faces detected, the for-loop is not executed at all, and hence you don't see the "no faces detected" message.
 
Share this answer
 
Comments
budi permana 1-Apr-15 15:09pm    
oh yeah, thanks for the solution sir. what should i do to overcome this ?
nv3 1-Apr-15 18:00pm    
Take the if .. else construct outside the for-loop. Simple as that.
budi permana 2-Apr-15 3:58am    
it works! thanks a lot sir, for helping me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900