how to get RGB data from image object generated by imagesc()?

26 vues (au cours des 30 derniers jours)
Brahad Kokad
Brahad Kokad le 29 Mar 2023
Modifié(e) : DGM le 29 Mar 2023
h = imagesc(X);
X is a 2d array of double. h returns an image object which is a rgb colormap weighted by data in X. How can I extract the RGB information from h and store it in a 3d array (of say double)?

Réponses (3)

Brahad Kokad
Brahad Kokad le 29 Mar 2023
Modifié(e) : Brahad Kokad le 29 Mar 2023
this won't work because rgb_data is same for all 3 channels, thus it'll result in a grayscale image
% create the image object using imagesc
h = imagesc(X);
% extract the RGB data from the image object
rgb_data = get(h, 'CData');
% add a third dimension to the RGB data to hold the color channel information
rgb_data_3d = cat(3, rgb_data, rgb_data, rgb_data);
% display the size of the 3D RGB array
disp(size(rgb_data_3d));

Image Analyst
Image Analyst le 29 Mar 2023
rgbImage = ind2rgb(X, colorMap);

DGM
DGM le 29 Mar 2023
Modifié(e) : DGM le 29 Mar 2023
I think it's arguable that the purpose of imagesc() involves the handling of arbitrarily-scaled data as images. On that assumption, there's no reason to expect that the working image/data will be suitable to be treated as an indexed image directly. Similarly, there's nothing about the transformation which would perform the colormapping based on the data extrema. Contrary to the goal of emulating imagesc() with its 'scaled' CDataMapping, using ind2rgb() is akin to using image() with its default 'direct' CDataMapping.
If you want to emulate the behavior of imagesc(), you would need to normalize and quantize your image/data according to the following expectations:
  1. the mapping corresponds to image extrema (this satisfies the 'scaled' CDataMapping)
  2. the quantization corresponds to the length of the colormap (necessary treated as an indexed image)
Consider the example:
% an image which does not span the full range of an integer class
% or arbitrarily-scaled float data
inpict = imread('pout.tif'); % grayscale, uint8
inrange = imrange(inpict) % data does not span [0 255]
inrange = 1×2
74 224
% a colormap
nlevels = 256;
CT = parula(nlevels);
% the expected output
imagesc(inpict)
colormap(CT)
axis image
% ind2rgb() expects its input to be integer-valued
% and scaled with respect to the length of the specified colormap
% in other words, the image is a literal map of indices into the colormap
% imagine what would happen if your data were in unit-scale
% or if your colormap only had 64 entries
outpict = ind2rgb(inpict,CT); % apply colormap without scaling
imshow(outpict)
% if you want to treat your data as an indexed image
% you have to actually make sure that the data
% is scaled and quantized appropriately
outpict = gray2ind(mat2gray(inpict),nlevels); % normalize & quantize
outpict = ind2rgb(outpict,CT); % apply colormap
imshow(outpict)

Catégories

En savoir plus sur Red dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by