遇到opencv
(當然據說目前挺火的 綠壩 用的就是這個)
opencv是個圖形函數庫
由一系列 C 函數和少量 C++ 類構成
OpenCV 對非商業應用和商業應用都是免費(FREE)的
相關網站
下載下來後
有些情況
windows下
有個face檢測的程序有意思
可以檢測人臉
直接調用人臉檢測函數
人臉檢測時
#include
#include
#include <stdio
#include <stdlib
#include <string
#include <assert
#include <math
#include <float
#include <limits
#include <time
#include <ctype
#ifdef _EiC
#define WIN
#endif
static CvMemStorage* storage =
static CvHaarClassifierCascade* cascade =
static CvHaarClassifierCascade* nested_cascade =
int use_nested_cascade =
void detect_and_draw( IplImage* image );
const char* cascade_name =
//
/*
const char* nested_cascade_name =
//
//
double scale =
int main( int argc
{
CvCapture* capture =
IplImage *frame
IplImage *image =
const char* scale_opt =
int scale_opt_len = (int)strlen(scale_opt);
const char* cascade_opt =
int cascade_opt_len = (int)strlen(cascade_opt);
const char* nested_cascade_opt =
int nested_cascade_opt_len = (int)strlen(nested_cascade_opt);
int i;
const char* input_name =
input_name = argv[
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name
if( !cascade )
{
fprintf( stderr
fprintf( stderr
return
}
if( !input_name || (isdigit(input_name[
capture = cvCaptureFromCAM( !input_name ?
else if( input_name )
{
storage = cvCreateMemStorage(
if( !image )
capture = cvCaptureFromAVI( input_name );
}
else
image = cvLoadImage(
cvNamedWindow(
if( capture )
{
for(;;)
{
if( !cvGrabFrame( capture ))
break;
frame = cvRetrieveFrame( capture );
if( !frame )
break;
if( !frame_copy )
frame_copy = cvCreateImage( cvSize(frame
IPL_DEPTH_
if( frame
cvCopy( frame
else
cvFlip( frame
detect_and_draw( frame_copy );
if( cvWaitKey(
goto _cleanup_;
}
cvWaitKey(
_cleanup_:
cvReleaseImage( &frame_copy );
cvReleaseCapture( &capture );
}
else
{
if( image )
{
detect_and_draw( image );
cvShowImage(
cvWaitKey(
cvReleaseImage( &image );
}
else if( input_name )
{
/* assume it is a text file containing the
list of the image filenames to be processed
FILE* f = fopen( input_name
if( f )
{
char buf[
while( fgets( buf
{
int len = (int)strlen(buf)
while( len >
len
buf[len] =
printf(
image = cvLoadImage( buf
if( image )
{
detect_and_draw( image );
c = cvWaitKey(
if( c ==
break;
cvReleaseImage( &image );
}
}
fclose(f);
}
}
}
cvDestroyWindow(
return
}
void detect_and_draw( IplImage* img )
{
static CvScalar colors[] =
{
{{
{{
{{
{{
{{
{{
{{
{{
};
IplImage *gray
int i
gray = cvCreateImage( cvSize(img
small_img = cvCreateImage( cvSize( cvRound (img
cvRound (img
cvCvtColor( img
cvResize( gray
cvEqualizeHist( small_img
cvClearmemStorage( storage );
if( cascade )
{
double t = (double)cvGetTickCount();
CvSeq* faces = cvHaarDetectObjects( small_img
//|CV_HAAR_FIND_BIGGEST_OBJECT
//|CV_HAAR_DO_ROUGH_SEARCH
|CV_HAAR_DO_CANNY_PRUNING
//|CV_HAAR_SCALE_IMAGE
cvSize(
t = (double)cvGetTickCount()
printf(
for( i =
{
CvRect* r = (CvRect*)cvGetSeqElem( faces
CvMat small_img_roi;
CvSeq* nested_objects;
CvPoint center;
CvScalar color = colors[i%
int radius;
center
center
radius = cvRound((r
cvCircle( img
if( !nested_cascade )
continue;
cvGetSubRect( small_img
nested_objects = cvHaarDetectObjects( &small_img_roi
//|CV_HAAR_FIND_BIGGEST_OBJECT
//|CV_HAAR_DO_ROUGH_SEARCH
//|CV_HAAR_DO_CANNY_PRUNING
//|CV_HAAR_SCALE_IMAGE
cvSize(
for( j =
{
CvRect* nr = (CvRect*)cvGetSeqElem( nested_objects
center
center
radius = cvRound((nr
cvCircle( img
}
}
}
cvShowImage(
cvReleaseImage( &gray );
cvReleaseImage( &small_img );
}
相關數據
haarcascade_frontalface_alt
是人臉檢測用的人的眼睛
當然可以改名或使用你自己的
java使用opencv
用JNI
FaceDetection
class JNIOpenCV {
static {
System
}
public native int[] detectFace(int minFaceWidth
}
public class FaceDetection {
private JNIOpenCV myJNIOpenCV;
private FaceDetection myFaceDetection;
public FaceDetection() {
myJNIOpenCV = new JNIOpenCV();
String filename =
String cascade =
int[] detectedFaces = myJNIOpenCV
int numFaces = detectedFaces
System
for (int i =
System
}
}
public static void main(String args[]) {
FaceDetection myFaceDetection = new FaceDetection();
}
}
就可以在java中實現人臉識別了
第一副圖是c語言的人臉識別效果
第二幅圖是java版的效果
相關文件備份
有運行不了的情況
題外話
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26235.html