How do I get a colormap for my image?

112 views (last 30 days)
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?

Accepted Answer

Alex Taylor
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
Louis Kok
Louis Kok on 16 May 2012
Sorry, the "Accept" button was hit on accident!
Louis Kok
Louis Kok on 16 May 2012
Thanks Walter, this was more of what I had in mind to just display each image only in a red, green and blue window. I will try this and see if this solves my problem.

Sign in to comment.

More Answers (5)

Image Analyst
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
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.
Louis Kok
Louis Kok on 16 May 2012
Thank you Analyst, I agree with you that the underlying image is still a 2D array of numbers. I got confused when I displayed only the redChannel as I did, and got only that plane's data displayed. What I'm actually interested in, is the resulting images to display only the red, green and blue color components of my RGB image. I have to do edge detection (Sobel's operator) on each of these three monochrome images -- which originates from the original RGB image.

Sign in to comment.


Michael Barrow
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

Louis Kok
Louis Kok on 16 May 2012
Here is a link to the image I'm referring to. Thank you for all your help and insight!!
  2 Comments
Image Analyst
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?
Louis Kok
Louis Kok on 16 May 2012
Sorry for the inconvenience! I'm having trouble only posting the image as it is, but I can give and example of what I'm expecting to get from this image processing in Matlab. Here is a link to a combined image of the results I'm expecting.
http://dl.dropbox.com/u/66925667/results.jpg
F.l.t.r. The original RGB image, the green image, a gray-scale image, the red and blue images as well as a gray-scaled image that has been passed through a low-pass filter after taking the fft of that gray-scale image.
A friend of mine sent me all these images, but he processed them from C++ software in Qt-environment.
I aim to produce these type of images in Matlab, since I also need to do edge detection on each of the three fundamental color images as well as to filter out high frequency components from the gray-scale image after applying an FFT algorithm. The catch is, I'm not allowed to use Matlab's FFT() functions or edge detection algorithms, I have to code them myself. I'm progressing slowly, since I'm having trouble to get these fundamental color images. I am able to get the gray-scale image of the original RGB image.
Thanks a lot for your insight, help and tips!! My apologies for the file download schlep.

Sign in to comment.


Louis Kok
Louis Kok on 16 May 2012
Walter, I've implemented your suggestion and it works perfectly fine as is!
Thanks a lot for all your contributions: Alex, Image Analyst and Walter. I've learned a lot about RGB images, some properties of RGB image processing we're not learning about on varsity as well as new coding techniques and tools I now have to create better algorithms for my practicals and assignments! I kind of feel stupid not thinking to zero-pad the color planes I do not want.
My apologies also for incompetent questions and statements regarding image processing. This semester is our firs DSP course and we've tackled mostly the math behind CTFT, DTFT, DFT and a quick overview of the FFT and the accompanying background foundation for DSP. Color image processing was the assignment and mostly self-study.

Louis Kok
Louis Kok on 16 May 2012
This is my derived and modified solution:
[filename filepath] = 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);
R = X(:,:,1);
G = X(:,:,2);
B = X(:,:,3);
Rimg = cat(3, R, zeros(size(R)), zeros(size(R)));
Gimg = cat(3, zeros(size(G)), G, zeros(size(G)));
Bimg = cat(3, zeros(size(B)), zeros(size(B)), B);
figure('Name','RED component of the imported image','NumberTitle','off');
image(Rimg);
figure('Name','GREEN component of the imported image','NumberTitle','off');
image(Gimg);
figure('Name','BLUE component of the imported image','NumberTitle','off');
image(Bimg);
G = rgb2gray(X);
figure('Name','Gray-scale image of the imported image','NumberTitle','off');
imshow(G);
Again, thanks a lot to everyone contributing!

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!