gpt4 book ai didi

opencv - 如何使用 PCA 降维

转载 作者:太空宇宙 更新时间:2023-11-03 21:27:16 24 4
gpt4 key购买 nike

输入:从一张75520维的图像中提取的LBP特征,所以输入的LBP数据包含1行75520列。

要求的输出:对输入应用 PCA 以降低维度,

目前我的代码看起来像,

void PCA_DimensionReduction(Mat &src, Mat &dst){

int PCA_DIMENSON_VAL 40
Mat tmp = src.reshape(1,1); //1 rows X 75520 cols
Mat projection_result;
Mat input_feature_vector;
Mat norm_tmp;
normalize(tmp,input_feature_vector,0,1,NORM_MINMAX,CV_32FC1);
PCA pca(input_feature_vector,Mat(),CV_PCA_DATA_AS_ROW, PCA_DIMENSON_VAL);
pca.project(input_feature_vector,projection_result);
dst = projection_result.reshape(1,1);
}

基本上我使用此功能来匹配两个图像之间的相似性,但由于没有应用 PCA,我没有得到正确的结果。

任何帮助将不胜感激...

问候

哈里斯...

最佳答案

您必须从大量图像中收集特征向量,从中生成单个 pca(离线),然后使用均值和特征向量进行投影。

// let's say, you have collected 10 feature vectors a 30 elements.
// flatten them to a single row (reshape(1,1)) and push_back into a big Data Mat

Mat D(10,30,CV_32F); // 10 rows(features) a 30 elements
randu(D,0,10); // only for the simulation here
cerr << D.size() << endl;
// [30 x 10]


// now make a pca, that will only retain 6 eigenvectors
// so the later projections are shortened to 6 elements:

PCA p(D,Mat(),CV_PCA_DATA_AS_ROW,6);
cerr << p.eigenvectors.size() << endl;
// [30 x 6]

// now, that the training step is done, we can use it to
// shorten feature vectors:
// either keep the PCA around for projecting:

// a random test vector,
Mat v(1,30,CV_32F);
randu(v,0,30);

// pca projection:
Mat vp = p.project(v);

cerr << vp.size() << endl;
cerr << vp << endl;
// [6 x 1]
// [-4.7032223, 0.67155731, 15.192059, -8.1542597, -4.5874329, -3.7452228]


// or, maybe, save the pca.mean and pca.eigenvectors only, and do your own projection:

Mat vp2 = (v - mean) * eigenvectors.t();

cerr << vp2.size() << endl;
cerr << vp2 << endl;
//[6 x 1]
//[-4.7032223, 0.67155731, 15.192059, -8.1542597, -4.5874329, -3.7452228]

好吧,哦,缺点是:从 4.4k 火车图像和 75k 特征元素计算 pca 会很顺利 ;)

关于opencv - 如何使用 PCA 降维,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27733002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com