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

Viewed 2608

I have a question about the working domain of OpenCV's resize function when using the INTER_AREA interpolation. Here are three different interpolations:

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'

The first two works OK, but the last one fails. Why would that be? What combination of input/output shape is acceptable?

(Note that the question is specific to INTER_AREA, as other interpolation schemes seem to work in all three cases).

1 Answers

OpenCV's resize() with INTER_AREA only works for images with at most 4 channels when the old image width and height are not an integer multiples of the new width and height (scale factors do not have to be the same for both width and height, as long as both scale factors are integers). Otherwise an error is generated. Unfortunately, this doesn't seem to be mentioned in the documentation and only way to find out is by digging into the source code.

Your first example works, because area interpolation is only used when image is shrinked (in both x and y directions). Bilinear interpolation is used otherwise, and it does not have this limitation on channels.

Related