cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘

文章目录

报错

在使用cv2.resize() 对图片调整大小时遇到了以下错误。

img_array = cv2.resize(img_array,(1024,1024))
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-1bq9o88m\opencv\modules\imgproc\src\resize.cpp:3929: error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize’

错误代码

报错的代码如下:

img = Image.open(r'E:\workspace\PyCharmProject\dem_feature\dem\512\label\1.png')
img_array = np.array(img).astype("int32")
print(img_array.shape)

img_array = cv2.resize(img_array,(1024,1024))
print(img_array)
print(img_array.shape)

解决

原因是在将读取的图像转化为numpy array时将其定义为了“int32”型,而cv2.resize函数的参数必须是浮点型的,因此解决如下:

img = Image.open(r'E:\workspace\PyCharmProject\dem_feature\dem\512\label\1.png')
img_array = np.array(img).astype("float")
print(img_array.shape)

img_array = cv2.resize(img_array,(1024,1024))
print(img_array)
print(img_array.shape)

即先将图片数组转换为浮点型,在进行resize处理。

查看打印输出:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43598687/article/details/125632498