how to convert index image into rgb image

조회 수: 7 (최근 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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by