Difference between jpg files from Matlab and ImageMagick

7 次查看(过去 30 天)
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 个评论
Geoff Hayes
Geoff Hayes 2014-10-15
Laurent - what is the line of code that you are using in MATLAB to adjust the compression ratio?

请先登录,再进行评论。

回答(1 个)

DGM
DGM 2024-9-22,12:34
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

类别

Help CenterFile Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by