问题
After extracting HOG features of folder of images, I want to add all this results in one matrix. How I can do this? this is my code in 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:
By looking at the documentation, calling extractHOGFeatures
computes a 1 x N
vector given an input image. Because it can be a bit cumbersome to calculate what the output size of this may be, which also depends on what parameters you set up for the HOG detector, it's best to first create an empty matrix and dynamically concatenate the features at each iteration. Usually for performance you pre-allocate a matrix if you want to populate the elements on an iterative basis. Not doing it this way gives a slight ding in performance, but it's the most adaptable given your situation. You may want to adjust the HOG parameters and if we do it the dynamic way, that removes the headache of determining what the total size of the matrix should be.
So do something like this. I've placed %//New
tags where I've modified your code:
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
will contain your HOG features where each row is for each image. Therefore, for a particular image i
, you can determine the HOG features by:
feature = featureMatrix(i,:);
Caveat
I need to mention that the above code assumes that all images in your directory are the same size. If they're not, then the output vector size for each HOG call is going to be different. If that's the case, you'll want to have a cell array to adapt for the different sizes.
Therefore, do something like this:
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
To access a particular image's features, or image i
, do:
feature = featureMatrix{i};
来源:https://stackoverflow.com/questions/30353483/how-to-add-hog-features-into-matrix-matlab