简体   繁体   中英

Why cv2.resize() does not work for integer array?

[~] python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np, cv2
>>> a=np.arange(10,250,10).reshape((6,4))
>>> a
array([[ 10,  20,  30,  40],
       [ 50,  60,  70,  80],
       [ 90, 100, 110, 120],
       [130, 140, 150, 160],
       [170, 180, 190, 200],
       [210, 220, 230, 240]])
>>> cv2.resize(a,(3,2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.1.0) /io/opencv/modules/imgproc/src/resize.cpp:3596: error: (-215:Assertion failed) func != 0 in function 'resize'

The problem persists both in Python 3.6 under Linux and in Python 3.7 under Windows 10. The error message under Windows 10 is:

OpenCV(3.4.1) Error: Assertion failed (func != 0) in cv::hal::resize, file C:\Miniconda3\conda-bld\opencv-suite_1533128839831\work\modules\imgproc\src\resize.cpp, line 3922
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(3.4.1) C:\Miniconda3\conda-bld\opencv-suite_1533128839831\work\modules\imgproc\src\resize.cpp:3922: error: (-215) func != 0 in function cv::hal::resize

If I change type to float64 or float32, eg

a=np.arange(10,250,10).reshape((6,4)).astype(np.float64)

then cv2.resize suddenly works.

I believe the underlying C++ function of cv2.resize does not handle int64 or int32 , only uint8 , so you can try:

cv2.resize(np.uint8(a),(3,2))

output:

array([[ 52,  65,  78],
       [172, 185, 198]], dtype=uint8)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM