0

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

obtained image

Leonid
  • 35
  • 6
  • The issue is that you can not create a figure larger than screen size. So whatever you display gets downsampled, and then when saving it, it loses resolution. In order to save all the extras you have to create a figure handle fig1 = figure('Position',[0,0,imgsize1,imgsize2]); and then save the handle saveas(fig1,'test2.jpg'); – Yuval Harpaz Apr 02 '19 at 11:55
  • I changed the code as you wrote, but the result is the same. – Leonid Apr 02 '19 at 12:41
  • Did you try using `print` instead of `saveas`? It does the same thing but has more options to control resolution. You’d do something like `print -djpeg -r600 filename.jpg`. The `-r` option specifies the resolution of the output in pixels per inch. If the image is still pixelated, increase the resolution further. – Cris Luengo Apr 02 '19 at 13:03

2 Answers2

1

enter image description hereIn 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);
Yuval Harpaz
  • 1,416
  • 1
  • 12
  • 16
0

One man on another forum suggested the code that solves the problem regarding horizontal axis, but the resolution of the final image is still bad. Here is the code:

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
h = figure(); % !!!


% pixel axes and actual plot
a=axes('Position',[.2 .2 .7 .7]);
set(a,'Units','normalized');
iptsetpref('ImshowAxesVisible','on');

im = image(img); % !!!

% real world axis (below)
b=axes('Position',[.2 .1 .7 0]);
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)');

print('test4','-djpeg','-r300')

Please find attached the obtained image.obtained image

Leonid
  • 35
  • 6