Home > database >  Differences between Python and Java when reading JPG images
Differences between Python and Java when reading JPG images

Time:04-12

I am trying to understand the differences between reading jpg images in Python vs. Java. The opencv implementation seems to be different for both languages.

Python:

## create a random image and save it
img = np.random.randint(0,200,size=(192,336,3))
cv.imwrite("rnd.jpg",img)

## read the image in python
img = cv.imread("rnd.jpg")
img.max(axis=(0,1))
## result: array([255, 228, 255], dtype=uint8)

Java:

// read the same image in java
Mat img = Imgcodecs.imread("rnd.jpg");
List<Mat> channels = new ArrayList<Mat>();
Core.split(img, channels);
channels.forEach(ch -> System.out.println(Core.minMaxLoc(ch).maxVal));
## Result:231.0 219.0 234.0

It seems that the python implementation of opencv is not decoding the jpg in the same way as the Java implementation.

Is there a way to make sure that both give the same result?

CodePudding user response:

According to this answer on Photography Stack Exchange JPEG:s are not guaranteed to decode to the same bits with different decoders. However, the differences are supposed to be "very, very small". Whether your results are out of spec or not I cannot tell, but it seems plausible that using random data should bring out the worst in any decoder/encoder combo...

  • Related