Alex
Alex

Reputation: 187

Matlab imwrite function changes the pixel values

I tried to change some pixel values of a Grayscale image and save it using imwrite in matlab. no problem with saving. the problem is when I read it back, some pixel values have been changed. not exactly the same values I assigned to pixels before saving it. I'm trying to hash images so 1unit difference will effect the hash numbers.

Upvotes: 1

Views: 1451

Answers (1)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38042

As mentioned by mmgp, JPG can be lossy. That means that some of the information in your image will be lost in favor of storage efficiency.

The rationale behind JPG is somewhat like that behind MP3 -- changes in hues etc. that the human eye is not particularly well-adapted to distinguish will be simplified or removed altogether, thus decreasing the amount of information in the image. The information in a JPG represents a similar-looking, but in fact very different image. This is probably what you're experiencing.

In Matlab, have a look at the output of help imwrite. You can give a parameter to the jpg write called 'Quality', which is a number between 0 and 100, 100 meaning (near-)lossless compression.

Although the JPEG standard does allow for (near-)lossless compression, it is not often used in practice (at least, in my field). More popular lossless image formats are PNG, JPEG2000 and TIFF. Read more about it here.

All of these are also available in Matlab's imwrite function.

Upvotes: 1

Related Questions