Note added 20-Dec-2022: I wrote this post with lossy JPEG in mind. If you work with DICOM data, see Jeff Mather's note in the comments below about the use of lossless JPEG in DICOM.
I have recently been reading a lot of image processing and image analysis questions on MATLAB Answers. Anyone who does the same will quickly come across comments and answers posted by the prolific Image Analyst, a MATLAB Answers MVP who has posted an astounding 34,600+ answers in the last 11 years.
I always enjoy the side commentary that I sometimes find in these answers. For example, I came across this code comment in one answer:
% This is a horrible image. NEVER use JPG format
% for image analysis. Use PNG, TIFF, or BMP instead.
Today, I want to take the opportunity to endorse this statement and to elaborate on it a bit. Why do we not love JPEG files for image analysis?
The reason is that JPEG is a lossy image compression format. Lossy compression methods achieve substantial reduction in file size by using "inexact approximations and partial data discarding to represent the content." With JPEG compression, roughly speaking, pixels are grouped into blocks, and then the pixel data in each block is quantized and partially discarded. The method is called lossy because you can't get the exact original image pixel data back from a JPEG file; information has been lost. I've written about lossy vs. lossless compression before; see my 02-May-2013 post.
Usually, this form of compression is acceptable for viewing photographs because the compression method exploits properties of human visual perception so that the information loss is relatively imperceptible. In image analysis applications, though, when you are trying to automatically detect or measure things, the pixel data imperfections in the JPEG file could mess it up.
Let me show you a couple of different versions of this picture:
The original picture was shot in RAW mode (which is lossless) and saved as a 4032x3024 PNG file, with a file size of 23 MB.
Here is a highly magnified view of the center of the picture. You can see the individual pixels.
I created a JPEG version of this file with a file size of only about 1 MB. If you look at the entire picture, the JPEG version looks like the original.
But if you look at a highly magnified view, you can see that pixel data has been partially discarded in a block-wise fashion.
You can probably imagine how this might affect object detection and measurement.
So, as Image Analyst says, stick to lossless formats such as PNG or TIFF for your image analysis applications.
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.