首页 > 解决方案 > PIL 和 OpenCV 的调整大小有什么区别

问题描述

我遇到了以下问题:这两个库的调整大小函数的行为不同。这是一个小测试:

import numpy as np
import PIL
import cv2
from matplotlib import pyplot as plt

img = np.random.randn(10, 10, 3)
SIZE = (5, 5)
img -= img.min()
img /= img.max()
img = (img*255).astype(np.uint8)

# Display the initial image
plt.figure(figsize=(16,9))
plt.imshow(img)
plt.show()
plt.close()

# resize the image in two different ways
img_cv2 = cv2.resize(img, dsize=SIZE, interpolation=cv2.INTER_LINEAR)
img_pil = PIL.Image.fromarray(img).resize(SIZE, resample=PIL.Image.BILINEAR)

# get the difference image and normalize it
diff = np.abs(img_cv2.astype(np.float32) - img_pil)
diff /= diff.max() or 1

# display results
fig, axs = plt.subplots(1, 3, figsize=(16, 9))
axs[0].imshow(img_cv2)
axs[1].imshow(img_pil)
axs[2].imshow(diff)
plt.show()
plt.close()

我现在的问题是:为什么会这样?是实现上的差异(我还没有检查 PIL 或 OpenCV 中的代码)还是我以错误的方式使用了这些函数?

以下是一些示例输出:Input imageResized images

标签: pythonpython-imaging-libraryimage-resizingopencv-python

解决方案


推荐阅读