how to convert index image into rgb image

3 ビュー (過去 30 日間)
kamarthi vanitha
kamarthi vanitha 2019 年 7 月 6 日
回答済み: Image Analyst 2025 年 6 月 4 日
how to convert index image into rgb image
  1 件のコメント
kamarthi vanitha
kamarthi vanitha 2019 年 7 月 6 日
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

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2025 年 6 月 4 日
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 2025 年 6 月 4 日
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.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by