Prevent Color Bar from Resizing Image in MATLAB

半腔热情 提交于 2019-12-11 12:58:07

问题


I am trying to add color bars to an image in MATLAB without losing the original resolution of the figure.

This link explains how to deal with the fact that adding a color bar resizes the original image. But the solution makes the original loose information by enlarging using interpolation (the set method used in the 6th line from the bottom). It is crucial to my application that this does not happen (Trying to observe Moire effects on sub-sampling)

The code I am using is appended below

%% Load images using relative paths
path1 = '../data/circles_concentric.png';
path2 = '../data/barbaraSmall.png';
img1 = imread(path1, 'png');
img2 = imread(path2, 'png');
%Shrinking factor
d1 = 2;
d2 = 3;
img1_shrunk1 = myShrinkImageByFactorD(img1, d1);
imshow(img1_shrunk1);
colorbar(gca); 
img1_shrunk2 = myShrinkImageByFactorD(img1, d2);
figure, imshow(img1_shrunk2);
colorbar(gca);

回答1:


I have dealt with this problem by simply putting the colorbar in a separate axis.

%Import image and colormap
[img,map]=imread('image.tif');

%Create figure and show the image on ax1
fig=figure;
ax1=axes(fig);
imshow(img,map,'Parent',ax1);

%Create ax2 and make it invisible
ax2=axes(fig,...
    'Position',[ax1.Position(1)+ax1.Position(3),ax1.Position(2),0.2,0.7]);
axis off
set(ax2,'color','none');

%Apply colormap to ax2 and, colorbar and adjust CLim
colormap(map);
colorbar(ax2,'Position',...
    [ax1.Position(1)+ax1.Position(3)+0.03,0.1,0.05,0.7],...
        'AxisLocation','in');
ax2.CLim=[minValue,maxValue];


来源:https://stackoverflow.com/questions/51692593/prevent-color-bar-from-resizing-image-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!