面部检测(如何检测我们检测到的面部周围的圆圈)

时间:2015-03-30 20:09:54

标签: c++ visual-studio-2010 opencv include

我的名字是budi。 我是图像处理的新手,最近我一直在努力学习opencv和visual studio。我已经成功检测到脸部,然后画出我已经检测到的脸部周围的圆圈,这要归功于一些DIY网站上的例子。我的问题是,如何检测一个环绕面部的圆圈?所以我可以将它用于“if”条件,例如

if(condition)//框架中至少有一个圆圈 { bla bla bla }

这是我使用的代码:

#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)
{
    // Setup serial port connection and needed variables.
    HANDLE hSerial = CreateFile(L"COM8", GENERIC_READ | GENERIC_WRITE, 0, 0,     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

    if (hSerial !=INVALID_HANDLE_VALUE)
    {
        printf("Port opened! \n");

        DCB dcbSerialParams;
        GetCommState(hSerial,&dcbSerialParams);

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

        //CvMemStorage* p_strStorage;

        char incomingData[256] = "";            // don't forget to pre-allocate memory
    //printf("%s\n",incomingData);
    int dataLength = 256;
    int readResult = 0;

        SetCommState(hSerial, &dcbSerialParams);
        }
    else
    {
        if (GetLastError() == ERROR_FILE_NOT_FOUND)
        {
            printf("Serial port doesn't exist! \n");
        }

        printf("Error while setting up serial port! \n");
    }
    char outputChars[] ="c" ;
    DWORD btsIO;

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

Mat frame, frameCopy, image;



    //int i;                                // loop counter
    //char charCheckForEscKey;          // char for checking key press (Esc exits program)



    //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);


//int servoPosition = 90;
//int servoOrientation = 0;
//int servoPosition1=90;
//int servoOrientation1=0;
//create a loop to capture and find faces
while(true)
{
    //p_imgOriginal = captureFrame;
    //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
    //for(int i = 0; i < faces.size(); i++)
    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 (radius >= 85)
        {
        outputChars[0] = 'a';
            WriteFile(hSerial, outputChars, strlen(outputChars), &btsIO, NULL);

            cout <<  "radius >= 85, advertising begin" << endl;

            //FlushFileBuffers(hSerial);
        }

        else if (radius <= 84)
        {
        outputChars[0] = 'b';
        WriteFile(hSerial, outputChars, strlen(outputChars), &btsIO, NULL);

            //cout <<  "radius >= 85, advertising begin" << endl;
        }
        /*else if (radius >= 85 | X<=164 && X>=276)
        {
            outputChars[0] = 'z';
            WriteFile(hSerial, outputChars, strlen(outputChars), &btsIO, NULL);

            cout <<  "radius >= 85, advertising begin" << endl;

            //FlushFileBuffers(hSerial);
        }

        else if (radius < 85) 
        {
            outputChars[0] = 'b';
            WriteFile(hSerial, outputChars, strlen(outputChars), &btsIO, NULL);

            cout << "radius:  " << radius << "radius < 85, advertising end!" << endl;

            //FlushFileBuffers(hSerial);
        }

        /*if (X>=165 | X<=275)
        {
            outputChars[0]='u';
            WriteFile(hSerial, outputChars, strlen(outputChars), &btsIO, NULL);

            cout <<"Face in the middle of the frame" << endl;

            FlushFileBuffers(hSerial);
        }

        /*if (X<=164 | X>=276)
        {
            outputChars[0]='y';
            WriteFile(hSerial, outputChars, strlen(outputChars), &btsIO, NULL);

            cout <<"no face in the middle of the frame" << endl;

            //FlushFileBuffers(hSerial);

        }*/

    }

    //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;

}

请帮帮我,谢谢大家的关注。

0 个答案:

没有答案