0

I trained my custom AI model in python, and I used OpenCV python binding to open and resizing images.

After training, I tried to do inference with test images, without using OpenCV. That is, I tried to open and resize test images with PIL.image. I appropriately considered some points, like color formatting, axis convention. However, the onnx inference result for test images are quite nonsense,even considering model's performance. So, I made below code snippet to check whether there exists any difference between OpenCV and Pillow's image resize policy.

import PIL
from PIL import Image
import cv2
import numpy as np

cvImg = cv2.imread("test.png")
cvImg = cv2.resize(cvImg, dsize= (200, 200), interpolation= cv2.INTER_LINEAR_EXACT)
cvImg = np.flip(cvImg, 2)

print(cvImg.shape)

pilImg = Image.open("test.png")
pilImg = pilImg.convert("RGB")
pilImg = pilImg.resize((200, 200), resample= PIL.Image.LINEAR)   #2 bilinear 

pilNP = np.asarray(pilImg)

print(pilNP.shape)
diff = cvImg - pilNP

print(np.mean(diff))

cv2.namedWindow("CV", cv2.WINDOW_NORMAL)
cv2.namedWindow("PIL", cv2.WINDOW_NORMAL)
cv2.namedWindow("Diff", cv2.WINDOW_NORMAL)

cv2.imshow("CV", cvImg)
cv2.imshow("PIL", pilNP)
cv2.imshow("Diff", diff)

np.savetxt('cv0.out', cvImg[:, :, 0], delimiter=',', fmt='%.2e')
np.savetxt('cv1.out', cvImg[:, :, 1], delimiter=',', fmt='%.2e')
np.savetxt('cv2.out', cvImg[:, :, 2], delimiter=',', fmt='%.2e')

np.savetxt('PIL0.out', pilNP[:, :, 0], delimiter=',', fmt='%.2e')
np.savetxt('PIL1.out', pilNP[:, :, 1], delimiter=',', fmt='%.2e')
np.savetxt('PIL2.out', pilNP[:, :, 2], delimiter=',', fmt='%.2e')

while True:
    k = cv2.waitKey()
    if k == 32:
        flag += 1
    elif k == 27:
        exit()

And the mean of difference between two resized image matrix was 63.06386666666667, which is quite considerable.

Also, the two resized images are looked quite similar, but the difference matrix has a lot of noise, which indicates two images has quite different pixel values.

I know what bilinear interpoliation is, but I'm quite confused which one among OpenCV and pillow is correct. It seems that Pillow core module, which actually implements resizing function, is not publically opened. Also, it's quite hard to dig into OpenCV's implemenation for resizing.

Looking for any help. Thx in advance!

FYI ) There exists issue about this point, but there is no progress so far :https://github.com/python-pillow/Pillow/issues/2718

happychild
  • 83
  • 7

0 Answers0