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

Click For Summary
SUMMARY

The discussion focuses on retaining image quality while adding axes to an image in MATLAB. The user initially faced issues with pixelation and incorrect axis sizing when saving the modified image. Key solutions include using the export_fig function by Yair Altman for high-quality exports and ensuring the output image is saved in PNG format instead of JPG to avoid loss of quality. Additionally, adjusting figure properties to maximize screen size and using the truesize() function can help maintain the original image's integrity.

PREREQUISITES
  • Familiarity with MATLAB image processing functions
  • Understanding of figure properties in MATLAB
  • Knowledge of image file formats, specifically PNG and JPG
  • Basic understanding of axes manipulation in MATLAB
NEXT STEPS
  • Research the export_fig function for exporting high-quality figures in MATLAB
  • Learn about the truesize() function to maintain image dimensions
  • Explore the imresize function for image resizing techniques
  • Investigate the use of griddedInterpolant for custom interpolation methods
USEFUL FOR

MATLAB users, image processing specialists, and anyone looking to enhance image quality while adding graphical elements like axes to images.

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: 380
  • test1.jpg
    test1.jpg
    112.2 KB · Views: 472
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: 416
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 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K