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

时间:2018-09-05 23:49:12

标签: python image numpy matrix

我使用矩阵的效果不是很好,而使用numpy的效果不是很好,但是我将图像切成具有覆盖末端的小块,我想重构它以总结如下覆盖部分:

Covering matrices and summing

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

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

当前结果产生一个图像,其中覆盖部分未汇总。我知道对矩阵求和并不是那么简单,这就是为什么我需要一些帮助

您有什么想法吗?

1 个答案:

答案 0 :(得分: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')

您可以轻松地将此扩展到您的情况。用零填充每个数组到合适的形状,然后求和