2016-05-13 45 views
3

我從Image transformation in OpenCV的答案https://stackoverflow.com/a/10374811/4828720中的代碼,並試圖使其適應我的圖像。我的OpenCV重映射()ing有什麼問題?

我的源圖像: My source image

在這裏面,我確定的格子磚的中心的像素座標,這裏所示:

Source points

我的目標分辨率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) 

如果我運行它,沒有源點在他們的目的地結束了,但我得到一個扭曲的混亂來代替。我添加的目標點之上的位置:

My result

我要去哪裏錯了?我注意到我的網格和地圖數組並不像示例中那樣分佈得很好。我的積分是否太少?我是否需要在常規電網中使用它們?我只嘗試使用外角的四個點也沒有運氣。

回答

0

整個問題是,我再次得到了由numpy的的行/列的索引,而不是X/Y混淆。 #opencv IRC頻道中的人指出了它。我的源和目的數組必須有自己的列切換:

source = np.array([ 
    [15, 315], 
    [18, 962], 
    [213, 526], 
    [215, 754], 
    [434, 516], 
    [433, 761], 
    [701, 225], 
    [694, 1036], 
], dtype=int) 

destination = np.array([ 
    [14, 14], 
    [14, 770], 
    [238, 238], 
    [238, 546], 
    [546, 238], 
    [546, 546], 
    [770, 14], 
    [770, 770] 
], dtype=int) 

然後它的工作如預期(忽略醜陋扭曲,這是座標的簡單列表中找到的bug):

enter image description here

1

如果你只有8點扭曲你的圖像沒有真正的扭曲,我會建議使用透視轉換here描述。

您引用的鏈接會嘗試消除導致非直線的額外扭曲,但圖像中的所有線條都是筆直的。

代碼是這樣:

import cv2 
import numpy as np 
import matplotlib.pyplot as plt 

img = cv2.imread('image.png') 
rows,cols,ch = img.shape 

pts1 = np.float32([ 
    [315, 15], 
    [962, 18], 
    [225, 701], 
    [1036, 694], 
], dtype=int) 

pts2 = np.float32([ 
    [14, 14], 
    [770, 14], 
    [14, 770], 
    [770, 770] 
], dtype=int) 

M = cv2.getPerspectiveTransform(pts1,pts2) 

dst = cv2.warpPerspective(img,M,(784,784)) 

plt.subplot(121),plt.imshow(img),plt.title('Input') 
plt.subplot(122),plt.imshow(dst),plt.title('Output') 
plt.show() 

enter image description here

+0

我的意思是包括一段說透視矯正是不夠的。它幾乎是,但我想要更多。對不起,謝謝!這裏http://i.imgur.com/Is5rwrq.png是在你的結果上繪製的黑條,你可以看到圖像的某些部分存在彎曲失真。我的8分實際上只是一種簡化,實際上我會用更多。我失去了爲什麼它現在無法正常工作。 – bugmenot123

+0

在這種情況下,我建議你檢測每一行的交集。這裏有一個源代碼的絢麗例子:http://stackoverflow.com/questions/10196198/how-to-remove-convexity-defects-in-a-sudoku-square/11366549#11366549 – tfv

+0

這是我的計劃,但它沒有爲我的圖像開箱即用(我的網格幾乎沒有區別),再加上我認爲重新映射值得一試。 – bugmenot123