Reading of JPEG Images

15 views (last 30 days)
Simone Croci
Simone Croci on 24 Apr 2020
Commented: Ameer Hamza on 25 Apr 2020
Hi All,
I have compared the image reading function of Photoshop and Matlab applied to jpg images and I have found a difference between them. For the comparison, I have first opened a jpg image with Photoshop and saved in png. Then, I have opened the jpg and png images with Matlab and I have computed the absolute difference between them. I have found that more than half of the pixels are different, with a maximal difference equal to 41. How could it be possible? which image reading function should I trust?
You can find the images in the attachment.
Best,
Simone

Answers (2)

Ameer Hamza
Ameer Hamza on 25 Apr 2020
It is not a matter of which to trust. This is the expected behavior. No image format saves the RGB intensity of every individual pixel. They use different methods to efficiently compress the image data so that the original image can be constructed from the compressed information. The purpose of image compression formats is not to save pixel-level information; rather, they are designed so that the overall appearance of the image remains the same.
JPEG format uses lossy compression: https://en.wikipedia.org/wiki/JPEG, which implies that if you compress an image using JPEG, some of the information in the original image is permanently lost. On the other hand, PNG is a lossless compression format: https://en.wikipedia.org/wiki/Portable_Network_Graphics, it retains all the information in the original image. Since PNG is a lossless format, therefore if the same image saved in JPEG and PNG, then the PNG file will have a larger size.
As you can see, the JPEG was never intended to save pixel-level information, therefore, If you compare both image pixel by pixel, you can see large variations at some points, but, overall, the appearance of the image is same. It is a tradeoff between retaining the image quality and making a file size smaller.
  2 Comments
Walter Roberson
Walter Roberson on 25 Apr 2020
No image format saves the RGB intensity of every individual pixel
... To be technically correct, the PPM file format stores RGB images with no compression at all. The data is also represented as text, rather than binary: the design intent was to make it text-only to prove a way to communicate between systems that might disagree on how to transfer binary files (for example a PPM file could be used on an EBCDIC computer.)
Ameer Hamza
Ameer Hamza on 25 Apr 2020
Walter, thanks for pointing this exceptional case.

Sign in to comment.


Simone Croci
Simone Croci on 25 Apr 2020
I know that jpg is a lossy and png a lossless. What I have compared are the behaviours of the image reading functions of Matlab and Photoshop. In order to study the image reading function of Photoshop, I have read a jpg image and then saved it in png, since png is lossles and keeps the pixel values of the jpg image read in Photohop. Then, I have read the jpg image and the png image in Matlab and compared them. The comparison shows that the image reading functions of Matlab and Photoshop behave differently when reading a jpg image.
  7 Comments
Simone Croci
Simone Croci on 25 Apr 2020
Thank you Ameer for your answer.
I have also converted the jpg image to png using GIMP, and then I have compared the jpg image and this new png image in MATLAB without finding a difference. So, MATLAB and GIMP use the same algorithm for reading jpg images, while Photoshop uses a different one.
Ameer Hamza
Ameer Hamza on 25 Apr 2020
Yes, MATLAB and GIMP may be using the same decoding algorithms. But the underlying reason is the different implementation of decoders across libraries.
Just as a side note, the results posted by Stephen does seem to be a rare coincidence. It tried counting the unique colors in several other jpg images using MATLAB and imageio, and the number is never the same. For example,
MATLAB:
>> JPG = imread('lena10.jpg');
>> size(unique(reshape(JPG,[],3),'rows'))
ans =
51606 3
imageio:
>>> JPG = imageio.imread('lena10.jpg')
>>> len(np.unique(JPG.reshape(-1, JPG.shape[-1]), axis=0))
45359

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!