python - 为什么cv2.resize()对整数数组不起作用?

标签 python opencv image-resizing

[~] 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'

在Linux下的Python 3.6和Windows 10下的Python 3.7中,该问题仍然存在。Windows10下的错误消息是:
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

如果我将类型更改为float64或float32,例如
a=np.arange(10,250,10).reshape((6,4)).astype(np.float64)

然后cv2.resize突然起作用。

最佳答案

我相信cv2.resize的基础C++函数不处理int64int32,仅处理uint8,因此您可以尝试:

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

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

关于python - 为什么cv2.resize()对整数数组不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56652405/

相关文章:

python opencv形状检测

python - SQLALchemy session.merge 不起作用

python - 如何在OpenCV中的python中平均获得2分?

javascript - 调整窗口大小时修改图像大小(jQuery)

c# - 如何调整图像大小并保持纵横比?

python - 如何在 Python 3 中使用过滤器、映射和归约

python - 运行时错误 : To use MKL 2018 with Theano you MUST set "MKL_THREADING_LAYER=GNU"

c++ - openCV c++ : Problems working with CvBoost (Adaboost classifer)

python-3.x - Tkinter视频的闪烁视频

html - 根据屏幕自动调整图像