Difference between jpg files from Matlab and ImageMagick
4 views (last 30 days)
Show older comments
Hi all,
I have a pgm file that I convert to jpg via matlab. To this end I use imread to read the file and imwrite to write it again. Inbetween I adjust the compression ratio by using the quality parameter. Finally I compute the error caused by the compression and compare it to the compression ratio.
If I do the same with Image Magick, I get significantly better results. For a given quality value, the resulting images look very similar (errors are roughly the same) but sometimes Graphics Magick achieves half the file size of Matlab.
Does anybody have an explanation for this? I was assuming that specifying the same quality level yields similar results independent of the software that I use for the conversion.
1 Comment
Geoff Hayes
on 15 Oct 2014
Laurent - what is the line of code that you are using in MATLAB to adjust the compression ratio?
Answers (1)
DGM
on 22 Sep 2024
I'm not familiar with whatever differences might be in the encoders, but there's one obvious thing that should be pointed out. MATLAB imwrite() always creates a 4:2:0 downsampled JPG, no matter what the -quality setting is. By default, imagemagick will adapt the downsampling to suit the specified quality parameter. That will definitely change the measured error and apparent compression ratio.
Assuming you have imagemagick and exiftool installed:
% inputs
inpict = imread('peppers.png'); % a clean file
q = 90; % quality parameter
% create the temp files
imwrite(inpict,'temp.png')
imwrite(inpict,'outml.jpg','quality',q)
cmdstr = sprintf('convert temp.png -quality %d outim.jpg',q);
system(cmdstr);
% imwrite() always produces a 4:2:0 JPG no matter what
fprintf('FileSize: %d\n',imfinfo('outml.jpg').FileSize)
[~,r] = system('exiftool -YCbCrSubSampling outml.jpg');
r = regexp(r,'YCbCr\d:\d:\d','match');
fprintf('SubSampling: %s\n',r{:})
FileSize: 41327
SubSampling: YCbCr4:2:0
% imagemagick will use 4:4:4 for 90% and up
% unless this default behavior is explicitly overridden
fprintf('FileSize: %d\n',imfinfo('outim.jpg').FileSize)
[~,r] = system('exiftool -YCbCrSubSampling outim.jpg');
r = regexp(r,'YCbCr\d:\d:\d','match');
fprintf('SubSampling: %s\n',r{:})
FileSize: 55541
SubSampling: YCbCr4:4:4
0 Comments
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!