MATLAB: Rgb2gray error

MATLAB and Simulink Student Suiterbg2gray error images processing

Good evening everyone,
I'm trying to read all images in a folder and convert them in gray scale with rgb2gray with this code (my images are not gray scale , there are RGB)
Filelist= dir('Clipboard*.png');
nfiles = length(Filelist);
for i=1:size(Filelist,1)
t=imread(Filelist(i).name);
I(:,:,i)=rgb2gray(t);
end
And after calculate the ssd between all the images :
N=((size(I(:,:,1),1)*size(I(:,:,1),2)).^2);
SSD=zeros(size(Filelist,1),size(Filelist,1));
for i=1:1:nfiles
for j=1:1:nfiles
SSD(i,j)=sum(sum(((I(:,:,i)-I(:,:,j)).^2)/(261501241)));
end
end
imagesc(SSD);
but i have this error:
Error using rgb2gray>parse_inputs (line 80)
MAP must be a m x 3 array.
Error in rgb2gray (line 52)
isRGB = parse_inputs(X);
Error in test21 (line 32)
I(:,:,i)=rgb2gray(t);

Best Answer

Convert to gray scale:
Filelist= dir('Clipboard*.png');
numberOfFiles = length(Filelist);
for k = 1 : numberOfFiles % Don't use i (the imaginary variable).
thisImage = imread(Filelist(k).name);
[rows, columns, numberOfColorChannels] = size(thisImage);
if k == 1
% Instantiate I with the size of the first image.
I = zeros(rows, columns, numberOfFiles, class(thisImage));
end
if numberOfColorChannels > 1
% It's color. Need to convert to gray scale before putting it into the 3D image as the k'th slice.
I(:,:, k) = rgb2gray(thisImage);
else
% Already grayscale. No need to convert. Just put in directly as the k'th slice.
I(:,:, k) = thisImage;
end
end
Related Question