【问题标题】:Covering matrices and image reconstruction using numpy使用 numpy 覆盖矩阵和图像重建
【发布时间】:2018-09-05 23:49:11
【问题描述】:

我不太擅长使用矩阵,而使用 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

当前结果生成的图像未对覆盖部分进行汇总。我知道求和矩阵不是那么简单,这就是我寻求帮助的原因

你有什么想法吗?

【问题讨论】:

  • 我实际上并没有看到图中发生了什么操作。你能解释一下吗?您能否通过粘贴小示例数组和预期输出来将其作为一个最小示例?
  • 我要做的操作是仅将 2 个矩阵的特定列相加:x = np.matrix([[1,1,1,1],[1,2,2,1] ,[1,2,2,1],[1,1,1,1]]) y = np.matrix([[7,7,7,7],[7,3,3,7],[ 7,3,3,7],[7,7,7,7]]) 我想“融合”第一个矩阵的最后两列和第二个矩阵的最后两列(步幅 = 2)。因为最终将是 f = np.matrix([[1,1,8,8,7,7],[1,2,9,4,3,7],[1,2,9,4,3 ,7],[1,1,8,8,7,7]]) 有可能吗?
  • OH,所以你在右边用两列零填充 x,然后在左边用两列零填充 y,然后添加。对吗?
  • 我不打算用零填充它们,而是将两个矩阵与它们的“连接列”(x 的最后两个和 y 的前两个)“组合”起来,有一个形状的最终矩阵:f.shape = (4,6)
  • 是的,这是填充数组的总和。我敢肯定还有其他方式来表达它,但对我来说没有一种明显的矢量化方式

标签: python image numpy matrix


【解决方案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')

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

【讨论】:

    猜你喜欢
    • 2015-10-30
    • 2021-06-10
    • 2017-03-23
    • 2014-06-20
    • 1970-01-01
    • 2015-07-25
    • 2021-03-24
    • 2020-05-08
    • 1970-01-01
    相关资源
    最近更新 更多