how to know the present colormap(eg: hsv, rgb etc..,)

7 views (last 30 days)
how to determine which colormap is being used and how to know in which format(rgb/hsv etc..,) the image has been read into matlab?? ps: thanks in adavance

Accepted Answer

Walter Roberson
Walter Roberson on 1 Jun 2013
1)
To determine which colormap is being used, use
num_entries = size(colormap(),1);
to determine the number of entries in the map. Then loop over all the named colormap functions and generate the num_entries map using that function, and compare it to the current colormap. If they are the same to within roundoff then you have found the colormap.
Note: colormap() does not keep track of the name of the map it has been called on. If you use the functional form of colormap instead of the command form, then colormap() cannot even find out.
The named maps are not static values: they are all functions that return a value when called. For example,
colormap(pink)
is really
T = pink();
colormap(T);
It is easy to set the active colormap to something that has never been named. For example,
colormap(sortrows(rand(20,3)))
Sample code:
known_maps = {@autumn, @bone, @colorcube, @cool, @copper, @flag, @gray, @hot, @hsv, @jet, @lines, @pink, @prism, @spring, @summer, @white, @winter};
curmap = colormap();
N = size(curmap,1);
found_map = false;
for K = 1 : length(known_maps)
this_map_fcn = known_maps{K};
this_map = this_map_fcn(N);
if all( (this_map(:) - curmap(:)) < 1e-5 )
disp(this_map_fcn);
found_map = true;
break;
end
end
if ~found_map
disp('unknown color map');
end
2) To determine which format a read-in image is in:
if isa(TheImage, 'logical')
disp('black and white image')
elseif ndims(TheImage) > 3
disp('CMYK TIFF image');
elseif ndims(TheImage) < 3
disp('grayscale or pseudocolor image');
else
disp('Not able to determine image color representation');
end
Notice that RGB vs HSV falls into "not able to determine".
If, in addition, the filename of the image is known, then:
dotpos = find(ImageName == '.', 1, 'last');
if isempty(dotpos)
disp('No file extension, not able to determine file format')
else
ext = lower(ImageName(dotpos+1:end));
switch ext
case {'bmp', 'cur', 'gif', 'hdf', 'hdf4', 'ico', 'jpg', 'jpeg', 'jpeg2000', 'jp2', 'jp2k', 'pbm', 'pcx', 'pgm', 'png', 'ppm', 'ras', 'xwd'}
disp('RGB');
case {'tif', 'tiff'}
info = [];
try
info = imfinfo(ImageName);
catch ME:
end
if ~isstruct(info)
disp('corrupt or missing TIF file')
elseif isfield(info, 'PhotometricInterpretation') &&
~isempty(info(1).PhotometricInterpretation )
%RGB, CMYK, CIELAB, ICCLAB
disp(info(1).PhotometricInterpretation);
else
disp('RGB')
end
otherwise
disp('Unknown file extension, not able to determine file format')
end
end
If you follow this code closely, you will note that HSV is not a possibility for any of the cases where it is possible to determine the format. None of the supported image formats allow for HSV. Any HSV data you have must have been computed (instead of being read in from an image), or else must have been loaded from a .mat or text file, or else must have been custom-loaded from a binary file.
I probably could have shorted this entire section to just say that "Your images are never stored in HSV", but that would have been marginally inaccurate, and it would not reflect what you actually asked, only what you probably meant.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!