How to extract raw data from a rgb image visualized with known colormap?

47 ビュー (過去 30 日間)
Xin Liu
Xin Liu 2022 年 10 月 24 日
編集済み: Xin Liu 2022 年 10 月 27 日
Hello, everyone,
I have a rgb image, and it was visualized with a known colormap. Now I want to extract the raw 2D matrix data rather than RGB values of this image before it was mapped to the colormap . How should I do?
Here is an example:
raw_data = rand(256,256);
figure;
imagesc(raw_data);
axis image off;
colormap(jet);
colorbar;
title('saved image');
Then I save this visualized image, names as, for example 'saved image.jpg'.
How should I retrieve 'raw_data' from 'saved_img.jpg' according to the known 'jet' colormap?
Thank you!

回答 (1 件)

Sameer Pujari
Sameer Pujari 2022 年 10 月 27 日
Hello Xin Liu
From the given info, I understand that you want to get RGB image back after you have visualised with a known colormap.
To do that you have to first get the colormap of orignal rgb image
RGB = imread('image.jpg');
[X,cmap] = rgb2ind(RGB,65536);
Then you can get the original image back using
X = ind2rgb(X,cmap);
The same can be done by using "colormap" function for the figure
RGB = imread('image.jpg');
[X,map] = rgb2ind(RGB,65536);
figure
image(X)
axis off
axis image
colormap(parula) %image will be visualized in parula colormap
colormap(cmap) %image will be visualized in orignal rgb colormap
I hope this information helps you!
  3 件のコメント
Xin Liu
Xin Liu 2022 年 10 月 27 日
編集済み: Xin Liu 2022 年 10 月 27 日
Hi Sameer,
Thank you for your code. You may misunderstand my purpose. My purpose is to get the original 2D MATRIX DATA rather than the original image. I saved this figure as a .jpg file and then I want to get the raw 2D MATRIX DATA from this .jpg file according to its colormap.
For example, a certain element in the raw MATRIX DATA is 0.5, and it was mapped to a rgb value of (0.3,0.5,0.8) in the saved .jpg file according to the 'jet' colormap. My purpose is to obtain 0.5 from the rgb value (0.3,0.5,0.8) according to the jet colormap.

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

Community Treasure Hunt

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

Start Hunting!

Translated by