为什么特征向量& LDA中的特征值变为零?

时间:2017-10-30 06:55:41

标签: c++ opencv probability eigenvalue linear-discriminant

我想在OpenCV中实现快速PLDA(概率线性判别分析)。在此LINK快速PLDA已在MatlabPython中实施。 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我已上传heretrain_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;

但我得到相同的结果:特征值和特征向量变为零: eigenvector and eigenvalue

1 个答案:

答案 0 :(得分:0)

由于浮点数的不精确,特征值接近于零。