How to convert multiple images from RGB to Grayscale or Index, Grayscale to RGB or Index and Index to RGB orGrayscale having a single function in use?

3 views (last 30 days)
I want to create a function which can convert multiple images into any desired image types .
  5 Comments

Sign in to comment.

Answers (2)

DGM
DGM on 1 Nov 2022
Edited: DGM on 1 Nov 2022
Since it's up to me to decide what you want, this must be it.
pathlist = {'peppers.png'; 'cameraman.tif'; 'canoe.tif'};
outputprefix = 'outputfile';
outputsuffix = '.png';
outputcolortype = 'indexed'; % 'rgb','indexed','gray'
% process the specified files
blindfileconverter(pathlist,outputprefix,outputsuffix,outputcolortype)
% a totally half-baked function that will probably break
function blindfileconverter(pathlist,outputprefix,outputsuffix,outputcolortype)
% BLINDFILECONVERTER(PATHLIST, PREFIX, SUFFIX, COLORTYPE)
% Convert a bunch of files from color type to another.
% Write all the files using a common filetype and naming scheme.
% This will strip alpha content from anything with it.
% Not all color options work with all output filetypes.
%
% PATHLIST is a cell array of char vectors specifying the files to load
% PREFIX is the output filename prefix
% (e.g. '/thisisthe/placeiwant/myimage')
% SUFFIX is the output filename suffix
% (e.g. '.png')
% COLORTYPE is the color type of the output file
% Supported: 'gray', 'rgb', or 'indexed'
defaultctlength = 256; % for indexed outputs
for k = 1:numel(pathlist)
% read the file, discarding unattached alpha
[inpict,map,~] = imread(pathlist{k});
% skip any multiframe images
if size(inpict,4) ~= 1
warning('image %s is multiframe; skipping',pathlist{k})
continue;
end
% if alpha is present, it may be attached or separate
% since there's no universal way to handle it for all possible outputs
% discard alpha if attached, ignore separate alpha
if any(size(inpict,3) == [2 4])
inpict = inpict(:,:,1:end-1);
end
% image depth should now be either 1 or 3
if ~any(size(inpict,3) == [1 3])
warning('image %s is not gray, indexed, or RGB; skipping',pathlist{k})
continue;
end
% determine if image is indexed
isindexed = ~isempty(map);
switch lower(outputcolortype)
case 'gray'
% if image is indexed
if isindexed
inpict = ind2rgb(inpict,map);
end
% if image is RGB
if size(inpict,3) == 3
inpict = rgb2gray(inpict);
end
case 'rgb'
% if image is indexed
if isindexed
inpict = ind2rgb(inpict,map);
end
% if image is gray
if size(inpict,3) == 1
inpict = repmat(inpict,[1 1 3]);
end
case 'indexed'
% if image is gray
if ~isindexed && (size(inpict,3) == 1)
inpict = repmat(inpict,[1 1 3]);
end
% if image is RGB
if size(inpict,3) == 3
[inpict map] = rgb2ind(inpict,defaultctlength);
end
otherwise
error('unknown output color type %s',outputcolortype)
end
if strcmp(outputcolortype,'indexed')
imwrite(inpict,map,sprintf('%s_%04d%s',outputprefix,k,outputsuffix))
else
imwrite(inpict,sprintf('%s_%04d%s',outputprefix,k,outputsuffix))
end
end
end
  6 Comments
Muhammad
Muhammad on 1 Nov 2022
The images can be of any type, i.e: rgb, grayscale or indexed. Yes it is possible that images could be of multiframes. When all the images had been processed they should go to a new folder.
DGM
DGM on 2 Nov 2022
Edited: DGM on 2 Nov 2022
Again, you haven't said which files are being selected. N just specifies how many files to select. Are these some sort of numbered image sequences and N just selects the first N files in the sequence? If so, what's the naming convention?
So inputs may be multiframe. imread() supports multiframe inputs for TIFF, GIF, PNM (PBM/PGM/PPM), CUR, ICO, HDF, and SVS files, but the means by which multiframe images are identified and read differs between those formats and is currently not correctly functional for GIF except in rare cases which cannot be detected from within MATLAB. Can you rely on all your files having the same format, or is this another required addition to complexity?
If images can be multiframe, then how do you intend to specify the output filetype? Is the output for each file supposed to be inherited from the input file, or is the output filetype explicitly specified? I hope that either way the answer is TIFF, because TIFF is the only supported filetype that can handle all the requirements. The only filetypes for which imwrite() supports multiframe output are GIF, TIF, and HDF. Even if multiframe support were dropped, PNG, TIFF and RAS are the only formats that would work. I don't know how much external support you'll find for RAS. While there are multiframe specifications for PNG, imwrite() doesn't support it.
% test these file formats
suffixes = {'gif','pgm','pbm','ppm','tif','hdf', ...
'png','jpg','jp2','bmp','ras','xwd'};
A = imread('peppers.png');
[Aind map] = rgb2ind(A,256);
Agray = im2gray(A);
nsuf = numel(suffixes);
supportsIDX = false(nsuf,1);
supportsGRY = false(nsuf,1);
supportsRGB = false(nsuf,1);
for k = 1:nsuf
fname = sprintf('test.%s',suffixes{k});
% test gray
try
imwrite(Agray,fname)
[Atest maptest] = imread(fname);
supportsGRY(k) = isempty(maptest) && size(Atest,3)==1;
catch
supportsGRY(k) = false;
end
% test RGB
try
imwrite(A,fname)
[Atest maptest] = imread(fname);
supportsRGB(k) = isempty(maptest) && size(Atest,3)==3;
catch
supportsRGB(k) = false;
end
% test indexed
try
imwrite(Aind,map,fname)
[Atest maptest] = imread(fname);
supportsIDX(k) = ~isempty(maptest);
catch
supportsIDX(k) = false;
end
end
supportsALL = supportsGRY & supportsRGB & supportsIDX;
table(suffixes',supportsGRY,supportsRGB,supportsIDX,supportsALL)
ans = 12×5 table
Var1 supportsGRY supportsRGB supportsIDX supportsALL _______ ___________ ___________ ___________ ___________ {'gif'} false false true false {'pgm'} true false false false {'pbm'} true false false false {'ppm'} false true false false {'tif'} true true true true {'hdf'} false true true false {'png'} true true true true {'jpg'} true true false false {'jp2'} true true false false {'bmp'} false true true false {'ras'} true true true true {'xwd'} false false true false
So should all output be TIFF-only? If output format is inherited, how would you convert a GIF file to RGB?
As far as complexity goes, if you're asking for multiframe support and generalized handling of numbered file sequences, I hope you realize that the code will only become more complex, not less.

Sign in to comment.


Image Analyst
Image Analyst on 2 Nov 2022
See the attached demo. You can specify the input extension and output extension on lines 13 and 14 to be whatever you want. You can optionally delete the input image, if you want, so that only the desired output extension version of the image remains.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!