OpenCV笔记--人脸识别算法Eigenfaces和Fisherfaces
创始人
2024-01-20 05:20:45
0

目录

1--前言

2--处理ORL数据集

3--Eigenfaces复现过程

4--Fisherfaces复现过程

5--分析


1--前言

①SYSU模式识别课程作业

②配置:基于Windows11、OpenCV4.5.5、VSCode、CMake(参考OpenCV配置方式)

③原理及源码介绍:Face Recognition with OpenCV

④数据集:ORL Database of Faces

2--处理ORL数据集

①源码:

import sys
import os.pathif __name__ == "__main__":BASE_PATH = './ORL/att_faces/orl_faces/'SEPARATOR = ";"dir_txt = open("./dir.txt", 'w')label = 0for dirname, dirnames, filenames in os.walk(BASE_PATH):# dirname当前路径; dirnames当前路径下所有目录名(不包含子目录);filenames当前路径下的所有文件名(不包含子目录)for subdirname in dirnames: # 遍历每一个目录subject_path = os.path.join(dirname, subdirname)for filename in os.listdir(subject_path):abs_path = "%s/%s" % (subject_path, filename)print("%s%s%d" % (abs_path, SEPARATOR, label))dir_txt.write(abs_path)dir_txt.write(SEPARATOR)dir_txt.write(str(label))dir_txt.write("\n")label = label + 1dir_txt.close()

②运行及结果:

python create_csv.py

3--Eigenfaces复现过程

①源码:

// 引用依赖
#include "opencv2/core.hpp"
#include "opencv2/face.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include 
#include 
#include // 使用相应的命名空间
using namespace cv;
using namespace cv::face;
using namespace std;// 标准化函数
static Mat norm_0_255(InputArray _src) {Mat src = _src.getMat();// Create and return normalized image:Mat dst;switch(src.channels()) {case 1:cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);break;case 3:cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);break;default:src.copyTo(dst);break;}return dst;
}// 读取CSV文件函数
static void read_csv(const string& filename, vector& images, vector& labels, char separator = ';') {std::ifstream file(filename.c_str(), ifstream::in);if (!file) {string error_message = "No valid input file was given, please check the given filename.";CV_Error(Error::StsBadArg, error_message);}string line, path, classlabel;while (getline(file, line)) {stringstream liness(line);getline(liness, path, separator);getline(liness, classlabel);if(!path.empty() && !classlabel.empty()) {images.push_back(imread(path, 0));labels.push_back(atoi(classlabel.c_str()));}}
}
int main(int argc, const char *argv[]) {//检查argc是否符合要求if (argc < 2) {cout << "usage: " << argv[0] << "   " << endl;exit(1);}string output_folder = ".";if (argc == 3) {output_folder = string(argv[2]);}// CSV文件的路径string fn_csv = string(argv[1]);// 初始化存储imgs和labels的向量vector images;vector labels;// 读取CSV文件try {read_csv(fn_csv, images, labels);} catch (const cv::Exception& e) {cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;exit(1);}// 判断img数目是否符合要求if(images.size() <= 1) {string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";CV_Error(Error::StsError, error_message);}// images的高度int height = images[0].rows;// 从训练集中选择一张图片作为测试集Mat testSample = images[images.size() - 1];int testLabel = labels[labels.size() - 1];images.pop_back();labels.pop_back();// 创建模型,使用PCA特征脸算法Ptr model = EigenFaceRecognizer::create();model->train(images, labels); // 训练模型int predictedLabel = model->predict(testSample);  // 使用测试集测试模型// 打印准确率string result_message = format("Predicted class = %d / Actual class = %d.", predictedLabel, testLabel);cout << result_message << endl;// 获取模型的特征值Mat eigenvalues = model->getEigenValues();// 展示特征向量Mat W = model->getEigenVectors();// 从训练集中获取样本均值Mat mean = model->getMean();// 根据argc判断进行展示或保存操作if(argc == 2) {imshow("mean", norm_0_255(mean.reshape(1, images[0].rows)));} else {imwrite(format("%s/mean.png", output_folder.c_str()), norm_0_255(mean.reshape(1, images[0].rows)));}// 显示或保存特征脸for (int i = 0; i < min(10, W.cols); i++) {string msg = format("Eigenvalue #%d = %.5f", i, eigenvalues.at(i));cout << msg << endl;// 获取特征向量Mat ev = W.col(i).clone();// resize成原始大小,并归一化到0-255Mat grayscale = norm_0_255(ev.reshape(1, height));// 显示图像并应用Jet颜色图以获得更好的观感。Mat cgrayscale;applyColorMap(grayscale, cgrayscale, COLORMAP_JET);// 根据argc判断进行展示或保存操作if(argc == 2) {imshow(format("eigenface_%d", i), cgrayscale);} else {imwrite(format("%s/eigenface_%d.png", output_folder.c_str(), i), norm_0_255(cgrayscale));}}// 在一些预定义的步骤中显示或保存图像重建的过程:for(int num_components = min(W.cols, 10); num_components < min(W.cols, 300); num_components+=15) {// 从模型中分割特征向量Mat evs = Mat(W, Range::all(), Range(0, num_components));Mat projection = LDA::subspaceProject(evs, mean, images[0].reshape(1,1));Mat reconstruction = LDA::subspaceReconstruct(evs, mean, projection);// 归一化reconstruction = norm_0_255(reconstruction.reshape(1, images[0].rows));// 根据argc判断进行展示或保存操作if(argc == 2) {imshow(format("eigenface_reconstruction_%d", num_components), reconstruction);} else {imwrite(format("%s/eigenface_reconstruction_%d.png", output_folder.c_str(), num_components), reconstruction);}}// 如果没有写入输出文件夹,则等待键盘输入if(argc == 2) {waitKey(0);}return 0;
}

②编译过程:

CMakeLists.txt如下:

cmake_minimum_required(VERSION 3.24)  # 指定 cmake的 最小版本
project(test) # 设置项目名称find_package(Opencv REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
add_executable(eigenfaces_demo eigenfaces.cpp) # 生成可执行文件
target_link_libraries(eigenfaces_demo ${OpenCV_LIBS} ) # 设置target需要链接的库
mkdir buildcd buildcmake ..cd ..mingw32-make

③运行及结果展示:

./eigenfaces_demo.exe ./dir.txt ./Engenfaces_Result

特征图:(简单修改源程序生成的文件名,再按顺序进行拼接即可生成拼接图,拼接程序参考)

重建过程:

均值图:

4--Fisherfaces复现过程

①源码:

// 引用依赖
#include "opencv2/core.hpp"
#include "opencv2/face.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include 
#include 
#include // 使用相应的命名空间
using namespace cv;
using namespace cv::face;
using namespace std;// 标准化函数
static Mat norm_0_255(InputArray _src) {Mat src = _src.getMat();Mat dst;switch(src.channels()) {case 1:cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);break;case 3:cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);break;default:src.copyTo(dst);break;}return dst;
}// 读取csv文件函数
static void read_csv(const string& filename, vector& images, vector& labels, char separator = ';') {std::ifstream file(filename.c_str(), ifstream::in);if (!file) {string error_message = "No valid input file was given, please check the given filename.";CV_Error(Error::StsBadArg, error_message);}string line, path, classlabel;while (getline(file, line)) {stringstream liness(line);getline(liness, path, separator);getline(liness, classlabel);if(!path.empty() && !classlabel.empty()) {images.push_back(imread(path, 0));labels.push_back(atoi(classlabel.c_str()));}}
}int main(int argc, const char *argv[]) {//检查argc是否符合要求if (argc < 2) {cout << "usage: " << argv[0] << "   " << endl;exit(1);}string output_folder = ".";if (argc == 3) {output_folder = string(argv[2]);}// CSV文件的路径string fn_csv = string(argv[1]);// 初始化存储imgs和labels的向量vector images;vector labels;// 读取CSV文件try {read_csv(fn_csv, images, labels);} catch (const cv::Exception& e) {cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;exit(1);}// 判断img数目是否符合要求if(images.size() <= 1) {string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";CV_Error(Error::StsError, error_message);}// images的高度int height = images[0].rows;// 从训练集中选择一张图片作为测试集Mat testSample = images[images.size() - 1];int testLabel = labels[labels.size() - 1];images.pop_back();labels.pop_back();// 创建模型,使用LDA线性判别分析Ptr model = FisherFaceRecognizer::create();model->train(images, labels); // 训练模型int predictedLabel = model->predict(testSample); // 使用测试集测试模型// 打印准确率string result_message = format("Predicted class = %d / Actual class = %d.", predictedLabel, testLabel);cout << result_message << endl;// 获取模型的特征值Mat eigenvalues = model->getEigenValues();// 展示特征向量Mat W = model->getEigenVectors();// 从训练集中获取样本均值Mat mean = model->getMean();// 根据argc判断进行展示或保存操作if(argc == 2) {imshow("mean", norm_0_255(mean.reshape(1, images[0].rows)));} else {imwrite(format("%s/mean.png", output_folder.c_str()), norm_0_255(mean.reshape(1, images[0].rows)));}// 显示或保存特征脸for (int i = 0; i < min(16, W.cols); i++) {string msg = format("Eigenvalue #%d = %.5f", i, eigenvalues.at(i));cout << msg << endl;// 获取特征向量Mat ev = W.col(i).clone();// resize成原始大小,并归一化到0-255Mat grayscale = norm_0_255(ev.reshape(1, height));// 显示图像并应用Jet颜色图以获得更好的观感。Mat cgrayscale;applyColorMap(grayscale, cgrayscale, COLORMAP_BONE);// 根据argc判断进行展示或保存操作if(argc == 2) {imshow(format("fisherface_%d", i), cgrayscale);} else {imwrite(format("%s/fisherface_%d.png", output_folder.c_str(), i), norm_0_255(cgrayscale));}}// 在一些预定义的步骤中显示或保存图像重建的过程:for(int num_component = 0; num_component < min(16, W.cols); num_component++) {// 从模型中分割特征向量Mat ev = W.col(num_component);Mat projection = LDA::subspaceProject(ev, mean, images[0].reshape(1,1));Mat reconstruction = LDA::subspaceReconstruct(ev, mean, projection);// 归一化reconstruction = norm_0_255(reconstruction.reshape(1, images[0].rows));// 根据argc判断进行展示或保存操作if(argc == 2) {imshow(format("fisherface_reconstruction_%d", num_component), reconstruction);} else {imwrite(format("%s/fisherface_reconstruction_%d.png", output_folder.c_str(), num_component), reconstruction);}}// 如果没有写入输出文件夹,则等待键盘输入if(argc == 2) {waitKey(0);}return 0;
}

②编译过程:

CMakeLists.txt如下:

cmake_minimum_required(VERSION 3.24)  # 指定 cmake的 最小版本
project(test) # 设置项目名称find_package(Opencv REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
#add_executable(eigenfaces_demo eigenfaces.cpp) # 生成可执行文件
#target_link_libraries(eigenfaces_demo ${OpenCV_LIBS} ) # 设置target需要链接的库
add_executable(fisherfaces_demo fisherfaces.cpp) # 生成可执行文件
target_link_libraries(fisherfaces_demo ${OpenCV_LIBS} ) # 设置target需要链接的库
mkdir buildcd buildcmake ..cd ..mingw32-make

③运行及结果展示:

./fisherfaces_demo.exe ./dir.txt ./Fisherfaces_Result

特征图:

重建过程:

均值图:

5--分析

未完待续!

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...