how to convert index image into rgb image

4 vues (au cours des 30 derniers jours)
kamarthi vanitha
kamarthi vanitha le 6 Juil 2019
how to convert index image into rgb image
  1 commentaire
kamarthi vanitha
kamarthi vanitha le 6 Juil 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

Connectez-vous pour commenter.

Réponses (2)

Image Analyst
Image Analyst le 4 Juin 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 le 4 Juin 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.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by