// 加载必要的头文件
#include <iostream> // 用于标准输入输出流
#include <fstream> // 用于文件的输入输出
#include <sstream> // 用于字符串的输入输出流操作
#include <opencv2/core.hpp> // OpenCV核心功能的头文件
#include "opencv2/imgcodecs.hpp" // OpenCV图像编解码功能的头文件
#include <opencv2/highgui.hpp> // OpenCV的高级GUI(图形用户界面)
// 使用标准命名空间和OpenCV命名空间,避免重复声明
using namespace cv;
using namespace std;
///
// 函数声明部分
// read_imgList函数用于从文本文件中读取图像路径并加载这些图像
static void read_imgList(const string& filename, vector<Mat>& images) {
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; // 存储读取的每行文字
while (getline(file, line)) {
images.push_back(imread(line, IMREAD_GRAYSCALE)); // 将每行读取到的图像路径用于加载图像,并转换为灰度图像
}
}
// formatImagesForPCA函数用于将图像数据格式化为一个适合PCA处理的矩阵
static Mat formatImagesForPCA(const vector<Mat> &data)
{
// 创建一个用于PCA处理的矩阵,将所有图像行向量垂直堆叠
Mat dst(static_cast<int>(data.size()), data[0].rows*data
08-19
03-19
5304
