我從Image transformation in OpenCV的答案https://stackoverflow.com/a/10374811/4828720中的代碼,並試圖使其適應我的圖像。我的OpenCV重映射()ing有什麼問題?
在這裏面,我確定的格子磚的中心的像素座標,這裏所示:
我的目標分辨率784我計算像素的目標座標。我得到的代碼是這樣的:
import cv2
from scipy.interpolate import griddata
import numpy as np
source = np.array([
[315, 15],
[962, 18],
[526, 213],
[754, 215],
[516, 434],
[761, 433],
[225, 701],
[1036, 694],
], dtype=int)
destination = np.array([
[14, 14],
[770, 14],
[238, 238],
[546, 238],
[238, 546],
[546, 546],
[14, 770],
[770, 770]
], dtype=int)
source_image = cv2.imread('frames.png')
grid_x, grid_y = np.mgrid[0:783:784j, 0:783:784j]
grid_z = griddata(destination, source, (grid_x, grid_y), method='cubic')
map_x = np.append([], [ar[:,1] for ar in grid_z]).reshape(784,784)
map_y = np.append([], [ar[:,0] for ar in grid_z]).reshape(784,784)
map_x_32 = map_x.astype('float32')
map_y_32 = map_y.astype('float32')
warped_image = cv2.remap(source_image, map_x_32, map_y_32, cv2.INTER_CUBIC)
cv2.imwrite("/tmp/warped2.png", warped_image)
如果我運行它,沒有源點在他們的目的地結束了,但我得到一個扭曲的混亂來代替。我添加的目標點之上的位置:
我要去哪裏錯了?我注意到我的網格和地圖數組並不像示例中那樣分佈得很好。我的積分是否太少?我是否需要在常規電網中使用它們?我只嘗試使用外角的四個點也沒有運氣。
我的意思是包括一段說透視矯正是不夠的。它幾乎是,但我想要更多。對不起,謝謝!這裏http://i.imgur.com/Is5rwrq.png是在你的結果上繪製的黑條,你可以看到圖像的某些部分存在彎曲失真。我的8分實際上只是一種簡化,實際上我會用更多。我失去了爲什麼它現在無法正常工作。 – bugmenot123
在這種情況下,我建議你檢測每一行的交集。這裏有一個源代碼的絢麗例子:http://stackoverflow.com/questions/10196198/how-to-remove-convexity-defects-in-a-sudoku-square/11366549#11366549 – tfv
這是我的計劃,但它沒有爲我的圖像開箱即用(我的網格幾乎沒有區別),再加上我認爲重新映射值得一試。 – bugmenot123