How to display an image using Imagesc or Imshow with only a part of the colorscale?
22 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Filip Juchnicki
am 12 Sep. 2021
Kommentiert: Image Analyst
am 13 Sep. 2021
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?
0 Kommentare
Akzeptierte Antwort
Atsushi Ueno
am 12 Sep. 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 Kommentare
Image Analyst
am 13 Sep. 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).
Weitere Antworten (3)
Image Analyst
am 12 Sep. 2021
Bearbeitet: Image Analyst
am 12 Sep. 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/736264/image.png)
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 Kommentare
Image Analyst
am 12 Sep. 2021
Simply make this change to the colormap:
cmap([1:100,152:256], :) = []; % Instead of 0
Atsushi Ueno
am 12 Sep. 2021
Bearbeitet: Atsushi Ueno
am 12 Sep. 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 Kommentare
Image Analyst
am 12 Sep. 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.
Atsushi Ueno
am 12 Sep. 2021
Atsushi Ueno
am 12 Sep. 2021
I understand your expectation. My answer above is what you don't want to be like.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/736259/image.png)
[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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Red finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!