计算拆分和混洗图像中的垂直边缘坐标(位置)

Calculate vertical edge coordinates(locations) in splitted & shuffled image

假设您有一张数字图像。您将该图像垂直裁剪为 6 块。之后,您将这些片段打乱并随机重新排列这些片段(子图像)。因此,您将获得如下图像。

我想像拼图一样重建原图。首先,我们需要计算子图像合并的 X 轴上的正确点。例如,在第 100 个像素(X 轴)中,两个子样本合并。我必须解析这些点以重新获得子图像。我怎样才能做到这一点?垂直分割的图像,X 轴上的 sobel 滤波器是否帮助我找到急剧的过渡?有什么建议吗?

这是一个有趣的有向图问题。在每个切片中,最后一列与第一列之间存在距离误差。构建图形后,您只需要找到头部,然后沿着最小成本路径走。 我已经草拟了一个脚本,您可以从以下位置开始:

import cv2
import numpy as np
import  matplotlib.pyplot as plt
cut_thr = 0.19 # magic number , but kind of arbitrary as if you add a cut, you just make your graph bigger
im = cv2.imread(r'example.png').astype(np.float32)/255 #read image
im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
dx=np.abs(np.diff(im,axis=1)) #x difference
dx = np.max(dx,axis=2) #max on all color channels
dx=np.median(dx,axis=0) #max on y axis
plt.plot(dx)

cuts = np.r_[0,np.where(dx>cut_thr)[0]+1,im.shape[1]] #inclusive borders
cuts = [im[:,cuts[i]:cuts[i+1]] for i in range(len(cuts)-1)]
n = len(cuts)
fig,ax = plt.subplots(1,n)
for a,c in zip(ax,cuts):
    a.imshow(c, aspect='auto')
    a.axis('off')

  d = np.ones((n,n))*np.nan # directed connectivity 
    for y in range(n):
        for x in range(y+1,n):
            d[y][x]=np.median(np.abs(cuts[y][:,-1]-cuts[x][:,0]))
            d[x][y]=np.median(np.abs(cuts[x][:,-1]-cuts[y][:,0]))
    src = np.arange(n)
    dst=np.nanargmin(d,axis=1) # the dest of source is the one with the lowest error
    indx=np.where(d==np.nanmin(d))[0][-1] #head, where to begin
    im = cuts[indx]
    for i in range(n-1):
        indx=dst[src[indx]]
        im = np.concatenate([im,cuts[indx]],axis=1)
    plt.figure()
    plt.imshow(im, aspect='equal')