zey*_*ger 4 python cv2 image-preprocessing
我正在尝试通过将图像数据集重新缩放为(10,10)来预处理numpy数组中的形状为(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)
Run Code Online (Sandbox Code Playgroud)
但是,当我实际尝试重新调整它们的大小时,在出现以下错误cv2.resize
:
error: OpenCV(4.0.0) /io/opencv/modules/imgproc/src/resize.cpp:3662: error: (-215:Assertion failed) func != 0 in function 'resize'
Run Code Online (Sandbox Code Playgroud)
在google中,我只发现在用c ++编写东西时犯同样错误的人做的事情非常不同,例如这样:调整图像大小并更改其深度,这是:http : //answers.opencv.org/question/19715/error-215- func-0-in-function-convertto /
那么,如何解决呢?
zey*_*ger 17
哦,我真的想通了。数据集中的图像类型为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)
Run Code Online (Sandbox Code Playgroud)
现在效果很好。看起来cv2.resize
无法使用int表示的图像。希望这会帮助任何人
实际上cv2.resize
可以处理以整数表示的图像,但您需要更改默认的插值方法。
这里列出了您可以使用的不同插值标志:OpenCV 调整大小插值标志。
默认情况下,cv2 使用 INTER_LINEAR 方法,顾名思义,它是线性的。因此,大多数时候它会导致给你必须表示为浮点数的值。与 INTER_CUBIC、INTER_AREA、INTER_LANCZOS4 相同...
但有些方法可以应用于整数,例如 INTER_NEAREST(最近邻)或 INTER_LINEAR_EXACT(与 INTER_LINEAR 相同,但四舍五入到最接近的整数)。
在上述情况下,我建议尝试:
import cv2
resized_img = cv2.resize(img, (10,10), interpolation=cv2.INTER_LINEAR_EXACT)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1926 次 |
最近记录: |