Difference between PIL resize and OpenCV resize


I want to mimic behavior of PIL.Image.resize(…,interpolation=PIL.Image.ANTIALIAS). What I would like to expect, is to have matching pixel vales (~99.9% exact match should be fine) . How can I do this with OpenCV ? I tried Opencv resize with available interpolation methods (including LANCZOS4) , but result does differ from the one I got from PIL.

Why I’m doing this? Well I 'm developing machine learning program in C that is using (infer) pre-trained model (done in Python+PIL with PIL.Image.ANTIALIAS). Even small differences in preprocessing of images has an impact on how machine learning model behave.

So either I find a way to achieve same resize behavior in OpenCV as used in PIL.Image.ANTIALIAS or how to use PIL in C.

So how can I get over this difference?

Thanks in advance for answering my question.

Please advice

In any case, you can easily convert PIL format to OpenCv format. Here’s how

1 Like

One major difference: The output of operations is completely different. In Pillow, ​you set radius, while in cv you set kernel size, which is literally diameter. You should use ksize=(7, 7) to achieve the same result.

But Dataloader could only support PIL Image as its input.
What I want to do is that:

  1. Use OpenCV to read, and preprocess image like transforms.
  2. Use data.Dataloader to create train dataloader and test dataloader. But it cannot accept data that is resized by OpenCV.
1 Like

Read with opencv, convert it to PIL format, process the data and then you can revert it back to cv2 if you want.

1 Like

ah neat. we must be on the same team.

OK Finally, our team rewrites the whole torchvision to enable it to support opencv readings, including the transform file.
Now it works well.

@LvJC: How to check that transform file uses opencv for resizing an image?