How to add colorbar using image data

How can I add colorbar using the colors represented in the attached image (imagesr.png)? Thanks!

Answers (1)

I would guess maybe,
colormap parula
However, we really need to see the corresponding grayscale image to make an informed choice.

15 Comments

You would need
colorbar
after the colormap call.
@Matt J how to extract colormap information using grayscale image?
Because we don't know, from your posted image, which regions are supposed to be bright and which are dark.
You cannot extract the colormap information from a grayscale image.
If you had a grayscale image and the corresponding colored image, then you could attempted to extract a colormap
GID = 1 + double(GrayImage(:));
CID = reshape(double(ColorImage),[],3);
cmap_red = accumarray(GID,CID(:,1),[256],@mean);
cmap_green = accumarray(GID,CID(:,2),[256],@mean);
cmap_blue = accumarray(GID,CID(:,3),[256],@mean);
cmap = uint8([cmap_red, cmap_green, cmap_blue]);
@Matt J Here is the grayscale image
That's not a grayscale image. That's a color image of a grayscale image.
It is moot in any case what you post for us. @Walter Roberson has given you code to estimate the colormap in his last comment.
I might code @Walter Roberson's solution slightly differently,
GID = round( rescale( GrayImage(:),1,256 ));
CID = reshape(double(ColorImage),[],3);
if any(CID(:)>1), CID=CID/255; end
cmap_red = accumarray(GID,CID(:,1),[256],@mean,nan);
cmap_green = accumarray(GID,CID(:,2),[256],@mean,nan);
cmap_blue = accumarray(GID,CID(:,3),[256],@mean,nan);
cmap = fillmissing( [cmap_red, cmap_green, cmap_blue],'linear' ,1);
colormap(cmap); colorbar
@Matt J Once I have grayscale image and cmap, how can I represent that cmap to plot colorbar on the side of the image? Thanks
colormap(cmap); colorbar
You have to be careful about the range of values for the colormap. colormap() accepts an N x 3 double array in the range [0 1], or accepts an N x 3 uint8 in the range 0 to 255.
With you having done double(ColorImage) and then @mean, the values will probably be in the range 0 to 255 double precision
One possible correction would be
CID = reshape(im2double(ColorImage),[],3);
Matt J
Matt J on 27 Jan 2024
Edited: Matt J on 27 Jan 2024
With you having done double(ColorImage)
I think you mean with you having done it. :-)
But I uint8() afterwards ;-)
Hi @Walter Roberson I implemented your solution like this:
GID = 1 + double(Img_gray(:)); % Img_gray is the grayscale image
CID = reshape(double(DeepfitSR),[],3); % DeepfitSR is the rgb image
cmap_red = accumarray(GID, CID(:,1), [256, 1], @mean);
cmap_green = accumarray(GID,CID(:,2),[256, 1],@mean);
cmap_blue = accumarray(GID,CID(:,3),[256, 1],@mean);
cmap = uint8([cmap_red, cmap_green, cmap_blue]);
imagesc(Img_gray);
hold on
colormap(cmap); colorbar
But I got following error
Error using colormap (line 99)
Colormap must have values in [0,1].
But I uint8() afterwards ;-)
Yes, that's true. I have modified my version accordingly.
load Images
GID = round( rescale( GrayImage(:),1,256 ));
CID = reshape(double(ColorImage),[],3);
if any(CID(:)>1), CID=CID/255; end
cmap_red = accumarray(GID,CID(:,1),[256,1],@mean,nan);
cmap_green = accumarray(GID,CID(:,2),[256,1],@mean,nan);
cmap_blue = accumarray(GID,CID(:,3),[256,1],@mean,nan);
cmap = fillmissing( [cmap_red, cmap_green, cmap_blue],'linear' ,1);
close all
tiledlayout(3,1);
nexttile; imshow(ColorImage);
nexttile; imshow(GrayImage, colormap(gray(256)));
nexttile; imshow(GrayImage, cmap); colorbar
DGM
DGM on 28 Jan 2024
Edited: DGM on 28 Jan 2024
There are multiple problems here. The two images aren't the same size, and they don't seem to be registered, so simply resizing them won't help.
Let's say you resized them anyway and ignored all the error caused by misregistration. I imagine this isn't an acceptable result:
Note how much color has simply been lost. Given the span of the color cloud, I have to question whether this was generated using a simple linear colormapping process. At the very least, it's not meaningful to reduce it to a 1D colormap. It looks more like it's a 2D mapping of some sort. If it is a 2D mapping, then I have to suspect that it's based on more information than the single grayscale image that's been given.
Is the image the process of any colormapping process, or is it just some sort of composite image? If that's the case, why have a colorbar at all?
The colored image is larger and blurrier than the source grayscale image. Maybe it's a screenshot? Either way, if the grayscale image is the source image, then we should be working from that instead of some mystery degraded copy, the meaning of which nobody understands yet. If you know where the colored copy came from or how it was created, now would be the time to tell us.

Sign in to comment.

Asked:

on 27 Jan 2024

Edited:

DGM
on 28 Jan 2024

Community Treasure Hunt

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

Start Hunting!