opencv在android预览上实现人脸识别(4)
2015-01-25 11:08
330 查看
简介
现在,继续进行opencv的学习ing。本章中,学习使用opencv提供的库来实现人脸识别的功能。
实现步骤
函数介绍
这里是使用的opencv库实现,主要用到两个方面: 1、opencv函数:detectMultiScale 2、haarcascade_frontalface_alt.xml 函数detectMultiScale在opencv官网上有详细资料讲解。 haarcascade_frontalface_alt.xml在从opencv里面下载的压缩包里可以找到,路径为:opencv-2.4.9\opencv-2.4.9\data\haarcascades
代码实现
代码的实现,也是在之前讲到的opencv在android上预览的那版最小代码上,用jni来实现的。这里只贴出jni中的代码,java的代码,都一样的。
#include <PreviceGray.h> #include <opencv2/core/core.hpp> #include <opencv2/core/mat.hpp> #include <opencv/cv.h> #include <string> #include <vector> #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <android/log.h> using namespace cv; using namespace std; void detectAndDisplay(Mat *frame); String face_cascade_name = "/sdcard/haarcascade_frontalface_alt.xml"; CascadeClassifier face_cascade; #define LOG_TAG "show infomation" void detectAndDisplay(Mat *frame) { std::vector<Rect> faces; Mat frame_gray; cvtColor( *frame, frame_gray, COLOR_BGR2GRAY ); equalizeHist( frame_gray, frame_gray ); face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) ); for ( size_t i = 0; i < faces.size(); i++ ) { Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 ); ellipse(*frame, center, Size( faces[i].width/2, faces[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 ); } } JNIEXPORT void Java_com_example_camera_1opencv_1android_PreviceGray_grayProc(JNIEnv* env, jclass obj, jlong imageGray) { int i; Mat mat = Mat(*((Mat*)imageGray)); if(!face_cascade.load(face_cascade_name)){ __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,"--(!)Error loading face cascade"); } detectAndDisplay(&mat); }
注意,在代码实现中,一定要先将haarcascade_frontalface_alt.xml复制到设备对应的/sdcard/haarcascade_frontalface_alt.xml,同时加入SD的读写操作权限。 在代码中,首先是face_cascade.load来加载haarcascade_frontalface_alt.xml,接着使用cvtColor将图像变成灰阶图像。 再然后equalizeHist来做直方图均衡化,直方图均衡化,可以将比较淡的图像变换为比较深的图像(即增强图像的亮度及对比度), 之后,使用face_cascade.detectMultiScale来判断获取图像中人脸信息的坐标。 最后在人脸中间坐标处画,根据脸部大小画一个圆作为标记。最后在预览图像中,就会有一个圆圈中识别到的人脸信息。
参考代码:http://download.csdn.net/detail/u011630458/8403763
相关文章推荐
- opencv在android预览上实现灰化/感应触屏/边缘检测(3)
- android 通过Camera和Surfaceview实现照相 图片预览 保存图片到本地 手动聚焦 FaceDetectionListener实现人脸识别
- android opencv 人脸检测实现
- Android 实现仿Window7图片预览窗格效果
- [Android编程心得] Camera(OpenCV)自动对焦和触摸对焦的实现
- OpenCV4Android实现图像二值化
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之游戏效果预览(一)
- {Android-OpenCV调用CvSmooth实现高斯模糊}
- opencv在android上预览的最小代码模块(2)
- 实现Android图片预览的拖拽缩放
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之游戏效果预览(一)
- Android上使调用OpenCV 2.4.10 实现二维码区域定位(Z-xing 码)
- android实现图片预览
- Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整实现(原理:底层SurfaceView+上层绘制ImageView)
- Android开发 摄像头SurfaceView预览 背景带矩形框 实现(原理:双surfaceview,顶层画矩形框,底层预览视频)
- Android相机开发(一)之预览拍照查看图片基本功能的实现
- Android开发:巧用延迟实现欢迎界面 及 摄像头预览后绘制背景标示(如坐标系、矩形框)
- 我的Android进阶之旅------>Android之Gallery和GridView两种方式与ImageSwitcher实现带预览的和幻灯片方式的两种图片浏览器
- Android OpenCV实现图片叠加,水印
- Android利用Gallery和ImageSwitcher实现在线相册图片预览功能(异步加载图片)