我試圖通過使用模板匹配將2個圖像縫合在一起查找3組點,我將其傳遞到cv2.getAffineTransform()
獲取扭曲矩陣,我將它傳遞到cv2.warpAffine()
以對齊我的圖像。使用warpAffine一起顯示拼接圖像
但是,當我加入我的圖像時,大部分仿射圖像都未顯示。我嘗試過使用不同的技術來選擇點,改變順序或參數等,但我只能得到一個很窄的仿射圖像滑動顯示。
有人可以告訴我,我的方法是否有效,並建議我可能會犯的錯誤?任何猜測什麼可能導致問題將不勝感激。提前致謝。
這是我得到的final result。這裏是原始圖像(1,2)和我使用的代碼:
編輯:這裏的變量trans
array([[ 1.00768049e+00, -3.76690353e-17, -3.13824885e+00],
[ 4.84461775e-03, 1.30769231e+00, 9.61912797e+02]])
這裏的結果是在這裏傳遞給cv2.getAffineTransform
點:unified_pair1
array([[ 671., 1024.],
[ 15., 979.],
[ 15., 962.]], dtype=float32)
unified_pair2
array([[ 669., 45.],
[ 18., 13.],
[ 18., 0.]], dtype=float32)
個
import cv2
import numpy as np
def showimage(image, name="No name given"):
cv2.imshow(name, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
return
image_a = cv2.imread('image_a.png')
image_b = cv2.imread('image_b.png')
def get_roi(image):
roi = cv2.selectROI(image) # spacebar to confirm selection
cv2.waitKey(0)
cv2.destroyAllWindows()
crop = image_a[int(roi[1]):int(roi[1]+roi[3]), int(roi[0]):int(roi[0]+roi[2])]
return crop
temp_1 = get_roi(image_a)
temp_2 = get_roi(image_a)
temp_3 = get_roi(image_a)
def find_template(template, search_image_a, search_image_b):
ccnorm_im_a = cv2.matchTemplate(search_image_a, template, cv2.TM_CCORR_NORMED)
template_loc_a = np.where(ccnorm_im_a == ccnorm_im_a.max())
ccnorm_im_b = cv2.matchTemplate(search_image_b, template, cv2.TM_CCORR_NORMED)
template_loc_b = np.where(ccnorm_im_b == ccnorm_im_b.max())
return template_loc_a, template_loc_b
coord_a1, coord_b1 = find_template(temp_1, image_a, image_b)
coord_a2, coord_b2 = find_template(temp_2, image_a, image_b)
coord_a3, coord_b3 = find_template(temp_3, image_a, image_b)
def unnest_list(coords_list):
coords_list = [a[0] for a in coords_list]
return coords_list
coord_a1 = unnest_list(coord_a1)
coord_b1 = unnest_list(coord_b1)
coord_a2 = unnest_list(coord_a2)
coord_b2 = unnest_list(coord_b2)
coord_a3 = unnest_list(coord_a3)
coord_b3 = unnest_list(coord_b3)
def unify_coords(coords1,coords2,coords3):
unified = []
unified.extend([coords1, coords2, coords3])
return unified
# Create a 2 lists containing 3 pairs of coordinates
unified_pair1 = unify_coords(coord_a1, coord_a2, coord_a3)
unified_pair2 = unify_coords(coord_b1, coord_b2, coord_b3)
# Convert elements of lists to numpy arrays with data type float32
unified_pair1 = np.asarray(unified_pair1, dtype=np.float32)
unified_pair2 = np.asarray(unified_pair2, dtype=np.float32)
# Get result of the affine transformation
trans = cv2.getAffineTransform(unified_pair1, unified_pair2)
# Apply the affine transformation to original image
result = cv2.warpAffine(image_a, trans, (image_a.shape[1] + image_b.shape[1], image_a.shape[0]))
result[0:image_b.shape[0], image_b.shape[1]:] = image_b
showimage(result)
cv2.imwrite('result.png', result)
來源:法根據收到的意見here,這tutorial這example從文檔。
偉大的工作從最後一個問題實施解決方案!對於一個建議,所以人們不必篩選所有的代碼,你可能需要包含一個你創建的轉換,並且只顯示試圖獲取該轉換的代碼,以使你獲得正確的座標。雖然我不知道這是否有必要,因爲我很快就能夠得到答案。主要的問題是,教程假定你從左到右 - 它實際上並不知道有多少像素需要移位。不過,你可以真正計算這個。 –
@AlexanderReynolds感謝您的評論,我已經添加了用於生成warpMatrix的變量與原始問題以及變換矩陣「trans」的關係。 我假設(也許不正確)由'cv2.getAffineTransformation'生成的變換矩陣也會移動圖像。你知道我該怎麼去計算這個轉變嗎? – Bprodz
是的,我願意。現在寫一個答案。 :) –