Syed Saad Waqar
Syed Saad Waqar

Reputation: 1

OpenCV from Python shows different results for JPG and PNG images?

I have been working to create an OMR bubble sheet scanner using OpenCV from Python.

I have created the bubble-sheets (using OpenCV) in PNG format.

Then I am using OpenCV to read those files. OpenCV does a perfect job on PNG images, it works perfectly.... but when I use this on JPG files, it simply doesn't! Lists running out of indexes because it cannot detect all the proper contours.

I know this doesn't make sense, but I tried saving the same PNG image directly into JPG (using Paint), and the same results. It works on PNG version, but doesn't work on JPG version. It also shows a different number of contours in the images even the images are exact same.

Any help regarding this would be greatly appreciated, thanks!

Upvotes: 0

Views: 3606

Answers (1)

ZdaR
ZdaR

Reputation: 22964

The problem is inherent to the image format you are using. There are majorly two type of compression techniques(all image formats: jpeg, png, webp are compression techniques):

  • Lossless Compression
  • Lossy compression

As the name suggests, Lossless compression technique do not change the underlying matrix data while compression, for example: PNG. And the Lossy compression technique may substitute the underlying data with some nearest round off values to save some space, for example: JPEG.

In your case while you are using JPEG format to save the image, it may distort some RGB pixel information, so if your threshold range are really tight then you will have different results as compared to PNG.

Upvotes: 1

Related Questions