How to display an image using Imagesc or Imshow with only a part of the colorscale?

27 visualizaciones (últimos 30 días)
I have an X by Y matrix filled with values from 0 to 255 and I want to create a figure with only a part of the grayscale (for example from 100 to 150) so that the program doesn't automatically rescale the 100 to be black and 150 as white. Attached file is the full 0 to 255 image that I need to extract from. Any tips regarding this?

Respuesta aceptada

Atsushi Ueno
Atsushi Ueno el 12 de Sept. de 2021
I finally understand your expectation. What you want is not setting colormap limits but creating a custom colormap.
[map,~] = meshgrid(1:400, 1:220); map = uint8(map.*255./400);
clmp = (100/255:50/255^2:150/255)'; % <<<=== this vector is for making a custom colormap.
figure; imagesc(map); colormap([clmp clmp clmp]); colorbar; % <<<=== it works for imagesc function
  3 comentarios
Filip Juchnicki
Filip Juchnicki el 13 de Sept. de 2021
Okay I will try my best to clarify. The end result needs to be a new figure identical to the original in size BUT with the colors starting and ending with the values from a certain range (example is 100 to 150 but I need it as a variable). My problem is that Imagesc always rescales the color values so that the lowest value is black and the highest is white. I need a workaround so that I can create a matrix that gives similar resulting figure to the above image but the color needs to depend on the original matrix - If the original gives some shade of gray at min = 100 - this is the shade I need the new one to start with. And if I set the max = 150 the shade of the original at 150 is the one I need the new figure to end with. I used the word "extract" because the new figure needs to depend on the values from the original.
Image Analyst
Image Analyst el 13 de Sept. de 2021
@Filip Juchnicki, please note that that is exactly what my answer below (the one you did not accept) does. If your data is uint8, you merely need to do
imshow(yourImage); % If image is uint8
If your image is double and goes between some arbitrary starting value, say -1000 and some arbitrary ending value, say 3000, and you want values in the 0-255 range to stay the color they are, then you merely need to cast the image to uint8 with the uint8() function:
imshow(uint8(yourDbouleImage)); % If image is double
In this case, values from 255 to 3000 will show up as white, and values from -1000 to 0 will show up as black (since there is no negative intensity and your screen only understands values from 0-255).

Iniciar sesión para comentar.

Más respuestas (3)

Image Analyst
Image Analyst el 12 de Sept. de 2021
Editada: Image Analyst el 12 de Sept. de 2021
You can get a mask for gray levels in that range
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
fprintf('Beginning to run %s.m ...\n', mfilename)
grayImage = uint8(repmat([0:255], 255, 1));
subplot(1, 2, 1);
imshow(grayImage, 'Colormap', gray(256));
colorbar;
axis('on', 'image');
title('All Gray Levels 0-255', 'FontSize', 20);
% Get mask of the desired range of gray levels from original image.
mask = grayImage >= 100 & grayImage <= 150;
% Erase everything outside this range.
maskedImage = grayImage; % Initialize
maskedImage(~mask) = 0; % Whatever gray level you want outside of the desired range.
subplot(1, 2, 2);
cmap = gray(256);
% Blacken 0-99 and 151-255
cmap([1:100,152:256], :) = 0;
imshow(maskedImage, 'Colormap', cmap);
axis('on', 'image');
colorbar;
title('Only Gray Levels 100-150', 'FontSize', 20);
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m.\n', mfilename)
impixelinfo
Note in the right image that the gray levels of 100 show up in gray level of 100, not 0, and gray levels of 150 show up as 150, not 255.
  2 comentarios
Filip Juchnicki
Filip Juchnicki el 12 de Sept. de 2021
Editada: Filip Juchnicki el 12 de Sept. de 2021
I realised I maybe didn't articulate what I need well enaugh as it is difficult to describe. What I want to achieve is the matrix that gives a figure like the one I attached but so that the scale doesn't start with a black color and end with white but instead starts and ends with a certain shade of gray described by a variable. For example when I specify the range of 100-150 it means that the image would have to be a gradient of dark gray (100 on the original figure) to lighter gray (the shade corresponding to 150 on the original figure as well). Sorry for the confusing description.
Image Analyst
Image Analyst el 12 de Sept. de 2021
Simply make this change to the colormap:
cmap([1:100,152:256], :) = []; % Instead of 0

Iniciar sesión para comentar.


Atsushi Ueno
Atsushi Ueno el 12 de Sept. de 2021
Editada: Atsushi Ueno el 12 de Sept. de 2021
[map,~] = meshgrid(1:400, 1:220); map = uint8(map.*255./400);
figure; imagesc(map); colormap('gray'); colorbar; % <<<=== it works for imagesc function
caxis([100 150]); % <<<=== this one
  2 comentarios
Image Analyst
Image Analyst el 12 de Sept. de 2021
Note that he said he doesn't want to "automatically rescale the 100 to be black and 150 as white." as your program does.

Iniciar sesión para comentar.


Atsushi Ueno
Atsushi Ueno el 12 de Sept. de 2021
I understand your expectation. My answer above is what you don't want to be like.
[map,~] = meshgrid(1:400, 1:220); map = uint8(map.*255./400);
figure; imagesc(map); colormap('gray'); colorbar; % <<<=== it works for imagesc function
caxis([-100/50*255 (255-100)/50*255]); % <<<=== (value-100)/50*255

Categorías

Más información sobre Red en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by