我正在尝试通过将它们重新缩放到(10,10)来预先处理以numpy形状(33600,784)表示的图像数据集(mnist)和形状(28,28)的图像。我写了一个函数:
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)
但是当我真的尝试重新缩放它们时
x_tr_resc = resize_dataset(x_tr)
我收到以下错误:
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-38-fc680e0206f0> in <module>()
----> 1 x_tr_resc = resize_dataset(x_tr)
<ipython-input-37-bd0b64255821> in resize_dataset(images)
6 for img in images:
7 img = img.reshape((28,28))
----> 8 resized_img = cv2.resize(img, dsize=(10, 10))
9 resized_images.append(resized_img)
10 return numpy.array(resized_images)
error: OpenCV(4.0.0) /io/opencv/modules/imgproc/src/resize.cpp:3662: error: (-215:Assertion failed) func != 0 in function 'resize'
我试图谷歌这个错误,但只发现在c ++上写相同错误的人做了非常不同的东西,比如这个:resize an image and changing its depth和这个:http://answers.opencv.org/question/19715/error-215-func-0-in-function-convertto/
我究竟做错了什么?
哦,我真的想通了。数据集中的图像为numpy.int64
类型。我只需将图像转换为float32
,就像这样:
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中表示的图像。希望这对任何人都有帮助