MATLAB degrades image quality

In summary, the problem is to add axes to an image without changing the image quality or size. Suggestions include using export_fig or truesize() functions.
  • #1
Leonid92
45
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: 315
  • test1.jpg
    test1.jpg
    112.2 KB · Views: 406
Physics news on Phys.org
  • #2
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
  • #3
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
  • #4
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: 322
  • #6
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.
 
  • #7
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
  • #8
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.
 

1. How does MATLAB degrade image quality?

MATLAB can degrade image quality in several ways. One common way is through the use of lossy compression algorithms, which discard certain image data to reduce file size. Another way is through improper resizing or filtering techniques, which can result in pixelation or blurring.

2. Can MATLAB be used to improve image quality?

Yes, MATLAB has various functions and tools that can be used to enhance image quality. These include denoising algorithms, contrast adjustments, and image sharpening techniques. Additionally, MATLAB allows for precise control over image processing, making it a useful tool for improving image quality.

3. Will using MATLAB degrade an image permanently?

No, using MATLAB to process an image will not permanently degrade it. The original image file remains unchanged, and any modifications made through MATLAB can be undone or altered at any time. However, if the modified image is saved and then reprocessed multiple times, it may lead to a decrease in image quality.

4. Are there any precautions to take when using MATLAB to process images?

Yes, there are a few precautions to keep in mind when using MATLAB for image processing. It is essential to understand the functions and tools being used and how they may affect image quality. Additionally, always work on a copy of the original image to avoid permanently altering the original file.

5. Can using MATLAB degrade image quality even if I don't make any modifications?

Yes, simply opening an image file in MATLAB and then saving it without making any changes can result in a degradation of image quality. This is because MATLAB automatically converts images to its native format, which may result in a loss of image data. It is best to work on a copy of the original image to avoid this issue.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
  • Astronomy and Astrophysics
7
Replies
226
Views
10K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
983
Back
Top