使用numpy覆盖矩阵和图像重建

问题描述 投票:-1回答:1

我使用矩阵和使用numpy的相当新的不是很好,但我有一个带有覆盖端的贴片切片,我想重新构建覆盖部分,如下所示:

我目前正在使用它来重建我的图像:

new_im = Image.new('L', (576,576)) #creating a new image
y = (vqr // u)*stride #calculating the position of the patch to "glue"
x = i % u*stride
new_im.paste(img, (x, y, x + w, y + h)) #pasting the patch to the new image

当前结果产生的图像中不包括覆盖部分。我知道求和矩阵并不那么简单,这就是为什么我要求一些帮助

你有什么想法?

python image numpy matrix
1个回答
1
投票

只需用适当的零列填充矩阵,然后像正常一样添加。

import numpy as np
# define inputs and desired output  
x = np.array([[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]])
y = np.array([[7,7,7,7],[7,3,3,7],[7,3,3,7],[7,7,7,7]])  
f = np.array([[1,1,8,8,7,7],[1,2,9,4,3,7],[1,2,9,4,3,7],[1,1,8,8,7,7]]) 

shape = (x.shape[0],x.shape[1]+2) # the shape of the desired output
xpad = np.zeros(shape) # pad the x array on the left with two columns of 0
ypad = np.zeros(shape) # and pad the y array on the right with two columns
xpad[:,:-2]=x # like this 
ypad[:,2:]=y
f_ = xpad+ypad # and then do the addition to get f_ the actual output
np.alltrue(f_==f) #True -- it works

评论:你可能不想要np.matrix。我认为他们会贬值它。在大多数情况下,它没有做任何np.array一般不能做更多。

另一种难以阅读的方法是

f_ = np.pad(x,((0,0),(0,2)),'constant')+np.pad(y,((0,0),(2,0)),'constant')

您可以轻松地将其扩展到您的案例。用零填充每个数组到适当的形状,然后求和

© www.soinside.com 2019 - 2024. All rights reserved.