Rgb2gray error

3 次查看(过去 30 天)
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);

采纳的回答

Image Analyst
Image Analyst 2020-4-1
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by