How to display an image using Imagesc or Imshow with only a part of the colorscale?
조회 수: 26 (최근 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
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.
[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!