随着深度学习的发展,opencv3.1也可以直接调用caffe或者torch。下面是使用opencv的dnn模块来进行人脸识别:
1:编译opencv3.1
首先下载opencv源码https://github.com/opencv/opencv
下载Cmake https://cmake.org/download/
下载opencv的
具体的camke过程可以参考这篇博客:
http://www.cnblogs.com/jliangqiu2016/p/5597501.html
编译完成后可以把不需要的文件删除仅保留include bin lib 文件即可。
编译好的opencv3.1和普通opencv的配置过程一样:
opencv_aruco310.lib
opencv_bgsegm310.lib
opencv_bioinspired310.lib
opencv_calib3d310.lib
opencv_ccalib310.lib
opencv_core310.lib
opencv_cudaarithm310.lib
opencv_cudabgsegm310.lib
opencv_cudacodec310.lib
opencv_cudafeatures2d310.lib
opencv_cudafilters310.lib
opencv_cudaimgproc310.lib
opencv_cudalegacy310.lib
opencv_cudaobjdetect310.lib
opencv_cudaoptflow310.lib
opencv_cudastereo310.lib
opencv_cudawarping310.lib
opencv_cudev310.lib
opencv_datasets310.lib
opencv_dnn310.lib
opencv_dpm310.lib
opencv_face310.lib
opencv_features2d310.lib
opencv_flann310.lib
opencv_fuzzy310.lib
opencv_highgui310.lib
opencv_imgcodecs310.lib
opencv_imgproc310.lib
opencv_line_descriptor310.lib
opencv_ml310.lib
opencv_objdetect310.lib
opencv_optflow310.lib
opencv_photo310.lib
opencv_plot310.lib
opencv_reg310.lib
opencv_rgbd310.lib
opencv_saliency310.lib
opencv_shape310.lib
opencv_stereo310.lib
opencv_stitching310.lib
opencv_structured_light310.lib
opencv_superres310.lib
opencv_surface_matching310.lib
opencv_text310.lib
opencv_tracking310.lib
opencv_ts310.lib
opencv_video310.lib
opencv_videoio310.lib
opencv_videostab310.lib
opencv_viz310.lib
opencv_xfeatures2d310.lib
opencv_ximgproc310.lib
opencv_xobjdetect310.lib
opencv_xphoto310.lib
在opencv的源码中提供了dnn的test.cpp
下面具体分析代码:
/* Find best class for the blob (i. e. class with maximal probability) */
获取prob层的输出:实际意义为测试图片所对应与标签的概率值。resize成一个列向量,然后排序,输出最大值和最大值所对应的位置。
void getMaxClass(dnn::Blob &probBlob, int *classId, double *classProb)
{
Mat probMat = probBlob.matRefConst().reshape(1, 1); //reshape the blob to 1x1000 matrix
Point classNumber;
minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);
*classId = classNumber.x;
}
相关系数函数:一种相似性度量用于判断两个人的相似性距离。
float mean(const std::vector<float>& v)
{
assert(v.size() != 0);
float ret = 0.0;
for (std::vector<float>::size_type i = 0; i != v.size(); ++i)
{
ret += v[i];
}
return ret / v.size();
}
float cov(const std::vector<float>& v1, const std::vector<float>& v2)
{
assert(v1.size() == v2.size() && v1.size() > 1);
float ret = 0.0;
float v1a = mean(v1), v2a = mean(v2);
for (