【问题标题】:How to Use PCA to Reduce Dimension如何使用 PCA 降维
【发布时间】:2015-02-28 05:51:58
【问题描述】:

输入:从尺寸为 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。

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

问候

哈里斯...

【问题讨论】:

  • hi-dim lbp,很酷;)但是您不应该为所有图像训练一个大 pca(离线)吗?每张图片都没有?
  • 嗨,@Haris,实际上,我正在玩同样的想法,atm。一路从 pca 到 random-projections,然后到 walsh-hadamard,不要忘记小波,最后是简单的 dft -> throw_out_hf -> dft_back,但最终 - 不使用 lbp(u),而是使用更短的四 - patch-lbp 功能(仅 16 个 bin,而不是 256 [lbpu 甚至 59],然后跳过任何功能压缩)对我来说效果更好。厘米? ;)
  • 我也很好奇,你是如何解决“IntraFace not available”问题的;)(我的主要问题是要找到一个好的替代品......)
  • 嗨@berak 感谢您的回复,关于您的第一条评论,我正在关注here 的实现,但不知道如何使用PCA 来减少维度。目前我正在获取大小为 75520 的特征向量,但对如何将其输入 PCA 感到困惑。
  • 您好,我对您的第二条评论有一些疑问,在您的四补丁-lbp 特征提取中,您如何修补面部?您是考虑整个面部吗?还是基于某些土地-标记?

标签: opencv pca


【解决方案1】:

您必须从 很多 图像中收集特征向量,从中(离线)制作单个 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 火车图像计算 pca 和 75k 特征元素将需要一个美好的一天;)

【讨论】:

  • 嗨,@berak 感谢您的回答,我会尽力理解您的回答,但在此之前还有一个问题,假设我必须比较两个图像,例如加载 img1-> 计算 LBP(1X75520) -> 应用 PCA 降维,然后对 img2 进行相同的过程,最后比较 img1 和 img2 的 PCA 输出的相似性。
  • 是的,完全正确。您比较 2 张图像的投影(缩短)特征。
  • 顺便说一句,intraface 呢?
  • 我通过一个开源项目从 github 的其他地方获得了该库。
  • 你需要保存 pca.mean 和 pca.eigenvectors。后面的投影是:从你的特征中减去平均值,然后乘以转置的特征向量。
猜你喜欢
  • 2017-08-05
  • 2013-03-06
  • 2013-12-31
  • 2020-10-27
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
  • 2018-01-28
相关资源
最近更新 更多