how to display a grayscale image in a monochrome color (other than white)

29 vues (au cours des 30 derniers jours)
srt10
srt10 le 18 Déc 2023
Modifié(e) : DGM le 19 Déc 2023
I have a grayscale image consisting of a matrix of 512x512 int16 numbers. If I run
figure;imshow(imagedata,[])
I can see my contrast adjusted image. Now all I want to do is to display this image by replacing white with green. I do not want to change the datatype to double or do something that would mess with the original data.
My first thought was to just look at the colormap and ideally that should look like
[0 0 0]
[1 1 1]
....
[255 255 255]
I thought I could just change that to be
[0 0 0]
[0 1 0]
.....
[0 255 0]
but when I take a look at the colormap of this grayscale image,
map = colormap;
the colormap has non-zero numbers (which are not equal to each other) in each of the r,g and b columns and so I don't understand this. The grayscale colormap is supposed to have the same numbers in the r,g and b columns.
All I want to do is to tell matlab that the highest number maps to green instead of white. What is the easiest way of doing this?

Réponse acceptée

srt10
srt10 le 18 Déc 2023
After much searching, I found the answer to my question. There is a function called
cmap2gray
that was introduced in R2020b (and I was using R2020a so I didn't find it right away).
The map which is output when you use the colormap function on a grayscale image is an "RGB colormap" and can be converted to a "grayscale colormap" by running
newmap = cmap2gray(map)
The 3 columns of newmap are identical (which is what you would expect). Now, I can easily convert my grayscale image which is originally black to white, to black to green by zeroing out the R and B columns in the newmap variable.
I can then apply this corrected "grayscale colormap" to my grayscale image to get it to display in green.
  4 commentaires
John D'Errico
John D'Errico le 19 Déc 2023
Voss spent a great deal of effort in explaining it, over and over again. Then you decided to post an answer that effectively does something very similar, while claiming that Voss did not even make an effort to answer your question.
DGM
DGM le 19 Déc 2023
Modifié(e) : DGM le 19 Déc 2023
Using cmap2gray() is not appropriate here, and the results it returns may not be linear or possibly not even monotonic. We'll get to that.
Voss is correct in that the colormap() function does not return any information about an image, but I disagree with the use in the example. It does one of two things. It can either be used to specify the colormap for a given axes or figure (e.g. the current axes), or it can be used to retrieve the current colormap from a figure or axes.
When you use colormap() to set a colormap without specifying a target axes or figure, it sets the colormap of the current figure, which in turn also sets the colormap of the descendant axes. When you call colormap() with an output argument, you're just getting whatever the current colormap is. As before, if you do so with no input argument, the default target is the current figure.
So what is the current colormap used by imshow()? Well, if you call imshow() with a single-channel image with the following syntax:
imshow(grayimage) % default scaling or
imshow(grayimage,[]) % scaled to data extrema
then imshow() will set the axes colormap to gray(256), and CDataMapping is set to 'scaled'. This is an example of scaled colormapping.
If you call imshow() like so instead:
imshow(indexedimage,colortable) % an indexed-color image
then imshow will set the axes colormap to the specified colortable, and CDataMapping to 'direct'. This is an example of direct colormapping.
In both of these instances, we're talking about the current axes colormap. Remember, the thing that you get when you call colormap() like this
CT = colormap(); % get the colormap for GCF
is the current figure colormap.
So what is the current figure colormap? That's hard to say. It could be any number of things if the figure has been used for other things prior. Chances are it's still the default colormap, which is parula(256).
What does cmap2gray() do? This tool does the same BT601 luma transformation that rgb2gray() and im2gray() do; the only difference is that it's tailored to accept inputs as Mx3 unit-scale colormaps, and it expands the output so that it stays Mx3.
What happens if you process one of the typical colormaps with this tool? Well, since none of the standard colormaps are linear and full-scale in BT601 luma (except gray()), the result will not be a linear grayscale ramp.
% a single-channel grayscale image
% a full-scale sweep from black-white
graypict = repmat(linspace(0,1,200),[100 1]);
% a colormap
CT = parula(256);
% display the image, rendered with the specified map
imshow(graypict)
colormap(CT)
% convert the colormap to gray
CT = cmap2gray(CT);
% display the image, rendered with the specified map
subplot(2,1,1)
imshow(graypict)
colormap(CT)
% plot the luma
subplot(2,1,2)
plot(CT(:,1))
ylim([0 1])
As you can see, the resulting map is not full-scale, linear, or monotonic. It's not really useful for much of anything. So why does cmap2gray() even exist? The example given in the documentation is pretty much it. It's useful for reducing the color tables associated with indexed-color images. That's not your application.
So if you wanted a gray colormap to modify, how would you get it? You'd use gray().
mycolormap = gray(256); % a gray colormap
mycolormap(:,[1 3]) = 0; % make it green
% display the image with the custom map
imshow(graypict,[]) % scaled to data extrema
colormap(mycolormap) % use the new map
Although creating colormaps by editing gray() is limited and cumbersome. A more useful and convenient approach would be something like:
CT0 = [0 0 0; 0 1 0.5]; % pick any endpoint colors you want
N = 256; % specify the colortable length
% generate a custom colormap
CT = interp1([0 1],CT0,linspace(0,1,N));

Connectez-vous pour commenter.

Plus de réponses (1)

Voss
Voss le 18 Déc 2023
Like this?
imagedata = randi([0,65535],512,512,'int16'); % random 512-by-512 int16 matrix
figure; imshow(imagedata,[])
cmap = colormap();
cmap(:,[1 3]) = 0;
colormap(cmap)
  14 commentaires
Voss
Voss le 18 Déc 2023
I think you are wrong about that, too.
srt10
srt10 le 18 Déc 2023
thanks for your opinion. It was very helpful. Just like your contribution to this question

Connectez-vous pour commenter.

Catégories

En savoir plus sur Orange dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by