MATLAB How Can I Retain Image Quality While Showing Axes in MATLAB?

AI Thread Summary
The discussion centers on the challenge of adding axes to an image (test1.jpg) while maintaining high image quality. The initial attempt to save the modified image (test2.jpg) resulted in pixelation and an oversized horizontal axis. Suggestions include saving the output as a PNG instead of JPG to avoid quality loss, maximizing the figure size before saving, and using the 'export_fig' function or 'truesize()' to improve the output. The user expresses a desire to keep the original image intact without interpolation, focusing solely on adding the axes. The conversation emphasizes the importance of resolution and proper file format for preserving image quality when adding graphical elements.
Leonid92
Messages
45
Reaction score
2
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:
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
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.
 

Attachments

  • test2.jpg
    test2.jpg
    59.5 KB · Views: 362
  • test1.jpg
    test1.jpg
    112.2 KB · Views: 460
Physics news on Phys.org
You are creating a figure that include your original image (1000x598) at 70% scale.
Put it in at 100% scale and make the final image bigger than the original. Big enough to include the original image at full scale as well as the graphics you're adding.
Right now, your output image is only 1000x522
 
  • Like
Likes Leonid92
Don't save your output image (with all the axes, graphics, labels, titles, etc.) as JPG format. Yes, that will give you a crappy image. Save the image as PNG format, which is lossless compression and will give you exactly what you see. Be sure to maximize your screen first so your output image is as big as possible (most resolution).
Code:
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
Remember, it will save basically a screenshot, which may be less than the resolution of your original image.
 
  • Like
Likes Leonid92
I wrote:
Code:
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
before
Code:
saveas(gcf,'test3.jpg');
But it didn't work - the resolution of the obtained image is very bad, and the horizontal axis is still bigger than the image size in corresponding direction.
 

Attachments

  • test3.jpg
    test3.jpg
    54.8 KB · Views: 394
kreil said:
Another option is that you can use interpolation to resize the image to any size and maintain reasonable quality. This is what the imresize function does, but you can also do the actual interpolation yourself if you want with griddedInterpolant:

https://www.mathworks.com/help/matlab/math/resample-image-with-gridded-interpolation.html
Thank you for your answer! But I wouldn't like to interpolate my image, I would like to preserve the image as it was, i.e. original image without any changes, just with added axes.
 
Leonid92 said:
Thank you for your answer! But I wouldn't like to interpolate my image, I would like to preserve the image as it was, i.e. original image without any changes, just with added axes.
Try export_fig by Yair Altman, on the File Exchange, or maybe try the truesize() function.
 
  • Like
Likes Leonid92
Image Analyst said:
Try export_fig by Yair Altman, on the File Exchange, or maybe try the truesize() function.
Thanks a lot! I will try these functions.
 

Similar threads

Replies
1
Views
2K
Replies
1
Views
3K
Replies
6
Views
5K
Replies
2
Views
4K
Replies
1
Views
2K
Replies
1
Views
4K
Back
Top