Difference between jpg files from Matlab and ImageMagick

조회 수: 4 (최근 30일)
Laurent
Laurent 2014년 10월 15일
답변: DGM 2024년 9월 22일
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일
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