How to Save image Features in .mat file??

I am trying to extract facial features from image using HOG and trying to save them in .mat file, but when i run my code it saves the features of all images in one column. I want to save the features of each image in separate row.
Here is my CODE:
myFolder = 'folder'; % or whatever.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
matFiles = dir(filePattern);
for k = 1:length(matFiles)
baseFileName = matFiles(k).name;
img = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', img);
storedStructure = imread(img);
% Now do whatever you need to do with the structure you recalled.
%figure % New figure, do not specify the index
%imshow(img);
FaceDetect = vision.CascadeObjectDetector;
FaceDetect.MergeThreshold = 7 ;
BB = step(FaceDetect,storedStructure); %figure(2),imshow(img);
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',3,'LineStyle','- ','EdgeColor','r');
end
% figure
for i = 1:size(BB,1)
J= imcrop(storedStructure,BB(i,:));
%subplot(6,6,i);
figure
imshow(J);
% Create a folder with the base file name
[~, baseFileNameNoExt, ~] = fileparts(baseFileName);
outputFolder = fullfile(myFolder, baseFileNameNoExt);
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
fprintf('Created folder : %s\n', outputFolder);
end
% Prepare output filename
outputFileName = fullfile(outputFolder, baseFileName);
% Write file to that folder
imwrite(J, outputFileName);
fprintf(' Copied %s to the folder called %s.\n', baseFileName, outputFolder);
end
end
J
HOG(J)
X=HOG(J)
save('features.mat','J');

 Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 27 Apr. 2018

0 Stimmen

In your code, the matrix you want to save is X, not J. Also, you are also calculating HOG feature for the last image. To calculate for all images and save them write your code as follows.
  • Before the start of first for loop add the following line
X = [];
  • At the end of inner for loop add the following line, like this
fprintf(' Copied %s to the folder called %s.\n', baseFileName, outputFolder); % line from your code
X{k, i} = HOG(J);
Each row in X represents HOG feature for one complete image. Each element in the row represents HOG features for one bounding box.
Then save the features like this
save('features.mat', 'X');

9 Kommentare

saeeda saher
saeeda saher am 29 Apr. 2018
Bearbeitet: saeeda saher am 29 Apr. 2018
I did it the way you guided:
myFolder = 'folder'; % or whatever.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
matFiles = dir(filePattern);
X=[]
for k = 1:length(matFiles)
baseFileName = matFiles(k).name;
img = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', img);
storedStructure = imread(img);
% Now do whatever you need to do with the structure you recalled.
%figure % New figure, do not specify the index
%imshow(img);
FaceDetect = vision.CascadeObjectDetector;
FaceDetect.MergeThreshold = 7 ;
BB = step(FaceDetect,storedStructure); %figure(2),imshow(img);
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',3,'LineStyle','- ','EdgeColor','r');
end
% figure
for i = 1:size(BB,1)
J= imcrop(storedStructure,BB(i,:));
%subplot(6,6,i);
figure
imshow(J);
% Create a folder with the base file name
[~, baseFileNameNoExt, ~] = fileparts(baseFileName);
outputFolder = fullfile(myFolder, baseFileNameNoExt);
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
fprintf('Created folder : %s\n', outputFolder);
end
% Prepare output filename
outputFileName = fullfile(outputFolder, baseFileName);
% Write file to that folder
imwrite(J, outputFileName);
fprintf(' Copied %s to the folder called %s.\n', baseFileName, outputFolder);
X{k, i} = HOG(J);
save('features.mat', 'X');
end
end
It saves the features of image not in a complete row. I want the features in row not in column.
Move the save line
save('features.mat', 'X');
outside the for loop, at the end of the code. Also, you can access column vector HOG features using curly bracket notation e.g. X{1,1}, X{3, 2}. Here X{i, j} represent HOG feature of j^th bounding box of i^th image.
saeeda saher
saeeda saher am 29 Apr. 2018
Bearbeitet: saeeda saher am 30 Apr. 2018
again same results. Its saving the features in 1 column, not saving them in rows. Please help
Ameer Hamza
Ameer Hamza am 30 Apr. 2018
Bearbeitet: Ameer Hamza am 30 Apr. 2018
I still cannot clearly understand how do you want to organize your matrix, because the method I presented will separately store all HOG features, which you can access easily. But if you want all the HOG features, of all the bounding boxes, of all the images in one matrix, where each row will represent one HOG feature, then replace the X{k, i} = HOG(J); line with the following line
X = [X HOG(J)'];
If you still want some different format, please provide an example of MATLAB matrix (probably take a screenshot) and show how the output should look like.
saeeda saher
saeeda saher am 30 Apr. 2018
Dear, Now its saving the features of all images into only one row. I want to save the features of each image into separate row.
saeeda saher
saeeda saher am 30 Apr. 2018
I want to save them in following format as shown in attachment
Alright, so now it is somewhat clear. Try the following
  • replace X = [] with
X = cell(length(matFiles), 1);
  • Replace X = [X HOG(J)'] with
X{k} = [x{k} HOG(J)'];
For the first image, use X{1} and it will return a row vector. similarly for second image use X{2} and so on.
saeeda saher
saeeda saher am 30 Apr. 2018
Thank You @Ameer Hamza
Ameer Hamza
Ameer Hamza am 30 Apr. 2018
You are welcome.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 27 Apr. 2018

0 Stimmen

You are doing a loop with imcrop, but you are only storing the output for the current iteration in J. Afterwards, J is going to have the value it had on the last iteration of the loop. You are then applying HOG only to that one cropped version.
You should be calculating the HOG inside the loop and saving all of the results.

Kategorien

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by