关于OpenCV resize's INTER_AREA working domain的问题(func != 0 && cn <= 4 in function 'cv::hal::resize' failure)

Question about OpenCV resize's INTER_AREA working domain (func != 0 && cn <= 4 in function 'cv::hal::resize' failure)

我对使用 INTER_AREA 插值时 OpenCV 的 resize 函数的工作域有疑问。这是三种不同的插值:

import cv2
import numpy as np

cv2.resize(np.zeros((17, 99, 99), dtype=np.uint8), (64, 32), interpolation=cv2.INTER_AREA)
# OK
cv2.resize(np.zeros((200, 64, 4), dtype=np.uint8), (64, 32), interpolation=cv2.INTER_AREA)
# OK
cv2.resize(np.zeros((200, 64, 64), dtype=np.uint8), (64, 32), interpolation=cv2.INTER_AREA)
# error: OpenCV(4.1.1) ..\modules\imgproc\src\resize.cpp:3557: error: (-215:Assertion failed) func != 0 && cn <= 4 in function 'cv::hal::resize'

前两个工作正常,但最后一个失败。为什么会这样?什么样的 input/output 形状组合是可以接受的?

(请注意,该问题特定于 INTER_AREA,因为其他插值方案似乎适用于所有三种情况)。

OpenCV 的 resize()INTER_AREA 仅适用于最多 4 个通道的图像,当旧图像宽度和高度不是新宽度和高度的整数倍时(比例因子没有宽度和高度都相同,只要两个比例因子都是整数)。否则会产生错误。不幸的是,documentation and only way to find out is by digging into the source code.

中似乎没有提到这一点

您的第一个示例有效,因为区域插值仅在图像缩小时使用(在 x 和 y 方向)。否则使用双线性插值,对通道没有这个限制