Recent MATLAB and Octave have stronger JPEG compression and show artifacts

Viewed 271

I wonder why the jpeg compression in recent MATLAB and Octave versions has gone so strong that it causes noticable compression artifacts:

  1. Octave 3 jpeg image with size of 41 KB with no artifacts: Octave 3 jpeg image with size of 41 KB with no artifacts

  2. MATLAB 9 jpeg image with size of 26 KB with artifacts: MATLAB 9 jpeg image with size of 26 KB with artifacts

  3. Octave 5 jpeg image with size of 23 KB with artifacts: Octave 5 jpeg image with size of 23 KB with artifacts

Here is the code to plot:

description = strcat('version-', num2str(version));% find out MATLAB/Octave version

x=1:2; % simple var

figure; % plot
plot(x, x);
title(description);

print(strcat("test_jpeg_size_", description ,'.jpg'), '-djpeg'); % write file

Do you know a possibility to tell MATLAB and Octave to do a weaker jpeg compression. I cannot find anything like this on https://de.mathworks.com/help/matlab/ref/print.html.

I know that I could plot png files and use imagemagick to convert it to jpeg with a given quality but this would be a workaround with additional tools. Or I could png files in the first place but the real images have no compression advantages for png (like like simple one here) and I would have to change a lot of other stuff.

2 Answers

This used to be documented*, I was surprised to not find it in the documentation pages. I tested it with the latest version of MATLAB (R2019b) and it still works:

The -djpeg option can take a quality value between 0 and 100, inclusive. The device option becomes -djpeg100 or -djpeg80, or whatever value you want to use.

print(strcat("test_jpeg_size_", description ,'.jpg'), '-djpeg100');

* Or at least I remember it being documented... The online documentation goes back to R13 (MATLAB 6.5), and it's not described in that version of the documentation nor in a few random versions in between that and the current version.


However, I strongly recommend that you use PNG for line drawings. JPEG is not intended for line drawings, and makes a mess of them (even at highest quality setting). PNG will produce better quality with a much smaller file size.

Here I printed a graph with -djpeg100 and -dpng, then cut out a small portion of the two files and show them side by side. JPEG, even at 100 quality, makes a mess of the lines:

comparing -djpeg100 and -dpng

Note that, in spite of not having any data loss, the PNG file is about 10 times smaller than the JPEG100 file.

You can go for

f = getframe(gcf);
imwrite(f.cdata, 'Fig1.jpg')

where imwrite takes the following options

  • Compression (compression scheme)
  • Quality (quality of JPEG-compressed file from 0 to 100)

See the doc of imwrite.

Related