python - 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 image和Resized images。
解决方案
推荐阅读
- vb.net - JSON 未反序列化为自定义类
- android - 我的 recyclerview 没有显示我的数组列表
- visual-studio-code - 如何在复制粘贴时覆盖 VS Code 中的文件(而不是将 *.1* 附加到文件名)?
- drools - 解释分数 KIE Workbench
- r - R 记分卡包 ID 列
- asp.net-core - 跟踪来自 ASP.NET Core 服务的每个响应代码
- c# - IText7 pdf2Data 它与c#核心兼容吗?
- javascript - 无法删除重复的项目
- java - setBackgroundResource() 覆盖屏幕内容
- sql - 使用 XML 过滤 SQL 结果