调整图像大小时出错:“错误:(-215:声明失败)函数'resize'中的func!= 0” [英] Error while resizing image: "error: (-215:Assertion failed) func != 0 in function 'resize'"
问题描述
我正在尝试将图像数据集重新缩放为(10,10),以numpy数组的形式显示形状为(28,28)的图像。我为此写了一个函数:
I'm trying to preprocess images dataset, represented in numpy array with images of shape (28, 28) by rescaling them to (10, 10). I wrote a function for that:
def resize_dataset(images):
resized_images = []
for img in images:
img = img.reshape((28,28))
resized_img = cv2.resize(img, dsize=(10, 10))
resized_images.append(resized_img)
return numpy.array(resized_images)
但是当我实际上尝试重新缩放它们,我在 cv2.resize
中收到以下错误:
But when I actually try to rescale them, I get the following error in cv2.resize
:
error: OpenCV(4.0.0) /io/opencv/modules/imgproc/src/resize.cpp:3662: error: (-215:Assertion failed) func != 0 in function 'resize'
在google中,我只发现在用c ++编写相同的错误的人做非常不同的事情,就像这样:调整图像大小并更改其深度,并且: http://answers.opencv.org/question/19715/error-215-func-0-in-function-convertto /
In google I only found people with the same error writing on c++ doing very different stuff, like this one: resize an image and changing its depth and this: http://answers.opencv.org/question/19715/error-215-func-0-in-function-convertto/
那么,如何解决呢?
推荐答案
<哦,我实际上已经知道了。数据集中的图像类型为 numpy.int64
。我只需要将图像转换为 float32
,就像这样:
Oh, I actually figured it out. Images in the dataset were of type numpy.int64
. I just had to convert images to float32
, like this:
def resize_dataset(images):
resized_images = []
for img in images:
img = img.reshape((28,28)).astype('float32') # <-- convert image to float32
resized_img = cv2.resize(img, dsize=(10, 10))
resized_images.append(resized_img)
return numpy.array(resized_images)
现在它可以很好地工作了。看来 cv2.resize
无法处理以int表示的图像。希望这对任何人都有帮助
And now it works nicely. It looks like cv2.resize
can't work with images represented in int. Hope this will help anyone
这篇关于调整图像大小时出错:“错误:(-215:声明失败)函数'resize'中的func!= 0”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!