gpt4 book ai didi

python - numpy.resize 和 numpy.reshape 函数如何在 python 内部工作?

转载 作者:行者123 更新时间:2023-12-03 21:44:36 40 4
gpt4 key购买 nike

在 numpy 包中,它们有两个函数 resize 和 reshape。它们在内部如何工作?他们使用什么样的插值?我查看了代码,但没有得到它。谁能帮我吗。或者如何调整图像大小。它的像素会发生什么?

最佳答案

两者都不插值。如果您想知道图像的插值和像素,它们可能不是您想要的功能。还有一些image处理图像分辨率的包(例如在 scipy 中)。

numpy数组有一个 shape属性。 reshape只是改变它,根本不改变数据。新形状必须引用与原始形状相同的元素总数。

 x = np.arange(12)
x.reshape(3,4) # 12 elements
x.reshape(6,2) # still 12
x.reshape(6,4) # error
np.resize不太常用,但它是用 Python 编写的,可供学习。你必须阅读它的文档,以及 x.resize是不同的。变大它实际上重复值或用零填充。

在 1d 中调整大小的示例:
In [366]: x=np.arange(12)
In [367]: np.resize(x,6)
Out[367]: array([0, 1, 2, 3, 4, 5])
In [368]: np.resize(x,24)
Out[368]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11])
In [369]: x.resize(24)
In [370]: x
Out[370]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0])

最近关于 scipy.misc.imresize 的问题.它还引用了 scipy.ndimage.zoom :

Broadcasting error when vectorizing misc.imresize()

关于python - numpy.resize 和 numpy.reshape 函数如何在 python 内部工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41171034/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com