Error while resizing image: “error: (-215:Assertion failed) func != 0 in function 'resize'”

我们两清 提交于 2020-05-14 18:11:27

问题


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)

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'

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/

So, how to fix it?


回答1:


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)

And now it works nicely. It looks like cv2.resize can't work with images represented in int. Hope this will help anyone



来源:https://stackoverflow.com/questions/55428929/error-while-resizing-image-error-215assertion-failed-func-0-in-functio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!