简体   繁体   English

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

[英]How does the numpy.resize and numpy.reshape function works internally in python ?

In the package numpy their are two function resize and reshape. 在包numpy他们是两个函数调整大小和重塑。 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 ? 它的像素会发生什么?

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 (eg in scipy ) that manipulate the resolution of images. 有一些image包(例如在scipy )操纵图像的分辨率。

Every numpy array has a shape attribute. 每个numpy数组都有一个shape属性。 reshape just changes that, without changing the data at all. reshape只是改变,而不改变数据。 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 is less commonly used, but is written in Python and available for study. np.resize不太常用,但是用Python编写并可用于研究。 You have to read its docs, and x.resize is different. 你必须阅读它的文档, x.resize是不同的。 Going larger it actually repeats values or pads with zeros. 越大,它实际上会用零重复值或填充。

examples of resize acting in 1d: 在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])

A recent question about scipy.misc.imresize . 关于scipy.misc.imresize的最近一个问题。 It also references scipy.ndimage.zoom : 它还引用了scipy.ndimage.zoom

Broadcasting error when vectorizing misc.imresize() 向量化misc.imresize()时的广播错误

As far as I know numpy.reshape() just reshapes a matrix (does not matter if it is an image or not). 据我所知numpy.reshape()只是重塑一个矩阵(如果它是一个图像则无关紧要)。 It does not do any interpolation and just manipulates the items in a matrix. 它不进行任何插值,只是操纵矩阵中的项目。

a = np.arange(12).reshape((2,6))


a= [[ 0  1  2  3  4  5]
   [ 6  7  8  9 10 11]]


b=a.reshape((4,3))

b=[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]
  [ 9 10 11]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM