Matlab:检查目录中的所有图像是否为灰度?

时间:2011-07-15 09:20:20

标签: matlab image-processing colors

我想检查当前目录中的所有jpg图像,如果它们是灰度或包含彩色像素...我试过:

figdirectory = pwd;
fullpath = sprintf('%s/*.jpg', figdirectory);
d = dir(fullpath);

% Loop
pages = [];
for i = 1:length(d)
    f = d(i).name;
    fname_input = sprintf('%s/%s', figdirectory, f);

    A = imread(fname_input); 
    B = rgb2gray(A);

    if(A-B == 0)
        hascolor = 0;
    else
        hascolor = 1;
    end
    pages = [pages; hascolor];        
end 

但是这给了我关于A和B的矩阵维度的错误。为什么A是第三维? 谢谢!

4 个答案:

答案 0 :(得分:4)

要做到这一点:

0)假设您的输入纯粹是“图像”而不是“音量”。

1)[r c d] = size(im);

2)如果d大于1,那么它是彩色图像

3)另外,它必须是灰度图像。

编辑: 您可以添加一个条件以将灰度img与颜色img牢固地区分开来。假设你的颜色imgs是3个通道,

如果d等于3,则检查所有3个通道是否相等

im(:,:,1)== im(:,:,2)&& IM(:,:,1)== IM(:,:,3) 然后你有一个灰度图像,否则颜色img

答案 1 :(得分:2)

来自imread http://www.mathworks.co.uk/help/techdoc/ref/imread.html

的手册
  

如果文件包含灰度图像,则A是M-by-N阵列。如果   file包含一个真彩色图像,A是一个M-by-N-by-3数组。

因此A将为彩色图像提供第三维。

如果您想测试灰度,那么您可以将图像转换为hsv。

B = rgb2hsv(A)

来自手册http://www.mathworks.co.uk/help/techdoc/ref/hsv2rgb.html

  

当B(:,2)为0时,颜色是不饱和的(即灰色阴影)。

如果是真彩色图像,就像张贴的那样,如果唯一(B(:,:,2))为零,那么

A = imread(fname_input);        
B = rgb2hsv(A);

%# if saturation levels are zero then it's a greyscale image
if(unique(B(:,:,2)) == 0)
    hascolor = 0;
else         
    hascolor = 1; 
end 

答案 2 :(得分:2)

您可以使用IMFINFO执行此任务,这样就不必将图像加载到内存中。

figdirectory = pwd;
fullpath = sprintf('%s/*.jpg', figdirectory);
d = dir(fullpath);

nImages = length(d);

%# imageType is a cell array with either 'grayscale', 'truecolor', or 'indexed', 
%# depending on the kind of image you have. 

imageType = cell(nImages,1);

for iImg = 1:nImages
info = imfinfo(d(iImg).name);
imageType{iImg} = info.ColorType;
end

%# isGrayscale is true for grayscale images, false otherwise 
%# (though note that there might be mapped images that map to a grayscale colormap).
isGrayscale = cellfun(@(x)strcmp(x,'grayscale'),imageType);

修改

%# the indexed images have to be loaded in order for you to check 
%# for grayscale maps

indexedIdx = find(cellfun(@(x)strcmp(x,'indexed'),imageType));

for iImg = indexedIdx(:)'

   [~,map] = imread(fullfile(figDirectory,d(iImg).name));

   %# It's a grayscale image if rgb map values are all equal
   isGrayscale(iImg) = all(all(bsxfun(@eq,map,map(:,1)),2),1);
end

%# finally, it is possible that the images are *stored* as truecolor
%# but containing, in fact, a grayscale image

truecolorIdx = find(cellfun(@(x)strcmp(x,'truecolor'),imageType));

for iImg = truecolorIdx(:)'

   img = imread(fullfile(figDirectory,d(iImg).name));

   %# It's a grayscale image if rgb map values are all equal
   isGrayscale(iImg) = all(all(all(bsxfun(@eq,img(:,:,1),img),1),2),3);
end

答案 3 :(得分:2)

这是一个基于IMFINFO的简单解决方案:

%# some test images shipped with the Image Processing Toolbox
fNames = {
    'circles.png'   %# binary
    'shadow.tif'    %# indexed color
    'coins.png'     %# gray
    'peppers.png'   %# RGB
};

isGrayscale = false(size(fNames));
for i=1:numel(fNames)
    imgInfo = imfinfo(fNames{i});
    if strcmp(imgInfo.ColorType,'truecolor')
        isGrayscale(i) = false;
    elseif strcmp(imgInfo.ColorType,'grayscale')
        isGrayscale(i) = true;
    elseif strcmp(imgInfo.ColorType,'indexed')
            %# indexed images colormap (the three channels should be all same)
        isGrayscale(i) = all(all( diff(imgInfo.Colormap,[],2)==0 ,2),1);
    end
end

您案例的第一部分可以是:

dirName = 'C:\path\to\myimages';
files = dir( fullfile(dirName,'*.jpg') );
fNames = {files.name}';

编辑:

@Adrian:关于你提出的图像,就保存的图像格式而言,它是一个彩色图像。现在所有R / G / B通道完全相同的事实只是一个特例......

无论如何,如果您希望能够检测到这种情况,请将上述代码的'truecolor'部分更改为:

#% ...
if strcmp(imgInfo.ColorType,'truecolor')
    img = imread(fNames{i});
    isGrayscale(i) = isequal(img(:,:,1),img(:,:,2),img(:,:,3));
elseif strcmp(imgInfo.ColorType,'grayscale')
#% ...