MATLAB: Hi! Would you please help me :] the db.mat only save the last input image value. this don’t give multiple images input values.

help

%%Module Training
clc;
clear all;
close all;
[fname path]=uigetfile('.jpg','Open a Face as input for Training');
fname=strcat(path,fname);
img=imread(fname);
imshow(img);
%%drawnow;
title('Input Face');
c=input('Enter the Class ');
%%Feature Extraction
F = FeatureStatistical(img);
try
S = load('db.mat');
F=[F c];
db=[db;F];
save db.mat db
catch
F=reshape(F,[3,2]);
F=reshape(F,[1,6]);
db=[F c];
save db.mat db
end
%%FeatureStatistical
function [F]= FeatureStatistical(img)
img=double(img);
mn=mean(mean(img));
sd=std(std(img));
F=[mn sd];
%%FaceClassifier
clc;
clear all;
close all;
[fname path]=uigetfile('.jpg','Provide a Face for Testing');
fname=strcat(path,fname);
img=imread(fname);
imshow(img);
title('Test Face');
Ftest = FeatureStatistical(img);
load db.mat
Ftrain=db(:,1:2);
Ctrain=db(:,7);
for(i=1:size(Ftrain,1))
dist(i,:)=sum(abs(Ftrain(i,:)-Ftest));
end
Min=min(dist);
if(Min<3)
m=find(dist==Min,1);
det_class=Ctrain(m);
msgbox(strcat('Detected Class= ',num2str(det_class)));
else
msgbox('Not Same');
end

Best Answer

Your question concerned the saving of data. I find the save command here:
try
S = load('db.mat');
F=[F c];
db=[db;F];
save db.mat db
catch
F=reshape(F,[3,2]);
F=reshape(F,[1,6]);
db=[F c];
save db.mat db
end
Now we cannot guess what happens, and the code suppressed the output of explanations also. What a pity. Prefer to display a message, if you catch an error:
try
S = load('db.mat');
F=[F c];
db=[db; F, c];
catch ME
disp(ME.message);
% F = reshape(F,[3,2]); % Omit the duplicate reshaping - it has no effect
F = reshape(F, [1,6]);
db = [F c];
end
save('db.mat', 'db');
So what do you see? Perhaps the file "db.mat" is not found in the current folder. Then provide a path.
Some hints:
  • "db.mat only save the last input image value" is not clear. Where do you expect the code to behave differently?
  • It is a mystery for me, why so many users include this evil brute clearing header in the code: clc;clear all;close all; Especially the clear all is bad, because it deletes all loaded functions from the memory. Reloading from the disk is slow and a waste of energy.
  • Do not shadow the important built-in function "path" by a local variable. This can cause strange results during debugging.
  • Use fullfile instead of strcat to create a file name.
  • While mean(mean(X)) is a valid method to calculate the total mean value as in mean(X(:)), this does not work for std. Are you sure that you want to get the standard deviation over the the standard deviations of the columns of X? I assume std(std(img)) is not what you want.