How do I get a colormap for my image?
59 views (last 30 days)
Show older comments
When I import my image using the imread function, I get a map with only zeros (map = []). I want the RGB components of the image, but cannot get them since this requires the picture's colormap (which is empty). This is the code I'm using:
filename = uigetfile('*.*');
[X, map] = imread(filename);
info = imfinfo(filename)
width = getfield(imfinfo(filename),'Width')
height = getfield(imfinfo(filename),'Height')
figure('Name','Display imported image','NumberTitle','off');
image(X);
rgb = ind2rgb(X,map);
The info of the image is:
info =
Filename: '11-11-11 (30).JPG'
FileModDate: '11-Nov-2011 21:07:46'
FileSize: 4549123
Format: 'jpg'
FormatVersion: ''
Width: 4320
Height: 2432
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: ''
NumberOfSamples: 3
CodingMethod: 'Huffman'
CodingProcess: 'Sequential'
Comment: {}
ImageDescription: ' '
Make: 'SONY '
Model: 'DSC-W350 '
Orientation: 1
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
DateTime: '2011:11:11 21:07:46 '
YCbCrPositioning: 'Co-sited'
DigitalCamera: [1x1 struct]
UnknownTags: [1x1 struct]
ExifThumbnail: [1x1 struct]
And then I get this error message:
??? Index exceeds matrix dimensions.
Error in ==> ind2rgb at 27
r = zeros(size(a)); r(:) = cm(a,1);
Error in ==> DSP_LCK at 24
rgb = ind2rgb(X,map);
Some advice please?
0 Comments
Accepted Answer
Alex Taylor
on 15 May 2012
The metadata associated with your image is indicating that your data is already an RGB image with separate RGB planes:
ColorType: 'truecolor'
NumberOfSamples: 3
ind2rgb is failing because you are providing input that is already an RGB image (and is not an indexed image).
If you want the RGB components:
R = X(:,:,1);
G = X(:,:,2);
B = X(:,:,3);
5 Comments
More Answers (5)
Image Analyst
on 15 May 2012
Try this demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format compact;
fontSize = 22;
%------------------------------------------------
% Ask user for image file.
% Initialize with some folder to start browsing from.
startingFolder = 'C:/Users/Louis/Pictures';
% Get the name of the color image file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
%------------------------------------------------
% Read in the image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
%------------------------------------------------
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
%------------------------------------------------
% Display the individual color channels.
subplot(2, 2, 2);
imshow(redChannel, []);
title('Red Channel Image', 'FontSize', fontSize);
subplot(2, 2, 3);
imshow(greenChannel, []);
title('Green Channel Image', 'FontSize', fontSize);
subplot(2, 2, 4);
imshow(blueChannel, []);
title('Blue Channel Image', 'FontSize', fontSize);
3 Comments
Image Analyst
on 16 May 2012
Sorry, but you are wrong. There is no mixing of the color channels. Each color channel is a gray scale image, not a color image, just in case you were incorrectly thinking that if you display the redChannel it should appear red. It's just a 2D array of numbers. Think about it.
If you want each color channel to appear in "it's own" color, then you have to apply a pseudocolor look up table with the colormap() function. But that just affects the display only, not the underlying image, which is still just a 2D monochrome array of numbers.
Michael Barrow
on 15 May 2018
Got here by googling how to read in a colormap for an image that does not have one. My goal was to import an indexed image directly as I needed a colormap to texture a 3D mesh.
The solution for me was to re-save the image in indexed mode using an external tool (gimp, an open source image editor).
When I used [X,map]=imread('newfile.png'); I had a usable map that allowed me to colour my mesh
1 Comment
DGM
on 9 Jul 2024
To avoid having to touch disk or use external tools, you could always use rgb2ind().
% a RGB image
rgbpict = imread('peppers.png');
% convert to indexed color using whatever settings you want
% most formats which support indexed output should support up to 256
[indpict CT] = rgb2ind(rgbpict,256);
imshow(indpict,CT)
xlim([200 500]); ylim([100 300]) % zoom in
% try a shorter table to help visualize the quantizaton
% the default is to use error-diffusion dithering
[indpict CT] = rgb2ind(rgbpict,16);
imshow(indpict,CT)
xlim([200 500]); ylim([100 300])
% but the dithering can be disabled
[indpict CT] = rgb2ind(rgbpict,16,'nodither');
imshow(indpict,CT)
xlim([200 500]); ylim([100 300])
... though it can't do a patterned dither, so if that's something you want, using external tools would make sense.
Louis Kok
on 16 May 2012
2 Comments
Image Analyst
on 16 May 2012
Can you make it easy and just post the image so we can see it immediately, and not something I have to download, extract, then load into MATLAB?
See Also
Categories
Find more on Colormaps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!