Reputation: 464
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);
Upvotes: 1
Views: 760
Reputation: 131
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];
Upvotes: 3