how to convert index image into rgb image

10 views (last 30 days)
how to convert index image into rgb image
  1 Comment
kamarthi vanitha
kamarthi vanitha on 6 Jul 2019
if i use [X1,cmap]=imread('c09_1.tif');
RGB1 = ind2rgb(X1,cmap);I am getting error as:
Index in position 1 exceeds array bounds.
Error in ind2rgb (line 26)
r = zeros(size(a)); r(:) = cm(a,1);
Error in Script_gray (line 15)
RGB1 = ind2rgb(X1,cmap);
please fix this error

Sign in to comment.

Answers (2)

Image Analyst
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
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.

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!