difference image imagesc rescaling

조회 수: 49 (최근 30일)
Guillaume
Guillaume 2014년 9월 5일
답변: Image Analyst 2014년 9월 8일
Hello,
I displayed an image with the functions "imagesc" and "image" (see below).
The image contains pixel values from 0 to 255, so there should not be any rescaling and both pictures should look the same. But it's not the case. Why?
Thanks for the answer. Guillaume

채택된 답변

Adam
Adam 2014년 9월 5일
편집: Adam 2014년 9월 8일
imagesc will linearly scale your data appropriately so that you see the full range of the data in the image with no clipping. This will then be applied to a colourmap whose caxis values run from the data min to the data max. Depending on the histogram of your data this may or may not be useful, but the lack of clipping/saturation means you can then compress the colourmap with caxis or apply some non-linear scaling to your data to reshape the histogram and, for example, boost lower amplitude data relative to the very small number of high amplitude pixels that define the default scaling of the colourmap.
image simply takes your actual data values and uses them as indices into what is, by default, a length 64 jet colourmap. i.e. all values above 64 will get clipped to 64.
If you do the following:
figure; imagesc( data )
figure; image( data ); set( gcf, 'Colormap', jet( 256 ) )
you should get two images that look the same and neither one saturated/clipped.
Personally I never use image unless I have full RGB data - i.e. an n*m*3 matrix which image will then use as true RGB values rather than an index into a jet (by default) colourmap. I always use imagesc to just plot 2-dimensional data which I want to map onto a colourmap.

추가 답변 (2개)

Image Analyst
Image Analyst 2014년 9월 5일
The bottom image has pixel values only from 0 to 60. Or at least that's what imagesc decided to use as a linear range to map into 0-255. Use caxis() to change if you want something different.
  댓글 수: 5
Adam
Adam 2014년 9월 5일
The bottom image is the one created by 'image', the top one by 'imagesc' so far as I can see.
Image Analyst
Image Analyst 2014년 9월 5일
Oh, right. image() does not do scaling and does not apply a colormap by default (just grayscale). I guess I'd have to see your image and try the two functions on it. Can you attach it? Since the bottom image uses image() function and it does no scaling, I'd be surprised if the max gray level is 255 instead of 60.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2014년 9월 8일
I think this code illustrates the situation well. Basically image() displays gray levels greater than the number of colors as the max/highest color while imshow() takes the colormap and applies it to all the gray levels. Run the demo, also attached as a m-file below the image, to product this image:
% Demo to how how the displayed image appears different with 64 colors
% (the default number of colors in the built in colormaps) in image() vs. imshow().
% imshow() always shows all the data from min at the lowest color, to the max at the highest color
% regardless of how many colors are in the colormap.
% image() on the other hand shows 0 at the lowest color and the gray level of the max number of colors in the highest color.
% So for example with an image with 256 gray levels from 0 to 255, if you have a colormap with 64 colors (rows),
% which is the default number of colors for the built-in colormaps such as jet, then pixels with gray levels
% more than 64 (i.e. 63 - 255) will be displayed with the color of the 64th row of the colormap.
% So the upper 3/4 of the gray levels all appear in the same color, instead of different colors like with imshow().
% Run the demo and you will understand better because it will illustrate it for you.
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.
fontSize = 20;
% Create an image with 255 gray levels.
myImage = uint8(255 * mat2gray(peaks(300)));
myImage = uint8(repmat(1:255, [256, 1]));
% myImage = imresize(myImage, 3.0, 'Nearest');
% Display it with imshow() and image(), and 64 and 256 colors in colormap.
% imshow() always displays the full data range.
% Value 255 will be the color of the last row of the colormap
% regardless of how many rows (colors) are in the colormap.
figure;
imshow(myImage); % Always displays full data range, regardless of colormap.
axis on;
colormap(jet(64)); % Displays values and color map all the way to 255
colorbar; % Display color bar ramp to the right of the image.
title('imshow() function with 64 colors in colormap', 'FontSize', fontSize);
% Enlarge figure to a quarter screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.5 .5 0.5]);
figure;
imshow(myImage); % Always displays full data range, regardless of colormap.
axis on;
colormap(jet(256)); % Displays values and color map all the way to 255
colorbar; % Display color bar ramp to the right of the image.
title('imshow() function with 256 colors in colormap', 'FontSize', fontSize);
% Enlarge figure to a quarter screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0.5 0.5 .5 0.5]);
% image() displays only the range of the colormap.
% Any pixel values beyond the last colormap color will be
% displayed as the last colormap value.
figure;
image(myImage); % Always displays full data range, regardless of colormap.
axis on;
colormap(jet(64)); % Displays values and color map all the way to 255
colorbar; % Display color bar ramp to the right of the image.
% Enlarge figure to a quarter screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 .5 0.5]);
title('image() function with 64 colors in colormap', 'FontSize', fontSize);
figure;
image(myImage); % Always displays full data range, regardless of colormap.
axis on;
colormap(jet(256)); % Displays values and color map all the way to 255
colorbar; % Display color bar ramp to the right of the image.
title('image() function with 256 colors in colormap', 'FontSize', fontSize);
% Enlarge figure to a quarter screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0.5 0 .5 0.5]);

카테고리

Help CenterFile Exchange에서 Blue에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by