Python使用?OpenCV?進行圖像投影變換
投影變換(仿射變換)
在數(shù)學中,線性變換是將一個向量空間映射到另一個向量空間的函數(shù),通常由矩陣實現(xiàn)。如果映射保留向量加法和標量乘法,則映射被認為是線性變換。
要將線性變換應用于向量(即,一個點的坐標,在我們的例子中——像素的 x 和 y 值),需要將該向量乘以表示線性變換的矩陣。作為輸出,你將獲得一個坐標轉(zhuǎn)換后的向量。
投影變換可以用以下矩陣表示:
其中:
是一個旋轉(zhuǎn)矩陣。該矩陣定義了將要執(zhí)行的變換類型:縮放、旋轉(zhuǎn)等。
是平移向量。它只是移動點。
是投影向量。對于仿射變換,該向量的所有元素始終等于 0。
如果 x 和 y 是一個點的坐標,則可以通過簡單的乘法進行變換:
這里,x' 和 y' 是變換點的坐標。
這就是仿射變換的全部理論。現(xiàn)在我將深入研究該程序:
步驟 1:讀取源圖像并獲取源圖像大?。?/strong>
# Read source image img_src = cv2.imread('source_image.jpg') h, w, c = img_src.shape # Get source image parameter: #[[left,top], [left,bottom], [right, top], [right, bottom]] img_src_coordinate = np.array([[0,0],[0,h],[w,0],[w,h]])
根據(jù)源圖像,我們將得到相關坐標如下:
[[左,上],[左,下],[右,上],[右,下]]
好的!現(xiàn)在我們使用 get_paste_position 來獲取目標圖像中的坐標,源圖像將被粘貼到該坐標。
def get_paste_position(event, x, y, flags, paste_coordinate_list): cv2.imshow('collect coordinate', img_dest_copy) if event == cv2.EVENT_LBUTTONUP: # Draw circle right in click position cv2.circle(img_dest_copy, (x, y), 2, (0, 0, 255), -1) # Append new clicked coordinate to paste_coordinate_list paste_coordinate_list.append([x, y])
點擊4個點后,我們將4個點保存到paste_coordinate_list:
while True: cv2.waitKey(1) if len(paste_coordinate) == 4: break
然后,這 4 個點將通過 cv2.findHomography 計算投影變換矩陣。
matrix, _ = cv2.findHomography(img_src_coordinate, paste_coordinate, 0)
得到投影變換矩陣后,我們將使用 cv2.warpPerspective 將源圖像轉(zhuǎn)換為具有目標圖像大小的透視圖像。
perspective_img = cv2.warpPerspective(img_src, matrix, (img_dest.shape[1], img_dest.shape[0]))
這是透視圖的樣子:
透視圖
最后,將透視圖像應用于目標圖像,這就是最終效果:
完整代碼:
import cv2 as cv2 import numpy as np # This function will get click pixel coordinate that source image will be pasted to destination image def get_paste_position(event, x, y, flags, paste_coordinate_list): cv2.imshow('collect coordinate', img_dest_copy) if event == cv2.EVENT_LBUTTONUP: # Draw circle right in click position cv2.circle(img_dest_copy, (x, y), 2, (0, 0, 255), -1) # Append new clicked coordinate to paste_coordinate_list paste_coordinate_list.append([x, y]) if __name__ == '__main__': # Read source image img_src = cv2.imread('woman-1807533_960_720.webp', cv2.IMREAD_COLOR) # cv2.imwrite('source_image.jpg', img_src) h, w, c = img_src.shape # Get source image parameter: [[left,top], [left,bottom], [right, top], [right, bottom]] img_src_coordinate = np.array([[0,0],[0,h],[w,0],[w,h]]) # Read destination image img_dest = cv2.imread('billboard-g7005ff0f9_1920.jpg', cv2.IMREAD_COLOR) # copy destination image for get_paste_position (Just avoid destination image will be draw) img_dest_copy = img_dest.copy()#np.tile(img_dest, 1) # paste_coordinate in destination image paste_coordinate = [] cv2.namedWindow('collect coordinate') cv2.setMouseCallback('collect coordinate', get_paste_position, paste_coordinate) while True: cv2.waitKey(1) if len(paste_coordinate) == 4: break paste_coordinate = np.array(paste_coordinate) # Get perspective matrix matrix, _ = cv2.findHomography(img_src_coordinate, paste_coordinate, 0) print(f'matrix: {matrix}') perspective_img = cv2.warpPerspective(img_src, matrix, (img_dest.shape[1], img_dest.shape[0])) cv2.imshow('img', perspective_img) cv2.copyTo(src=perspective_img, mask=np.tile(perspective_img, 1), dst=img_dest) cv2.imshow('result', img_dest) cv2.waitKey() cv2.destroyAllWindows()
到此這篇關于Python使用 OpenCV 進行圖像投影變換的文章就介紹到這了,更多相關Python OpenCV 圖像投影內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python矩陣轉(zhuǎn)換為一維數(shù)組的實例
今天小編就為大家分享一篇python矩陣轉(zhuǎn)換為一維數(shù)組的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06python中的關鍵字參數(shù)*args和**kwargs詳解
這篇文章主要介紹了python中的關鍵字參數(shù)*args和**kwargs詳解,在定義類或函數(shù)時,有時候會用到*args和**kwargs,前者叫位置參數(shù),后者叫關鍵字參數(shù),需要的朋友可以參考下2023-11-11