how to convert index image into rgb image
Show older comments
Answers (2)
Image Analyst
on 4 Jun 2025
When you did this:
[X1,cmap]=imread('c09_1.tif');
Did you check the value of cmap? It's probably empty and so passing it to ind2rgb will give an error. Make sure you set cmap to something valid and then call it
cmap = hsv(256); % Construct valid colormap.
rgbImage = ind2rgb(X1, cmap); % Now you can use the valid cmap.
Deepak
on 4 Jun 2025
I understand that you are trying to convert an indexed image to an RGB image using "ind2rgb", but you are encountering an error related to array bounds. This typically happens when the image X1 contains index values that exceed the size of the colormap "cmap".
To resolve this, ensure that the data in "X1" is of type uint8 or uint16 and that all its values are valid indices within the colormap (i.e., less than or equal to size(cmap,1)).
Below is a sample MATLAB code to fix the issue:
[X1, cmap] = imread('c09_1.tif');
% Ensure X1 is in the correct range for the colormap
if max(X1(:)) > size(cmap,1)
error('Image contains indices exceeding the colormap size.');
end
% Convert to RGB
RGB1 = ind2rgb(X1, cmap);
If X1 is of type double, make sure it contains integer indices starting from 1. If the image data has been shifted or scaled (e.g., from 0-based indexing), adjust it accordingly:
X1 = double(X1) + 1; % if original indices start from 0
Please find attached the documentation of functions used for reference:
I hope this helps in resolving the issue.
Categories
Find more on Convert Image Type 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!