带有2D图像的Python中的Lanczos插值 [英] Lanczos Interpolation in Python with 2D images

查看:61
本文介绍了带有2D图像的Python中的Lanczos插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试重新缩放2D图像(灰度).图像大小为256x256,所需的输出为224x224.像素值的范围是0到1300.

I try to rescale 2D images (greyscale). The image size is 256x256 and the desired output is 224x224. The pixel values range from 0 to 1300.

我尝试了两种方法来通过Lanczos插值对它们进行缩放:

I tried 2 approaches to rescale them with Lanczos Interpolation:

首先使用PIL图片:

import numpy as np
from PIL import Image
import cv2

array = np.random.randint(0, 1300, size=(10, 256, 256))
array[0] = Image.fromarray(array[0]).resize(size=(224, 224), resample=Image.LANCZOS)

导致错误消息: ValueError:图像模式错误

然后是CV2:

array[0] = cv2.resize(array[0], dsize=(224, 224), interpolation=cv2.INTER_LANCZOS4)

导致错误消息: ValueError:无法将输入数组从形状(224,224)广播到形状(256,256)

如何正确执行?

推荐答案

在第二种情况下,您将256x256图像的大小调整为224x224,然后将其分配回原始数组的一个切片中.该切片的尺寸仍为256x256,因此NumPy不知道如何进行数据复制.

In the second case, you are resizing a 256x256 image to 224x224, then assigning it back into a slice of the original array. This slice still has size 256x256, so NumPy doesn't know how to do the data copy.

相反,创建一个大小合适的新输出数组:

Instead, create a new output array of the right sizes:

array = np.random.randint(0, 1300, size=(10, 256, 256))
newarray = np.zeros((10, 224, 224))
newarray[0] = cv2.resize(array[0], dsize=(224, 224), interpolation=cv2.INTER_LANCZOS4)

这篇关于带有2D图像的Python中的Lanczos插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆