从python中的补丁/磁贴重建图像

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

我有一个任务,我使用代码平铺输入图像:

def tileImage(img,numrows,numcols,show = False):
    height = int(img.shape[0] / numrows)
    width = int(img.shape[1] / numcols)
    tiles = []
    for row in range(numrows):
        for col in range(numcols):
            y0 = row * height
            y1 = y0 + height
            x0 = col * width
            x1 = x0 + width
            tiles.append(img[y0:y1, x0:x1])
            if show:
                cv2.imshow("tile",img[y0:y1, x0:x1])
                cv2.rectangle(img,(x0,y0),(x1,y1),(255),1)
                cv2.imshow("Tile",img)
                cv2.waitKey(0)
                cv2.destroyAllWindows
    return tiles

之后,我计算了所有瓷砖的总和,并按升序对它们进行排序。我将sum.sorted [-20]作为阈值,并将低于该阈值的所有图块设置为0,以忽略背景。到目前为止一切正常。

现在我需要使用瓷砖重建图像。我试过np.reshape。

shutOffTiles = np.reshape(thresholded,(height*numrows,width*numcols,1))

尺寸还可以。但是,由于瓷砖Reconstruction的顺序,结果看起来像这样

我还试图压平瓷砖并重塑它们。有没有人正确的索引解决方案?非常感谢你提前

与@Paul Panzer的解决方案

thresholded.reshape(numrows, numcols, height, width).swapaxes(1, 2).reshape(height*numrows, width*numcols, 1)

我得到:enter image description here

python numpy image-processing cv2
1个回答
2
投票

事实证明,需要两种成分。

1)几何:

由于嵌入原始图像中的图块不是存储器连续的(例如在左上图块的第一行到达下一图块的第一行之后),因此简单的重塑将不起作用。相反,我们必须首先将第0个轴(枚举图块的轴)拆分为行和列。然后我们必须将两个水平轴和两个垂直轴彼此相邻移动,在每种情况下,瓷砖尺寸必须最后。最后,我们可以将两对组合成一个轴,并在右侧添加一个新轴。

thresholded.reshape(numrows, numcols, height, width).swapaxes(1, 2).reshape(height*numrows, width*numcols, 1)

2)标准化。

显然,处理和可视化之间存在不匹配,可以通过规范化来解决 - OP比我更了解。

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