I got an error with this code. any one help me, please :(

2 views (last 30 days)
clc;
clear all;
close all;
TrainDatabasePath = 'C:\Users\diahj\OneDrive\Documents\Data Train\';
TestDatabasePath = 'C:\Users\diahj\OneDrive\Documents\Data Test\';
TestImage = strcat(TestDatabasePath,'\','1','.jpg');
im1 = imread(TestImage);
im = imresize(im1,0.01);
TrainFiles = dir(TrainDatabasePath);
% Train_Number = 0;
% T = [];
% for i = 1 : 2
str = int2str(1);
str = strcat('\',str,'.jpg');
str = strcat(TrainDatabasePath,str);
im2 = imread(str);
im3 = imresize(im2,0.03);
img = rgb2gray(im3); %Gives the matrix form of the image
%imshow(img);
[irow icol] = size(img); %Gives the size of the image matrix
temp = reshape(img',irow*icol,1); %Converts the inverse matrix of image into a single column matrix
T = temp;
% end
m = mean(T,2); %returns a column containing mean value of each row
%Train_Number = size(T,2);
%[r c] = size(T);
%A = zeros(r,c);
%for i = 1 : Train_Number %to convert to double precision value (64-bit) so as to cover large numbers and find differences with mean values
temp = double(T(:,1))-m ;
A = [temp];
%end
L = A'*A;
[V D] = eig(L); %Eigen matrix : for detailed comparisons
L_eig_vec = zeros(L);
%for i = 1 : size(V,2)
if( D(1,1)>1 )
L_eig_vec = [L_eig_vec V(:,1)]; %Separating positive(>0) Eigen values from the Eigen Matrix(removing black color)
end
%end
L_eig_vec = [0];
%disp(L_eig_vec);
Eigenfaces = A * L_eig_vec; %Converts all the extracted eigen values to a double precision column matrix
%ProjectedImages = zeros(size(Eigenfaces));
ProjectedImages = [];
Train_Number = size(Eigenfaces,2);
for i = 1 : Train_Number
temp = Eigenfaces'*A(:,i);
ProjectedImages = [ProjectedImages temp]; %Creates projection from previous data
end
InputImage = imread(TestImage); %Read input test image
InputImage = imresize(InputImage,0.01);
temp = rgb2gray(InputImage); %Gray scale image formed
[irow icol] = size(temp);
InImage = reshape(temp',irow*icol,1);
Difference = double(InImage)- m; %Double precision (64 bit)
ProjectedTestImage = Eigenfaces'*Difference;
Euc_dist = zeros(size(ProjectedImages)); for i = 1 : Train_Number %Calculates Euclidean distance(checks diff between projected and trained image) q = ProjectedImages(:,i); temp = ( norm( ProjectedTestImage - q ) )^2; Euc_dist = [Euc_dist temp]; end [Euc_dist_min , Recognized_index] = min(Euc_dist); %Minimum Euclidean Distance OutputName = strcat(int2str(Recognized_index),'.jpg'); %Create path for recognized image SelectedImage = strcat(TrainDatabasePath,'\',OutputName); SelectedImage = imread(SelectedImage); %imshow(im); %title('Test Image'); figure(); imshow(SelectedImage); title('Equivalent Image'); %str = strcat('Matched image is : ',output); %disp(str); op = strcat(int2str(Recognized_index),''); temp=strcat('The identified gesture enclosed in the image is : ',op); disp(temp);
the extension format was mpo, i replaced with jpg cause i have jpg dataset. I wanna try my dataset but i got this error
Error using - Matrix dimensions must agree.
Error in singleimage (line 55) Difference = double(InImage)- m; %Double precision (64 bit)
  3 Comments
Diah Junaidi
Diah Junaidi on 29 Nov 2017
the previous data set's ext was .MPO but I wanna use mine with the .JPG's data set, sir.
Rik
Rik on 29 Nov 2017
I don't know the MPO file type, so I will assume you are loading the JPGs correctly.
Have you tried the debugger? Have read the links I provided?
If that doesn't help, try to reduce your code to a MWE, so we can reproduce it. You will have to provide relevant files, but try to make it as small as possible. This usually helps you find the error yourself, and if it doesn't, it helps others find it.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 29 Nov 2017
For line 55 (insert before your existing line 55), put this:
[rows1, columns1, numberOfColorChannels1] = size(InImage) % No semicolon!!!
[rows2, columns2, numberOfColorChannels2] = size(m) % No semicolon!!!
Tell me what you have in the command window -- copy and paste back here in a comment below. You should see two sets of 3 numbers and the sets should match exactly. Evidently they don't because you're trying to subtract two images that aren't the same lateral size, or else one is color and one is gray scale.

Products

Community Treasure Hunt

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

Start Hunting!