【发布时间】:2018-04-11 02:17:52
【问题描述】:
我想在 OpenCV 中实现快速 PLDA(概率线性判别分析)。在此,LINK 快速 PLDA 已在Matlab 和Python 中实现。 PLDA 的组成部分之一是 LDA。我为在 OpenCV 中实现 LDA 编写了以下代码:
int LDA_dim = 120;
// Load data
FileStorage fs("newStorageFile.yml", FileStorage::READ);
// Read data
Mat train_data, train_labels;
fs["train_data"] >> train_data;
fs["train_labels"] >> train_labels;
// LDA
if (LDA_dim > 0)
{
LDA lda(LDA_dim);
lda.compute(train_data, train_labels); // compute eigenvectors
Mat eigenvectors = lda.eigenvectors();
}
我已将上述链接中介绍的数据库从.mat 转换为.yml。结果是newStorageFile.yml,我上传了here。 train_data 有 650 行和 600 列,而 train_labels 有 650 行和 1 列。我不知道为什么特征向量和特征值变为零!!? PLZ帮我修复这个代码。
最好把将数据从.mat转换成.yml的代码带上来:
function matlab2opencv( variable, fileName, flag)
[rows cols] = size(variable);
% Beware of Matlab's linear indexing
variable = variable';
% Write mode as default
if ( ~exist('flag','var') )
flag = 'w';
end
if ( ~exist(fileName,'file') || flag == 'w' )
% New file or write mode specified
file = fopen( fileName, 'w');
fprintf( file, '%%YAML:1.0\n');
else
% Append mode
file = fopen( fileName, 'a');
end
% Write variable header
fprintf( file, ' %s: !!opencv-matrix\n', inputname(1));
fprintf( file, ' rows: %d\n', rows);
fprintf( file, ' cols: %d\n', cols);
fprintf( file, ' dt: f\n');
fprintf( file, ' data: [ ');
% Write variable data
for i=1:rows*cols
fprintf( file, '%.6f', variable(i));
if (i == rows*cols), break, end
fprintf( file, ', ');
if mod(i+1,4) == 0
fprintf( file, '\n ');
end
end
fprintf( file, ']\n');
fclose(file);
编辑 1 ) 我用自己生成的一些样本尝试了 LDA:
Mat train_data = (Mat_<double>(3, 3) << 25, 45, 44, 403, 607, 494, 2900, 5900, 2200);
Mat train_labels = (Mat_<int>(3, 1) << 1, 2, 3 );
LDA lda(LDA_dim);
lda.compute(train_data, train_labels); // compute eigenvectors
Mat_<double> eigenvectors = lda.eigenvectors();
Mat_<double> eigenvalues = lda.eigenvalues();
cout << eigenvectors << endl << eigenvalues;
【问题讨论】:
-
@bytefish 既然你已经在 OpenCV 中开发过 LDA,我想你可以帮助我......
-
你找到解决这个问题的方法了吗?我在这里面临同样的问题,我保存了 lda.yml,但是当我打开文件时,所有向量都是 0.0
-
@Amir,不,我改变了我的算法!!
-
首先,数学特征向量不能为零(特征值可以)。其次,为方阵定义特征向量和特征值。对于非方阵,定义了奇异值。您正在显示三个向量 (0,0)、(1,0) 和 (0,1);和两个特征值 0 和 0。
标签: c++ opencv probability eigenvalue linear-discriminant