如何将HOG功能添加到矩阵(matlab)

时间:2015-05-20 15:01:26

标签: matlab image-processing matrix computer-vision matlab-cvst

在提取图像文件夹的HOG功能后,我想将所有这些结果添加到一个矩阵中。我怎么能这样做?这是我在matlab中的代码:

training_female = 'E:\Training Set\Female Images';

% read all images with specified extention, its jpg in our case
filenames = dir(fullfile(training_female, '*.jpg'));

% count total number of photos present in that folder
total_images = numel(filenames);

for n = 1:total_images

% Specify images names with full path and extension    
full_name= fullfile(training_female, filenames(n).name);

% Read images
training_images = imread(full_name);
[featureVector, hogVisualization] = extractHOGFeatures(training_images);
figure (n)

    % Show all images
    imshow(training_images); hold on;                  
    plot(hogVisualization);
end

1 个答案:

答案 0 :(得分:2)

通过查看documentation,调用extractHOGFeatures计算给定输入图像的1 x N向量。因为计算它的输出大小可能有点麻烦,这也取决于您为HOG检测器设置的参数,所以最好首先创建一个空矩阵并在每次迭代时动态连接这些特征。通常,对于性能,如果要在迭代的基础上填充元素,则预先分配矩阵。不这样做会给性能带来轻微的影响,但考虑到你的情况,它是最具适应性的。您可能希望调整HOG参数,如果我们以动态方式执行,则可以消除确定矩阵总大小应该是什么的麻烦。

所以做这样的事情。我已将%//New个标记放在我修改代码的位置:

training_female = 'E:\Training Set\Female Images';

% read all images with specified extention, its jpg in our case
filenames = dir(fullfile(training_female, '*.jpg'));

% count total number of photos present in that folder
total_images = numel(filenames);

featureMatrix = []; %// New - Declare feature matrix

for n = 1:total_images

    % Specify images names with full path and extension    
    full_name= fullfile(training_female, filenames(n).name);

    % Read images
    training_images = imread(full_name);
    [featureVector, hogVisualization] = extractHOGFeatures(training_images);

    %// New - Add feature vector to matrix
    featureMatrix = [featureMatrix; featureVector];
    figure(n);

    % Show all images
    imshow(training_images); hold on;                  
    plot(hogVisualization);
end

featureMatrix将包含您的HOG功能,其中每行代表每个图像。因此,对于特定图像i,您可以通过以下方式确定HOG功能:

feature = featureMatrix(i,:);

买者

我需要提一下,上面的代码假设目录中的所有图片大小相同。如果它们不是,则每个HOG调用的输出向量大小将不同。如果是这种情况,您将需要一个单元阵列来适应不同的大小。

因此,做这样的事情:

training_female = 'E:\Training Set\Female Images';

% read all images with specified extention, its jpg in our case
filenames = dir(fullfile(training_female, '*.jpg'));

% count total number of photos present in that folder
total_images = numel(filenames);

featureMatrix = cell(1,total_images); %// New - Declare feature matrix

for n = 1:total_images

    % Specify images names with full path and extension    
    full_name= fullfile(training_female, filenames(n).name);

    % Read images
    training_images = imread(full_name);
    [featureVector, hogVisualization] = extractHOGFeatures(training_images);

    %// New - Add feature vector to matrix
    featureMatrix{n} = featureVector;
    figure(n);

    % Show all images
    imshow(training_images); hold on;                  
    plot(hogVisualization);
end

要访问特定图片的功能或图像i,请执行以下操作:

feature = featureMatrix{i};