當我決定從頭開始撰寫代碼以更好地理解它的管道時,我正在對 OpenCV 函式cv2.warpPerspective進行一些實驗。盡管我(希望)遵循了每一個理論步驟,但似乎我仍然缺少一些東西,而且我正在努力理解什么。請你幫助我好嗎?
SRC 影像(左)和 True DST 影像(右)
cv2.warpPerspective 的輸出與 True DST 重疊
# Invert the homography SRC->DST to DST->SRC
hinv = np.linalg.inv(h)
src = gray1
dst = np.zeros(gray2.shape)
h, w = src.shape
# Remap back and check the domain
for ox in range(h):
for oy in range(w):
# Backproject from DST to SRC
xw, yw, w = hinv.dot(np.array([ox, oy, 1]).T)
# cv2.INTER_NEAREST
x, y = int(xw/w), int(yw/w)
# Check if it falls in the src domain
c1 = x >= 0 and y < h
c2 = y >= 0 and y < w
if c1 and c2:
dst[x, y] = src[ox, oy]
cv2.imshow(dst gray2//2)
我的代碼的輸出
PS:輸出影像是Estimated DST和True DST的重疊,以更好地突出差異。
uj5u.com熱心網友回復:
您的問題相當于一個錯字。您混淆了坐標的命名。單應性假設(x,y,1)順序,這將對應于(j,i,1).
只需(x, y, 1)在計算中使用,然后(xw, yw, w)在結果中使用(然后x,y = xw/w, yw/w)。如果公式正確,該w因子反映了數學。
避免索引到.shape. 指數不會“說話”。只需做(height, width) = src.shape[:2]并使用這些。
我建議修復命名方案,或在評論中將其定義在頂部。我建議堅持使用x,y而不是 i、j、u、v,然后使用前綴/后綴擴展它們所在的空間(“src/dst/in/out”)。也許像ox,oy迭代一樣,只是xw,yw,w為了單應性結果,它變成x,y通過除法,和ix,iy(整數化)在輸入中采樣?然后你可以使用dst[oy, ox] = src[iy, ix]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/510785.html
上一篇:在螢屏之間傳遞值Kivy
