简体   繁体   English

Python:增加二维阵列的分辨率(如拆分像素)

[英]Python: Increase resolution of 2D Array (As in split the pixels)

I want to resize a 2D array of the size (25,180) to size (50,360) with keeping all the values at the same relative place.我想将大小为 (25,180) 的二维数组调整为大小 (50,360),同时将所有值保持在相同的相对位置。 If it was an image id should still look the same but have 4 times the pixels.如果它是一个图像 id 应该仍然看起来相同但有 4 倍的像素。 So for a smaller array example of (2,3):因此,对于 (2,3) 的较小数组示例:

 ((1,3,6),
  (4,7,8))

it should look like (4,6):它应该看起来像 (4,6):

((1,1,3,3,6,6),
 (1,1,3,3,6,6),
 (4,4,7,7,8,8),
 (4,4,7,7,8,8))

I have tried resize and reshape but none seemed to do the job for me.我试过调整大小和重塑形状,但似乎没有一个适合我。 formulating this question i have found an easy solution using loops:制定这个问题我找到了一个使用循环的简单解决方案:

But I bet theres a clever and/or build in function for something like this right?但我敢打赌 function 中有一个聪明的和/或构建的这样的东西对吗?

I have tried resize and reshape but none seemed to do the job for me.我试过调整大小和重塑形状,但似乎没有一个适合我。 formulating this question i have found an easy solution using loops.制定这个问题我找到了一个使用循环的简单解决方案。 But I bet theres a clever and/or build in function for something like this right?但我敢打赌 function 中有一个聪明的和/或构建的这样的东西对吗?

ary = np.array(((1,3,6),(4,7,8)))
aarryy=np.zeros((4,6))
for i in range(len(ary)):
    #print(ary[i])
    for j in range(len(ary[i])):
        #print(ary[i][j])
        aarryy[i*2][j*2]=ary[i][j]
        aarryy[i*2][j*2+1]=ary[i][j]
    aarryy[i*2+1]=aarryy[i*2]

I am pretty sure there are some more elegant numpy magic (indexing is one of my weak point in numpy), and that someone will post one.我很确定有一些更优雅的 numpy 魔法(索引是我在 numpy 中的弱点之一),并且有人会发布一个。 But in the meantime, here is a one-liner但与此同时,这是一条单线

ary[np.arange(len(ary)*2)//2][:,np.arange(ary.shape[1]*2)//2]

The trick is that np.arange(n*2)//2 is array([0,0,1,1,...,n-1,n-1] . So I use that to index lines, and then same for columns诀窍是np.arange(n*2)//2array([0,0,1,1,...,n-1,n-1] 。所以我用它来索引行,然后列相同

(First time I post 2 answers instead of editing the 1st one. Hope I am not doing something wrong by doing so, but since the possibility exists, and that my two answers are totally different way to do the same thing, I guess this is appropriate) (我第一次发布 2 个答案而不是编辑第一个答案。希望我这样做没有做错什么,但由于存在这种可能性,而且我的两个答案是完全不同的方式来做同样的事情,我想这是合适的)

Another way is use hstack to duplicate lines.另一种方法是使用 hstack 复制行。 That way那样

np.hstack([ary,ary]).reshape(len(ary)*2,-1)

is

array([[1, 3, 6],
       [1, 3, 6],
       [4, 7, 8],
       [4, 7, 8]])

You can't directly duplicate columns the same way (lines and columns are not really dual. You can't just do the same with vstack)您不能以相同的方式直接复制列(行和列并不是真正的双重。您不能只对 vstack 做同样的事情)

But you can do the same operation on the transposed matrix, then transpose the result back但是你可以对转置矩阵做同样的操作,然后将结果转回去

So所以

ar2=np.hstack([ary,ary]).reshape(len(ary)*2,-1)
resized=np.hstack([ar2.T, ar2.T]).reshape(len(ar2.T)*2, -1).T

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

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