How to perform automatic conversion of matrix dimensions of images in a way that it works for both rgb and grayscale ??

To perform face recognition, i have a train database with some images. The images in train database need to be pre-processed before they are used for comparison with the test image, which is the input image.
This is what i have done :
T = [];
for i = 1 : Train_Number
str = int2str(i);
str = strcat('\',str,'.jpg');
str = strcat(TrainDatabasePath,str);
img = imread(str);
img = rgb2gray(img);
img = imresize(img, [200 180]);
[irow icol] = size(img);
temp = reshape(img',irow*icol,1);
% temp = reshape(permute(double(img), [2,1,3]), irow*icol, 1);
T = [T temp]; % 'T' grows after each turn
end
This works fine when the train database contains only rgb images. But for grayscale images, it gives error related to dimensions.
How can i make it work for both rgb and grayscale ??

2 Comments

Please do not only claim, that there is an error, but post the message and mention the line also. The less we have to guess, the more efficient is an answer. Thanks.
Ok.. When i include a grayscale image in the database, i get an error.
This is my code :
T = [];
for i = 1 : Train_Number
str = int2str(i);
str = strcat('\',str,'.jpg');
str = strcat(TrainDatabasePath,str);
img = imread(str);
% img = rgb2gray(img);
img = imresize(img, [200 180]);
[irow icol] = size(img);
temp = reshape(permute(double(img), [2,1,3]), irow*icol, 1);
T = [T temp]; % 'T' grows after each turn
end
This is the error i get :
*??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> CreateDatabase at 50
T = [T temp]; % 'T' grows after each turn*

Sign in to comment.

 Accepted Answer

Some cleanup and an implicit conversion of grayscale images to pseudo-RGB:
nCol = 200;
nRow = 180;
% BUG?! T = nan(nCol * nRow, Train_Number); % Pre-allocate!!!
T = nan(nCol * nRow * 3, Train_Number); % Pre-allocate!!! [EDITED]
for i = 1 : Train_Number
filename = fullfile(TrainDatabasePath, sprintf('%d.jpg', i));
img = imread(filename);
if ndims(img) == 2 % Gray scale image:
img = double(cat(3, img, img, img)); % Pseudo RGB image
end
img = imresize(img, [nCol, nRow]);
temp = permute(img, [2,1,3]);
T(:, i) = temp(:);
end
But, I do not expect that the training of a face recognition system is efficient, when you feed it with RGB and gray-scale images. I'd prefer converting the RGB to gray scale instead.
img = rgb2gray(img);

8 Comments

I tried your code,and this is the error i am getting :
??? Subscripted assignment dimension mismatch.
Error in ==> CreateDatabase at 63 T(:, i) = temp(:);
What does it mean ??
This means, that the left hand side "T(:, i)" and the right hand side "temp(:)" have a different number of elements. The debugger can reveal such problems easily:
dbstop if error
Then start the code again until it stops at the error. Then:
size(T)
size(temp)
I assume, you need:
T = nan(nCol * nRow * 3, Train_Number); % Pre-allocate!!!
Thanks..
But it is still giving an error in recognition.m :
This is my code :
% Extracting the PCA features from test image
I = imread(TestImage);
InputImage = imresize(I, [200 180]);
temp = InputImage(:,:,1);
[irow icol] = size(temp);
InImage = reshape(temp',irow*icol,1);
Difference = double(InImage)-m; % Centered test image
ProjectedTestImage = Eigenfaces'*Difference; % Test image feature vector
and this is the error :
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> Recognition at 52
Difference = double(InImage)-m; % Centered test image
Here, m is the mean of face images in training database,
and, m = M*Nx1 where M*N = [200 180]
m comes from eigenface.m :
where, m = mean(T,2);
and T comes from createdatabase.m :
where T is a 2D matrix containing 1D image vectors, i have already mentioned that part.
@Lian: There is no chance that I can guess the size and type of "m" in your code. So please use the debugger: dbstop if error and find the sizes of the variables. Perhaps you need bsxfun, or imresize(m, 200, 180), or reshape(m, ...) also. In any way, please try to solve this by your own at first.
Bugs are gone !!
Thanks to Image Analyst for the video tutorial.. Its exactly what i needed. And Thanks Jan Simon for all the suggestions..
This was the problem :
InImage : unint8 36000x1
m : double 108000x1
Eigenfaces : double 108000x20
So i converted all of them to double 36000x1 using imresize.
Is that the right way to solve it ?? Anyway, it works fine for the bugs.
Now one last thing, the end result is not matching exactly, which means it selects the wrong image. While i was trying with only RGB images, it was identifying the exact image, and matching the test image with the train image.
Is this inaccuracy caused because of the dimension conversion ??
Matrix dimensions must agree.
Error in recog (line 18) Difference = double(InImage)-m; % Centered test image
Error in main (line 115) OutputName=recog(TestImage,m,A,Eigenfaces);
HELP????
@Mahima Goyen: All you provide is a line of code and the partial error message. It seems like "double(InImage)" and "m" do not have the same dimensions. Without further information, there is no chance to guess, why this happens or what the problem is.
Please open a new thread for a new question and add more information.

Sign in to comment.

More Answers (0)

Asked:

on 15 Feb 2013

Commented:

Jan
on 9 Nov 2017

Community Treasure Hunt

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

Start Hunting!