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

我想像拼圖一樣重建原始影像。首先,我們需要計算X軸上子影像合并的正確點。例如,在第 100 個像素(X 軸)中,兩個子樣本合并。我必須決議這些點以重新獲得子影像。我怎樣才能做到這一點?垂直分割的影像,X 軸上的索貝爾過濾器是否可以幫助我找到尖銳的過渡?有什么建議嗎?
uj5u.com熱心網友回復:
這是一個有趣的有向圖問題。在每個切片中,最后一列與第一列的距離誤差。構建圖形后,您只需要找到頭部,然后沿著最小成本路徑走即可。我已經勾畫了一個你可以從頭開始的腳本:
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')

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/365633.html
