There is an original image test1.jpg. The problem is to show axes in the image and retain image quality. I am running the following code which is taken from matlab, multiple axes or scales for image pixels and real distance:
img = imread('test1.jpg');
% define variables
imgsize1 = size(img,1); % size on screen
imgsize2 = size(img,2);
xreal = 1; % meter
yreal = 1; % meter
% create figure
figure('Position',[0,0,imgsize1,imgsize2]);
% pixel axes and actual plot
a=axes('Position',[.2 .2 .7 .7]);
set(a,'Units','normalized');
iptsetpref('ImshowAxesVisible','on');
imshow(img,'Parent',a);
% real world axis (below)
b=axes('Position',[.2 .1 .7 1e-12]);
set(b,'Units','normalized');
set(b,'Color','none');
set(b,'xlim',[0 xreal]);
% real world axis (left)
c=axes('Position',[.09 .2 1e-12 .7 ]);
set(c,'Units','normalized');
set(c,'Color','none');
set(c,'ylim',[0 yreal],'YDir','reverse');
% set labels
xlabel(a,'Pixels')
xlabel(b,'Real distance (m)')
ylabel(a,'Pixels');
ylabel(c,'Real distance (m)');
saveas(gcf,'test2.jpg');
1) The obtained image test2.jpg has bad quality - it became strongly pixelated. 2) The horizontal axis is bigger than the image.
I tried to use imwrite, but it doesn't save axes in image.
Please advise, how can I solve these problems. I will be very appreciate for any help.
The original and obtained images are attached to this message. original image
In principle you can do what you want with imwrite. The problem is that you have to write all the axes into the rectangle of your image data. This solution makes a brown mask, later to be filled with the map image. You can see that there is some inaccuracy there, because some brown pixels remain in the final output. Hope this gives you a lead.
% read map
img = imread('test1.jpg');
imgsize1 = size(img,1); % size on screen
imgsize2 = size(img,2);
% create figure small enough for the screen
h = figure('Position',[0,0,imgsize2/3,imgsize1/3],'units','pixels');
a=axes('Position',[.2 .2 .7 .7]);
iptsetpref('ImshowAxesVisible','on');
% make a brown rectangle to mark map location in figure
pix = [];
pix(1,1,1) = 200;
pix(1,1,2) = 50;
pix(1,1,3) = 50;
img1 = uint8(repmat(pix,imgsize1,imgsize2,1));
imshow(img1,'Parent',a);
set(a,'Units','normalized','fontsize',13);
%iptsetpref('ImshowAxesVisible','on');
b=axes('Position',[.2 .1 .7 1e-12]);
set(b,'xlim',[0 1],'Color','none','fontsize',13);
c=axes('Position',[.09 .2 1e-12 .7 ]);
set(c,'ylim',[0 1],'YDir','reverse','Color','none','fontsize',13);
% set labels
xlabel(a,'Pixels')
xlabel(b,'Real distance (m)')
ylabel(a,'Pixels');
ylabel(c,'Real distance (m)');
% save brown map
saveas(h,'test2.jpg');
img2 = imread('test2.jpg');
% find map size in saved image
x = find(img2(:,700,1) == 200,1,'last')-find(img2(:,700,1) == 200,1);
y = find(img2(700,:,1) == 200,1,'last')-find(img2(700,:,1) == 200,1);
ratio = (imgsize1/x + imgsize2/y)/2;
% correct size of image
img3 = imresize(img2,ratio);
% find map coord in image
x3 = find(img3(:,1700,1) == 200,1);
y3 = find(img3(1700,:,1) == 200,1);
% fill brown rectangle with real map
img3(x3:x3+imgsize1-1,y3:y3+imgsize2-1,:) = img;
imwrite(img3,'test3.jpg')
figure;
imshow(img3);