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

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

本文介绍了面部检测(如何检测我们检测到的面部周围的圆圈?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



这里是我使用的代码:

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 :

#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,

if (circle->total=1) { bla bla bla ...}





请帮帮我,谢谢各位你的注意。



please help me, thank you all for your attention.

推荐答案


这篇关于面部检测(如何检测我们检测到的面部周围的圆圈?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:11