【问题标题】:Calculate vertical edge coordinates(locations) in splitted & shuffled image计算分割和打乱图像中的垂直边缘坐标(位置)
【发布时间】:2022-01-01 23:25:27
【问题描述】:

假设您有一个数字图像。您将该图像垂直裁剪为 6 块。之后,您将这些碎片洗牌并随机重新排列这些碎片(子图像)。所以,你会得到如下图。

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

【问题讨论】:

    标签: python opencv image-processing computer-vision python-imaging-library


    【解决方案1】:

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

    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')
    

    【讨论】:

    • 好答案!我了解您对子图像进行切片的方法,但我不明白您如何比较和排序子图像。此外,这种情况下可以接受重构部分(第 3 个代码块),但不适用于其他示例。您对比较子图像有什么建议吗?你提取和比较左右边缘会起作用吗?
    • 每个子图都是图中的一个节点,边是源图最后一列到目的图第一列的差(这就是为什么d(x,y) !=d(y,x))。构建完整的图矩阵(标记为 d)后,您只需要找到一条包含所有权重最小的节点的路径。
    • 获得所有权重后,就是旅行商问题 (en.m.wikipedia.org/wiki/Travelling_salesman_problem)
    • 我明白了!即使我更改了阈值,您的代码也不适用于其他示例。排序算法不适用于其他图像。所以,我改变了算法。它首先寻找最左边的子图像,然后预测每个子图像的右边。
    猜你喜欢
    • 2013-08-28
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多