計算分割和混洗圖像中的垂直邊緣坐標(位置)

達斯梅迪克斯

假設您有一張數字圖像。您將該圖像垂直裁剪為 6 塊。之後,您將這些片段打亂並隨機重新排列這些片段(子圖像)。因此,您將獲得如下所示的圖像。

SS

我想像拼圖一樣重建原始圖像。首先,我們需要計算X軸上子圖像合併的正確點。例如,在第 100 個像素(X 軸)中,兩個子樣本合併。我必須解析這些點以重新獲得子圖像。我怎樣才能做到這一點?垂直分割的圖像,X 軸上的索貝爾過濾器是否可以幫助我找到尖銳的過渡?有什麼建議嗎?

這是一個有趣的有向圖問題。在每個切片中,最後一列與第一列的距離誤差。構建圖形後,您只需要找到頭部,然後沿著最小成本路徑走即可。我已經勾畫了一個你可以從頭開始的腳本:

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

在此處輸入圖片說明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章