3月 162013
 

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

人脸识别,读入一张图片,检测人脸,当然也可以通过更换xml文件来检测眼睛,嘴巴等。
其中可以把检测图片这段代码注释,把中间的opencvframegrabber的注释去了,可以实时检测视频中的人脸,我的电脑貌似有点慢。
当然也可以用摄像头实时检测
参考
http://opencvlover.blogspot.com/2012/11/face-detection-in-javacv-using-haar.html

好吧这篇文章被墙了。

可能会提示CV_HAAR_DO_CANNY_PRUNING找不到
import这个即可。
import static com.googlecode.javacv.cpp.opencv_objdetect.*;
cvHaarDetectObjects参数调整:
调节程序中的缺省参数(scale_factor=1.1, min_neighbors=3, flags=0)用于对目标进行更精确同时也是耗时较长的进一步检测。为了能对视频图像进行更快的实时检测,参数设置通常是:scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING, min_size= (例如, 对于视频会议的图像区域)

public class test {  
	static CvMemStorage storage;
	static CvHaarClassifierCascade cascade;
	static String XML_FILE = "haarcascade_frontalface_default.xml";
        public static void main(String args[]) throws Exception{ 
	/* 视频人脸检测
	IplImage pFrame=null;
	OpenCVFrameGrabber grabber = new OpenCVFrameGrabber("infile2.wmv");
	grabber.start();
	//FrameGrabber grabber = FrameGrabber.createDefault(0);  //摄像头人脸检测
        //grabber.start();
        pFrame = grabber.grab();
        while( pFrame!=null )
        {
    	detect( pFrame );
        //cvReleaseImage( pFrame );
        pFrame = grabber.grab();
        if( cvWaitKey( 10 ) >= 0 )
            break;
    }
   */
	IplImage img = cvLoadImage("xinsrc_1320805282048718186931.jpg");
	detect( img );

    }

    public static void detect(IplImage src){

		CvHaarClassifierCascade cascade = new 
				CvHaarClassifierCascade(cvLoad(XML_FILE));
		CvMemStorage storage = CvMemStorage.create();
		CvSeq sign = cvHaarDetectObjects(
				src,
				cascade,
				storage,
				1.2,
				2,
				CV_HAAR_DO_CANNY_PRUNING);

		cvClearMemStorage(storage);

		int total_Faces = sign.total();		
                if(total_Faces==0)
        	return;
		for(int i = 0; i < total_Faces; i++){
			CvRect r = new CvRect(cvGetSeqElem(sign, i));
			cvRectangle (   src,
				        cvPoint(r.x(), r.y()),
					cvPoint(r.width() + r.x(), r.height() + r.y()),
					CvScalar.RED,
					2,
					CV_AA,
					0);

		}
		cvShowImage("Result", src);
		cvWaitKey( 0 );
	}
}

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)