How to convert a grayscale indexed image into a RGB indexed image ?

12 views (last 30 days)
I am using NIR and Visible Red images to calculate a NDVI for my project.
What i am getting from this is a grayscale indexed image and what i am trying to do is convert this grayscale to RGB without losing the indexed points.
this anexed image can show explain better:
I need to maintain the Index in the RGB image after conversion. Is it possible to do that ?
If yes, how can i do it ?
I tried to do with 2 different approachs and i lost the index information both times.
RGB = cat(3, ndvi, ndvi, ndvi);
and
[indexedImage, ~] = imread(fullFileName); % read the ndvi file
RGB = ind2rgb(indexedImage,jet);

Accepted Answer

DGM
DGM on 10 Oct 2023
Edited: DGM on 10 Oct 2023
There are basically three ways an image can be rendered in MATLAB.
You can have a MxNx3 RGB truecolor image. That's fairly straightforward. The values in the array are literal RGB component values that lie in a particular interval implied by the numeric class (e.g. 0-255 for uint8, or 0-1 for float classes).
You can have a MxN index array and a Px3 colortable/colormap. The elements in the index array are integer values which represent specific rows in the colortable. In MATLAB terminology, this is referred to as "direct" color data mapping. This is what I would call an "indexed color image". This is how an image is stored in a GIF file or an indexed PNG.
You can have any MxN array. This may be in the aforementioned class-dependent ranges, or it may be in any arbitrary scale, depending on how they're displayed. This is because grayscale images are rendered in pseudocolor (or gray) using either a default or user-specified colormap. Unlike the prior case, the relationship between the values in the array and the rows of the colormap/table are determined by the color axis limits (see caxis()/clim()). This is what is referred to as "scaled" color data mapping.
In this case with "scaled" CDataMapping, the values are not direct indices into the colormap. This isn't what I'd call an "indexed" image, and I suggest that the manner in which the datatips label the values as "Index" can be misleading. Those are just the values in the array. The actual row index they represent is something that depends on the limits of the color axis.
Based on those three cases:
You can convert a MxN gray image into an RGB image, but its values will necessarily be shifted and rescaled to the standard data ranges in order for them to display properly. In all typical cases, they will become integer-valued. That's not what you want.
You can convert the same gray image to an indexed image, but again, its values will become integer indices. That's also not what you wanted either.
I don't know of an image format which supports arbitrarily-scaled float data, an embedded colormap/colortable, and embedded color data scaling information.
Consider that it's probably not a great idea to try to do the thing you're trying to do. Just as graphs and charts are visualizations of data, not stores of data, pseudocolor images should be treated as visual conveniences, not stores of data. In my opinion, you would be better off trying to create whatever pseudocolor representation you want, but keep your MxN data unadulterated.
Regarding converting the image into a pseudocolor RGB image, see:

More Answers (1)

Image Analyst
Image Analyst on 10 Oct 2023
There is no such thing as an RGB indexed image.
You can convert your indexed image into an RGB image with
rgbImage = ind2rgb(indexedImage, yourColorMap);
Once converted, the RGB image will not have any "index" information. However it is not lost because you still have it in the indexedImage variable.
However if you want to retain the white box with info in it, you must save that first and then paste it over the RGB image after conversion. You can do this with drawrectangle. See attached demos.
If you have hundreds of these (doubtful) then it can be automated in a number of ways.
If you want to mouse around and see the index, then maybe you can just call colormap so the underlying image in the axes is still indexed but just appears pseudocolored.

Categories

Find more on Modify Image Colors 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!