5

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).

user209974
  • 1,737
  • 2
  • 15
  • 31
  • 1
    `cv2.resize(np.zeros((2080, 64, 64), dtype=np.uint8), (64, 32), interpolation=cv2.INTER_AREA)` works, as does `4160`, but `1040` fails.... a mistery – lenik Oct 01 '19 at 14:00

1 Answers1

5

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.

sebasth
  • 959
  • 10
  • 21
  • What about @lenik's examples? They don't seem to be explained by what you describe. – user209974 Oct 01 '19 at 14:51
  • Included that in the answer. There is a different code path when scale (*x_old*/*x_new*, and *y_old*/*y_new*) are integers (instead of fractions). – sebasth Oct 01 '19 at 15:05