如何检测面部轮廓(侧面)? [英] how to detect the facial profile (side face) ?

查看:98
本文介绍了如何检测面部轮廓(侧面)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我一直在使用opencv和visual studio来尝试一个人脸检测程序,它确实有效。我的问题是,是否可以同时检测正面和侧面(面部轮廓)(在一个程序中一次)?我已经调用了lbpcascade_profileface.xml,然后我使用了haarcascade_profileface.xml但程序运行不正常。有时即使是正面也无法检测到。有什么建议吗?谢谢你

hello guys, i've been using opencv and visual studio to try a face detection program and it works. My question is, whether the front and side faces (facial profile) can be detected simultaneously (at one time in one program)? i already called the lbpcascade_profileface.xml, and then i used the haarcascade_profileface.xml but the program doesn't run properly. and sometimes even the frontal face is undetectable. any suggest please? thank you

推荐答案

你应该知道配置文件面的轮廓不容易被类似功能或LBP功能检测到,因此我建议你使用HOG和SVM profile faces,下面是一个代码,我认为应该至少工作但是使用haar级联分类器。



You should know that the outline of profile faces is not easy to detect with haar like features or LBP features ,thus I suggest you use HOG plus SVM for profile faces, below is a code I think should atleast work but uses haar cascade classifiers.

#include "opencv2/opencv.hpp"

using namespace cv;

CvHaarClassifierCascade *cascade,*cascade_profile; 

CvMemStorage *storage; 

void detectFaces( IplImage *img ); 

 int main( ) 
 { 
	 CvCapture *capture; IplImage *frame = 0; 
	 int key; 
	 char *filename = "haarcascade_frontalface_alt.xml"; 
         char *filename_profile = "haarcascade_profileface.xml"; 

	  capture = cvCreateCameraCapture(0);

	  storage = cvCreateMemStorage(0);

	  cascade = (CvHaarClassifierCascade*)cvLoad( filename,storage, 0, 0 );
          cascade_profile = (CvHaarClassifierCascade*)cvLoad( filename_profile,storage, 0, 0 );

	 assert( cascade && storage && capture && cascade_profile); 
	 cvNamedWindow( "video", 1 ); 
	 while( capture ) 
	 { 
		 frame = cvQueryFrame( capture ); 
		 if( !frame ) 
		 { 
			 fprintf( stderr, "Cannot query frame!\n" ); 
			 break; 
		 } 
		
		 detectFaces( frame ); 
                 detectFacesProfile( frame ); 
                 cvShowImage( "video", frame );
                 key = cvWaitKey( 25 ); 
	 } 
	 
	 cvReleaseCapture( &capture ); 
	 cvDestroyWindow( "video" ); 
	 
	 cvReleaseHaarClassifierCascade( &cascade ); 
         cvReleaseHaarClassifierCascade( &cascade_profile ); 
	 cvReleaseMemStorage( &storage ); 
	 return 0; 
 } 
 
 void detectFaces( IplImage *img ) 
 { 
	 int i; 
	 CvSeq *faces = cvHaarDetectObjects(img,cascade,storage,1.15,3,0,cvSize( 20, 20 ) ); 
	 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 ); 
	 } 

	 cvClearMemStorage( storage );
 }

void detectFacesProfile( IplImage *img ) 
 { 
	 int i; 
	 CvSeq *faces = cvHaarDetectObjects(img,cascade_profile,storage,1.15,3,0,cvSize( 20, 20 ) ); 
	 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 ); 
	 } 

	 cvClearMemStorage( storage );
 }


private Bitmap DetectFace(Bitmap faceImage)
        {
            var image = new Image<Bgr, byte>(faceImage);
            var gray = image.Convert<Gray, Byte>();
            var haarCascadeFilePath = _httpContext.Server.MapPath("~/haarcascade_profileface.xml");
            var face = new CascadeClassifier(haarCascadeFilePath);

            Rectangle[] facesDetected = face.DetectMultiScale(gray, 1.1, 2 , new Size(20,20));
            Total_faces = facesDetected.Length;
            Image<Gray, byte> result = null;
            if (Total_faces == 0)
            {
                var haarCascadeFilePath_tree = _httpContext.Server.MapPath("~/haarcascade_frontalface_alt_tree.xml");
                var face_tree = new CascadeClassifier(haarCascadeFilePath_tree);
                Rectangle[] facesDetected_tree = face_tree.DetectMultiScale(gray, 1.1, 2, new Size(20, 20));
                Total_faces = facesDetected_tree.Length;
                if (Total_faces == 0)
                {

                }else { 
                image.Draw(facesDetected_tree[0], new Bgr(Color.Blue), 2);
                result = image.Copy(facesDetected_tree[0]).Convert<Gray, byte>();

                if (result != null)
                {
                    result = result.Resize(100, 100, Inter.Cubic);

                    return result.Bitmap;
                }
                }
            }
            else
            {
                image.Draw(facesDetected[0], new Bgr(Color.Blue), 2);
                result = image.Copy(facesDetected[0]).Convert<Gray, byte>();

                if (result != null)
                {
                    result = result.Resize(100, 100, Inter.Cubic);

                    return result.Bitmap;
                }
            }
            

            return null;
        }


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆