How to display an image using Imagesc or Imshow with only a part of the colorscale?
27 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Atsushi Ueno
2021-9-12
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 个评论
Image Analyst
2021-9-13
@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).
更多回答(3 个)
Image Analyst
2021-9-12
编辑:Image Analyst
2021-9-12
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 个评论
Image Analyst
2021-9-12
Simply make this change to the colormap:
cmap([1:100,152:256], :) = []; % Instead of 0
Atsushi Ueno
2021-9-12
编辑:Atsushi Ueno
2021-9-12
[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 个评论
Image Analyst
2021-9-12
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
2021-9-12
Atsushi Ueno
2021-9-12
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Red 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!