1

I've come across a difference in MATLAB and libjpg when it opens a JPEG file.

Basically if you open a JPEG file in libjpg (either using libjpg9 or using Gimp) and save the file as a bitmap (lossless), then open both the JPEG and BMP file into matlab as below:

A=imread('example1.jpg');
B=imread('example2.bmp');
C=abs(double(A(:,:,1))-double(B)(:,:,1));

Now I would expect that since they are the same image that array C would contain zeros but it turns out that libjpg is up to 5 shades out on each channel in either direction!

The difference is bigger in sharper images.

The most troubling thing is that I am trying to make a focus stacking tool and I was finding my output images were sharper in Matlab than my c++ program using libjpeg. I have been looking at the two for weeks and I've narrowed down the difference to when the JPEG files are loaded. It appears that MATLAB loads the images as being sharper (or higher quality) than libjpg. This is clear when you compare the results from my two scripts.

Has anyone else come across this problem? Why does libjpg9 interpret jpeg files differently than MATLAB? And why does libjpg9 lose quality when loading JPEGs?

EDIT:

For an example, please download this zip. Inside it has a JPG in the sRGB colour space, and a MATLAB m script. To use the example open the JPG in gimp and export it as a BMP. Then simply run the script and it will load and compare the two files.

https://dl.dropboxusercontent.com/u/15883001/JPGError.zip

I have found that JPGs that are saved with GIMP are fine, the problem appears to be with JPGs in the sRGB colour space.

In the example you can also see that each channel is sharper when reading the JPG into MATLAB. This is my main concern, slightly different colour representations are fine but loss of detail (i.e. loss of sharpness) is really bad.

EDIT2:

I have used 'Paint Shop Pro 5.0' to read in the JPG and save it as a BMP and it matches the MATLAB version exactly! I am using MATLAB 7.8.0 (R2009a)

I tried using the latest version of Paint.NET but it was off too but in a way slightly different to libjpg, it looked like it was more randomly off rather than losing detail like libjpg. I also confirmed that this was the case in matlab as the amount of edges detected are roughly similar. This is more what I would expect with slight differences in colour spaces, overall the detail was not lost in Paint.NET's interpretation of the JPG.

I am now thinking it is a bug in libjpg.

For info I am using Gimp 2.8.10, and Paint.NET v3.5.11.

ALM865
  • 1,078
  • 13
  • 21
  • Could you please post the c++ code of your loading jpeg function? – Bentoy13 May 20 '14 at 07:02
  • Are both using the same colorspace? And by "5 shades", I presume you mean a value difference of 5 in a single colour (R,G,B) - which could simply be that Matlab rounds up and libjpg rounds down on something - it's probably not easy to tell which is "right", unless you start by creating a bitmap with known colours in known places. Still jpeg is a destructive compression, so using JPEG as a source for something WILL have errors. – Mats Petersson May 20 '14 at 07:43
  • Note that 5 in 0..255 is about 2%, and almost certainly not noticeable with the naked eye (unless you put the pictures right next to each other) – Mats Petersson May 20 '14 at 07:44
  • Bentoy13, I have supplied you with enough information. Please use Gimp to open a jpeg (note that the gimp uses libjpeg, the exact same version and method I use in my code) and then export the file as a bitmap. Then load both files in Matlab as shown above and the two will be different. Mats Petersson, both the JPEG and Bitmap are RGB, it was a good point though since I didn't check initially. Also 2% matters a huge amount when edge detecting and stacking multiple images, the error multiplies as you go. It's a huge problem in my script that I am developing. – ALM865 May 20 '14 at 09:23
  • Bentoy13, also you could use the example.c included in libjpg9 here: http://www.ijg.org/files/jpegsr9.zip and change the line "put_scanline_someplace(buffer[0], row_stride);" to memcpy each line of the JPG into a char array, then save the result as a bitmap. That will essentially do the same as opening in Gimp and exporting as a bitmap but is significantly more convoluted. – ALM865 May 20 '14 at 09:27
  • @ALM865 I don't find any relevant difference in the code ... two differences: when loading a JPEG, Gimp converts CMYK it to RGB; moreover, Gimp seems to use explicitely the resolution field written in the jpeg header. Another point is that apparently Matlab 2007 uses libjpeg6 ... I can't find if libjpeg is still used in newer versions, but the contrary would be odd to me. Sorry not to find anything more useful :( – Bentoy13 May 20 '14 at 13:13
  • One tool to investigate further: with Matlab, you can view the info stored in the header of the image this way: `header_jpg = imformats('jpg'); header_jpg.info(my_image_name)`. Have you tried with different types of JPEG images (gray, more or less compressed, ...), to see if your problem is linked with a specific format? – Bentoy13 May 20 '14 at 13:51
  • @Bentoy13 the JPGs I am having trouble with are in the sRGB colourspace, JPG files saved directly from Gimp (or libjpg for that matter) are fine. Also I was using libjpg6b in my C++ code until I realised the discrepancy, I then updated my code to use libjpg9 and the discrepancy was still there. I can confirm whether MATLAB uses libjpg but it looks as though you can build and include it in MATLAB if you wish (http://stackoverflow.com/questions/14062215/building-matlab-jpeg-toolbox-with-libjpeg8d), but the imread function appears to be inbuilt and I can't find details on what it uses. – ALM865 May 20 '14 at 23:33
  • @ALM865 I've found [this repository](ftp://196.203.130.15/pub/logiciels/matlab2007/toolbox/matlab/imagesci/) where you can find the original complete code of Matlab2007, and on this page the code of `imread`. Yes, I know, it's incredible ... I really don't know why it's public, don't ask me. I used it to write my previous comment. – Bentoy13 May 21 '14 at 07:12
  • @Bentoy13 that repository is interesting! I haven't gotten any further in finding out why there is a difference. There is a definite loss of quality in files loaded in libjpg, but i think I'm just going to bite the bullet and just assume it's not a fixable problem in libjpg. – ALM865 May 23 '14 at 02:35
  • @ALM865 It's a pity to be stuck that way ... – Bentoy13 May 23 '14 at 07:06

0 Answers0