这个系列的目的是通过对OpenCV示例,进一步了解OpenCV函数的使用,不涉及具体原理。
目录
简介
Example运行截图
Example分析
Example代码
简介
本文记录了对OpenCV示例
pca
.cpp
的分析。
这个示例主要演示了如何使用OpenCV完成PCA降维操作
。
PCA(Principal Component Analysis)是一种常用的数据分析方法。PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维数据的降维。本文主要讨论OpenCV的示例
pca.cpp,不涉及具体数学原理,如果读者想进一步了解PCA原理可以参考
PCA的数学原理。
示例涉及PCA类构造函数,
project函数,backProject函数,和Mat类
reshape函数,convertTo函数
的使用
。
PCA构造函数
PCA类是OpenCV实现主要成分分析的类,在人脸识别等机器学习的项目中大量应用,使用前需要先实例化对象。
函数原型:
PCA(InputArray data, InputArray mean, int flags, int maxComponents = 0);
PCA(InputArray data, InputArray mean, int flags, double retainedVariance);
参数说明:
data:需要PCA的数据,每一行(列)表示一个样本;
mean:
平均值;如果矩阵是空的(noArray()),则从数据计算;
flags:
操作标志,具体参数如下:
DATA_AS_ROW :每一行表示一个样本;
DATA_AS_COL :每一列表示一个样本;
maxComponents :
PCA应保留的最大组件数;默认情况下,所有组件都保留;
retainedVariance:PCA应保留的方差百分比。使用这个参数将让PCA决定保留多少组件,但它将始终保持至少2。
|
PCA::project函数
该函数的作用是将输入数据vec(该数据是用来提取PCA特征的原始数据)投影到PCA主成分空间中去,返回每一个样本主成分特征组成的矩阵。因为经过PCA处理后,原始数据的维数降低了,因此原始数据集中的每一个样本的维数都变了,由改变后的样本集就组成了本函数的返回值。
函数原型:
Mat project(InputArray vec) const;
参数说明:
vec:参与投影(降维)的数据
PS:如果选择DATA_AS_ROW,每一行表示一个样本,则vec也需要按此
|
PCA::backProject函数
一般调用backProject()函数前需调用project()函数,因为backProject()函数的参数vec为经过PCA投影降维过后的矩阵。 因此backProject()函数的作用就是用vec来重构原始数据集(关于该函数的本质数学实现暂时还不是很了解)。
函数原型:
Mat backProject(InputArray vec) const;
参数说明:
vec:
参与反投影(反降维)的数据
|
Mat类是OpenCV 2.0以来主要的数据类型,之前在同系列博客
OpenCV3.0 Examples学习笔记(3)-cout_mat.cpp
中略有涉及,示例主要涉及
reshape函数,
convertTo函数
Mat::reshape函数
该函数会为当前矩阵创建一个新的矩阵头(指针),新的矩阵拥有不同的尺寸或者不同的通道数,其优点在于运算复杂度为O(1),不用复制矩阵数据.正是因为不用复制数据,所以在转变过程中要保证原数据矩阵在数据上的连续性(这里的连续性是相对于原矩阵来说)
函数原型:
Mat reshape(int cn, int rows=0) const;
参数说明:
cn:新的通道数;如果cn值为0表示变换前后通道数不变
rows:新的行数;如果rows值为0表示变换后矩阵的行数不变
|
Mat::convertTo函数
这个函数提供点算子(像素变换)能力,通过增益(
alpha
)和偏置(
beta
)参数对图像进行调整,我们也可以使用它完成亮度
(
beta
)
和对比度
(
alpha
)
的调整,其公式如下
函数原型: void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const; 参数说明: m:输出矩阵;如果在使用前没有声明或需要修改,会自行分配(需要赞美的能力!)。 rtype:新的矩阵类型。因此也有人使用这个函数进行类型转换 alpha:增益参数,对比度 beta:偏置参数,亮度 PS:convertTo属于Mat的成员函数。 PS2:saturate_cast用于防止溢出,结果小于0则转为0,大于255,则转为255。 |
示例涉需要通过命令行存入一个txt格式输入文件,其格式如下:
C:/Mysher/OpenCV/opencv310/opencv/sources/samples/data/att_faces/s1/1.pgm
C:/Mysher/OpenCV/opencv310/opencv/sources/samples/data/att_faces/s2/1.pgm
C:/Mysher/OpenCV/opencv310/opencv/sources/samples/data/att_faces/s3/1.pgm
C:/Mysher/OpenCV/opencv310/opencv/sources/samples/data/att_faces/s4/1.pgm
C:/Mysher/OpenCV/opencv310/opencv/sources/samples/data/att_faces/s5/1.pgm
C:/Mysher/OpenCV/opencv310/opencv/sources/samples/data/att_faces/s6/1.pgm
C:/Mysher/OpenCV/opencv310/opencv/sources/samples/data/att_faces/s7/1.pgm
C:/Mysher/OpenCV/opencv310/opencv/sources/samples/data/att_faces/s8/1.pgm
C:/Mysher/OpenCV/opencv310/opencv/sources/samples/data/att_faces/s9/1.pgm
C:/Mysher/OpenCV/opencv310/opencv/sources/samples/data/att_faces/s10/1.pgm
C:/Mysher/OpenCV/opencv310/opencv/sources/samples/data/att_faces/s11/1.pgm
|
Example运行截图
|
|
Example分析
0.全局函数和变量
0.1read_imgList函数,从txt文件中加载pgm格式图像
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, 0));
}
}
注意:
(2)getline函数,
从输入流中读入字符具体见参考资料8.《C++ getline的使用》
0.2formatImagesForPCA函数,将所有图像存入一个Mat中,每一行表示一个样本
static Mat formatImagesForPCA(const vector<Mat> &data)
{
Mat dst(static_cast<int>(data.size()), data[0].rows*data[0].cols, CV_32F);
for(unsigned int i = 0; i < data.size(); i++)
{
//Mat image_row = data[i].clone().reshape(1,1);
Mat image_row = data[i].clone().reshape(1,1);
Mat row_i = dst.row(i);
image_row.convertTo(row_i,CV_32F);
//Mat tmp(1,data[0].rows*data[0].cols, CV_32F);
//image_row.convertTo(tmp,CV_32F);
}
return dst;
}
注意:
(1)
Mat::
reshape,创建一个新的头,示例中将所有图像存入一个Mat,其每一行都包含一个完整的样本信息,此处通过reshape将图像的Mat转换为1行1列的向量;
(2)Mat::
convertTo,类型转换,示例中只是用于复制;
0.3toGrayscale函数,将数据转换为可以显示的灰度图像格式
static Mat toGrayscale(InputArray _src) {
Mat src = _src.getMat();
// only allow one channel
if(src.channels() != 1) {
CV_Error(Error::StsBadArg, "Only Matrices with one channel are supported");
}
// create and return normalized image
Mat dst;
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
return dst;
}
0.4params结构体,用于滑动条响应函数传递参数
struct params
{
Mat data;
int ch;
int rows;
PCA pca;
string winName;
};
0.5onTrackbar函数,响应滑动条操作,先将第一个样本投影到新的基(降维),再反投影,最后灰度化
static void onTrackbar(int pos, void* ptr)
{
cout << "Retained Variance = " << pos << "% ";
cout << "re-calculating PCA..." << std::flush;
double var = pos / 100.0;
struct params *p = (struct params *)ptr;
p->pca = PCA(p->data, cv::Mat(), PCA::DATA_AS_ROW, var);
Mat point = p->pca.project(p->data.row(0));
Mat reconstruction = p->pca.backProject(point);
reconstruction = reconstruction.reshape(p->ch, p->rows);
reconstruction = toGrayscale(reconstruction);
imshow(p->winName, reconstruction);
cout << "done! # of principal components: " << p->pca.eigenvectors.rows << endl;
}
1.main函数
1.1在命令行中获取写有文件列表的文档
if (argc != 2) {
cout << "usage: " << argv[0] << " <image_list.txt>" << endl;
exit(1);
}
1.2将文档目录存入变量
// Get the path to your CSV.
string imgList = string(argv[1]);
1.3声明存储图像Mat对象的向量
// vector to hold the images
vector<Mat> images;
1.4调用全局函数read_imgList,从txt文件中加载pgm格式图像
// Read in the data. This can fail if not valid
try {
read_imgList(imgList, images);
} catch (cv::Exception& e) {
cerr << "Error opening file \"" << imgList << "\". Reason: " << e.msg << endl;
exit(1);
}
1.5判断如果读取的图像少于2,则返回
// Quit if there are not enough images for this demo.
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);
}
1.6将所有图像存入一个Mat中,每一行表示一个样本
// Reshape and stack images into a rowMatrix
Mat data = formatImagesForPCA(images);
1.7创建PCA对象
// perform PCA
PCA pca(data, cv::Mat(), PCA::DATA_AS_ROW, 0.95); // trackbar is initially set here, also this is a common value for retainedVariance
1.8先将第一个样本投影到新的基(降维),再反投影,最后灰度化
// Demonstration of the effect of retainedVariance on the first image
Mat point = pca.project(data.row(0)); // project into the eigenspace, thus the image becomes a "point"
Mat reconstruction = pca.backProject(point); // re-create the image from the "point"
reconstruction = reconstruction.reshape(images[0].channels(), images[0].rows); // reshape from a row vector into image shape
reconstruction = toGrayscale(reconstruction); // re-scale for displaying purposes
1.9创建窗口
// init highgui window
string winName = "Reconstruction | press 'q' to quit";
namedWindow(winName, WINDOW_NORMAL);
1.10记录参数
// params struct to pass to the trackbar handler
params p;
p.data = data;
p.ch = images[0].channels();
p.rows = images[0].rows;
p.pca = pca;
p.winName = winName;
1.11创建滑动条
// create the tracbar
int pos = 95;
createTrackbar("Retained Variance (%)", winName, &pos, 100, onTrackbar, (void*)&p);
1.12显示结果,如果键盘按键q响应,则推出
// display until user presses q
imshow(winName, reconstruction);
int key = 0;
while(key != 'q')
key = waitKey();
参考资料:
1.《
Opencv起步之主成分分析(PCA)
》