numpy.resize和numpy.reshape函数如何在python中内部工作? [英] How does the numpy.resize and numpy.reshape function works internally in python ?

查看:1140
本文介绍了numpy.resize和numpy.reshape函数如何在python中内部工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the package numpy their are two function resize and reshape. How internally they work? What kind of interpolation does they use ?I looked into the code, but didnt get it. Can anyone help me out. Or how does an image get resized. What happens with its pixels ?

推荐答案

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

Neither interpolates. And if you are wondering about interpolation and pixels of an image, they probably aren't the functions that you want. There some image packages (e.g in scipy) that manipulate the resolution of images.

每个 numpy 数组都有形状属性。 reshape 只需更改即可,而无需更改数据。新形状必须引用与原始形状相同的元素总数。

Every numpy array has a shape attribute. reshape just changes that, without changing the data at all. The new shape has to reference the same total number of elements as the original shape.

 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 是不同的。更大它实际上用零重复值或垫。

np.resize is less commonly used, but is written in Python and available for study. You have to read its docs, and x.resize is different. Going larger it actually repeats values or pads with zeros.

在1d中调整大小的例子:

examples of resize acting in 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

A recent question about scipy.misc.imresize. It also references scipy.ndimage.zoom:

向量化misc.imresize()时的广播错误

这篇关于numpy.resize和numpy.reshape函数如何在python中内部工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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