C++ 如何使用 OpenCV 进行人脸识别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7949494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:46:15  来源:igfitidea点击:

How to do Face Recognition using OpenCV?

c++opencvface-recognition

提问by Raman Sharma

I am trying to do some Face Recognition (not detection) stuff using OpenCV. I found this article with some code:

我正在尝试使用 OpenCV 做一些人脸识别(不是检测)的东西。我发现这篇文章有一些代码:

http://www.cognotics.com/opencv/servo_2007_series/index.html

http://www.cognotics.com/opencv/servo_2007_series/index.html

However, this code is written using the older C-style OpenCV API. Does someone have a C++ API version of this using a more recent version like OpenCV 2.3.1?

但是,此代码是使用较旧的 C 样式 OpenCV API 编写的。有人使用像 OpenCV 2.3.1 这样的更新版本有 C++ API 版本吗?

回答by bytefish

Update: OpenCV 2.4.2 now comes with the very new cv::FaceRecognizer. Please see the very detailed documentation at:

更新:OpenCV 2.4.2 现在带有非常新的cv::FaceRecognizer。请参阅非常详细的文档:

I have released libfacerec, a modern face recognition library for the OpenCV C++ API (BSD license). libfacerec has no additional dependencies and implements the Eigenfaces method, Fisherfaces method and Local Binary Patterns Histograms. Parts of the library are going to be included in OpenCV 2.4.

我已经发布了libfacerec,一个用于 OpenCV C++ API(BSD 许可证)的现代人脸识别库。libfacerec 没有额外的依赖项,并实现了 Eigenfaces 方法、Fisherfaces 方法和 Local Binary Patterns Histograms。部分库将包含在 OpenCV 2.4 中。

The latest revision of the libfacerec is available at:

libfacerec 的最新版本可从以下网址获得:

The library was written for OpenCV 2.3.1 with the upcoming OpenCV 2.4 in mind, so I don't support OpenCV versions earlier than 2.3.1. This project comes as a CMake project with a well-documented API, there's also a tutorial on gender classification. You can see a HTML version of the documentation at:

该库是为 OpenCV 2.3.1 编写的,考虑到即将推出的 OpenCV 2.4,所以我不支持早于 2.3.1 的 OpenCV 版本。这个项目是一个 CMake 项目,带有一个记录良好的 API,还有一个关于性别分类的教程。您可以在以下位置查看文档的 HTML 版本:

If you want to understand how those algorithms work, you might want to read my Guide To Face Recognition (includes Python and GNU Octave/MATLAB examples):

如果您想了解这些算法的工作原理,您可能需要阅读我的人脸识别指南(包括 Python 和 GNU Octave/MATLAB 示例):

The relevant publications are:

相关刊物有:

  • Turk, M., and Pentland, A. Eigenfaces for recognition.. Journal of Cognitive Neuroscience 3 (1991), 71–86.
  • Belhumeur, P. N., Hespanha, J., and Kriegman, D. Eigenfaces vs. Fisherfaces: Recognition using class specific linear projection.. IEEE Transactions on Pattern Analysis and Machine Intelligence 19, 7 (1997), 711–720.
  • Ahonen, T., Hadid, A., and Pietikainen, M. Face Recognition with Local Binary Patterns.. Computer Vision - ECCV 2004 (2004), 469–481.
  • Turk, M. 和 Pentland, A. Eigenfaces 用于识别。. 认知神经科学杂志 3(1991),71-86。
  • Belhumeur, PN, Hespanha, J. 和 Kriegman, D. Eigenfaces vs. Fisherfaces:使用类特定线性投影进行识别。. IEEE Transactions on Pattern Analysis and Machine Intelligence 19, 7 (1997), 711–720。
  • Ahonen, T.、Hadid, A. 和 Pietikainen, M.使用局部二进制模式进行人脸识别。. 计算机视觉 - ECCV 2004 (2004), 469–481。

回答by morynicz

I'm doing a face recognition project for my engineer's degree, using c++ api. I think that everything regarding face recognition in c++ is fairly straightforward, even simpler than in C (less pointers). To use PCA you have a class named PCA described here. Just use the proper methods and read documentation with understanding. To build the matrix with input data I've created a matrix of proper size, then pasted pictures as rows (use method reshape) into it (there is a method in cv::Mat that lets you to get easily a row of a matrix). You just need to keep sure that base data and tested data have the same parameters (channels, size,etc.)

我正在为我的工程师学位做一个人脸识别项目,使用 c++ api。我认为 C++ 中关于人脸识别的一切都相当简单,甚至比 C 中的更简单(更少的指针)。要使用 PCA,您需要在此处描述一个名为 PCA 的类。只需使用正确的方法并在理解的情况下阅读文档。为了使用输入数据构建矩阵,我创建了一个适当大小的矩阵,然后将图片作为行粘贴(使用 reshape 方法)到其中(cv::Mat 中有一种方法可以让您轻松获得矩阵的一行)。您只需要确保基础数据和测试数据具有相同的参数(通道、大小等)

EDIT:

编辑:

     using namespace cv; //somewhere near top

inserting data to data matrix:

将数据插入数据矩阵:

    62       Mat reshaped=img.reshape(1,1);
    63       Mat dataRow=_data.row(y++);
    64       resize(reshaped,dataRow,dataRow.size(),0,0,CV_INTER_LINEAR);

computing pca:

计算PCA:

    251 _pca(_data,Mat(),CV_PCA_DATA_AS_ROW); //compute pca 
    252 _pca.project(_data,_vectors); // project original data to new coordinates

As opencv's documentation isn't the best out there, it doesn't hurt to spend some time reading it. Most of the c api functions have their equivalents in c++ api, You only need to do some "write into search window and hit enter" searching. And, there are also tutorials in c++ to get a grip of the c++ api.

由于 opencv 的文档并不是最好的,所以花一些时间阅读它并没有什么坏处。大多数 c api 函数在 c++ api 中有它们的等价物,您只需要执行一些“写入搜索窗口并按回车键”搜索。而且,也有 C++ 教程来掌握 C++ api。