Python使用?OpenCV?進行圖像投影變換

投影變換(仿射變換)
在數(shù)學(xué)中,線性變換是將一個向量空間映射到另一個向量空間的函數(shù),通常由矩陣實現(xiàn)。如果映射保留向量加法和標(biāo)量乘法,則映射被認為是線性變換。
要將線性變換應(yīng)用于向量(即,一個點的坐標(biāo),在我們的例子中——像素的 x 和 y 值),需要將該向量乘以表示線性變換的矩陣。作為輸出,你將獲得一個坐標(biāo)轉(zhuǎn)換后的向量。
投影變換可以用以下矩陣表示:

其中:

是一個旋轉(zhuǎn)矩陣。該矩陣定義了將要執(zhí)行的變換類型:縮放、旋轉(zhuǎn)等。

是平移向量。它只是移動點。

是投影向量。對于仿射變換,該向量的所有元素始終等于 0。
如果 x 和 y 是一個點的坐標(biāo),則可以通過簡單的乘法進行變換:

這里,x' 和 y' 是變換點的坐標(biāo)。
這就是仿射變換的全部理論?,F(xiàn)在我將深入研究該程序:
步驟 1:讀取源圖像并獲取源圖像大小:
# 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ù)源圖像,我們將得到相關(guān)坐標(biāo)如下:
[[左,上],[左,下],[右,上],[右,下]]

好的!現(xiàn)在我們使用 get_paste_position 來獲取目標(biāo)圖像中的坐標(biāo),源圖像將被粘貼到該坐標(biāo)。

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)換為具有目標(biāo)圖像大小的透視圖像。
perspective_img = cv2.warpPerspective(img_src, matrix, (img_dest.shape[1], img_dest.shape[0]))
這是透視圖的樣子:

透視圖
最后,將透視圖像應(yīng)用于目標(biāo)圖像,這就是最終效果:

完整代碼:
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()到此這篇關(guān)于Python使用 OpenCV 進行圖像投影變換的文章就介紹到這了,更多相關(guān)Python OpenCV 圖像投影內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
django模板語法學(xué)習(xí)之include示例詳解
寫過 Web 程序的都對 include 包含文件很熟悉,那么在 Django,include 又是怎么一個機制呢?下面這篇文章主要給大家介紹了關(guān)于django模板語法學(xué)習(xí)之include的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
python矩陣轉(zhuǎn)換為一維數(shù)組的實例
今天小編就為大家分享一篇python矩陣轉(zhuǎn)換為一維數(shù)組的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Python根據(jù)字典的值查詢出對應(yīng)的鍵的方法
這篇文章主要介紹了Python根據(jù)字典的值查詢出對應(yīng)的鍵的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
python中的關(guān)鍵字參數(shù)*args和**kwargs詳解
這篇文章主要介紹了python中的關(guān)鍵字參數(shù)*args和**kwargs詳解,在定義類或函數(shù)時,有時候會用到*args和**kwargs,前者叫位置參數(shù),后者叫關(guān)鍵字參數(shù),需要的朋友可以參考下2023-11-11

