Python使用?OpenCV?進(jìn)行圖像投影變換
投影變換(仿射變換)
在數(shù)學(xué)中,線性變換是將一個(gè)向量空間映射到另一個(gè)向量空間的函數(shù),通常由矩陣實(shí)現(xiàn)。如果映射保留向量加法和標(biāo)量乘法,則映射被認(rèn)為是線性變換。
要將線性變換應(yīng)用于向量(即,一個(gè)點(diǎn)的坐標(biāo),在我們的例子中——像素的 x 和 y 值),需要將該向量乘以表示線性變換的矩陣。作為輸出,你將獲得一個(gè)坐標(biāo)轉(zhuǎn)換后的向量。
投影變換可以用以下矩陣表示:
其中:
是一個(gè)旋轉(zhuǎn)矩陣。該矩陣定義了將要執(zhí)行的變換類型:縮放、旋轉(zhuǎn)等。
是平移向量。它只是移動(dòng)點(diǎn)。
是投影向量。對(duì)于仿射變換,該向量的所有元素始終等于 0。
如果 x 和 y 是一個(gè)點(diǎn)的坐標(biāo),則可以通過(guò)簡(jiǎn)單的乘法進(jìn)行變換:
這里,x' 和 y' 是變換點(diǎn)的坐標(biāo)。
這就是仿射變換的全部理論?,F(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ù)源圖像,我們將得到相關(guān)坐標(biāo)如下:
[[左,上],[左,下],[右,上],[右,下]]
好的!現(xiàn)在我們使用 get_paste_position 來(lái)獲取目標(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])
點(diǎn)擊4個(gè)點(diǎn)后,我們將4個(gè)點(diǎn)保存到paste_coordinate_list:
while True: cv2.waitKey(1) if len(paste_coordinate) == 4: break
然后,這 4 個(gè)點(diǎn)將通過(guò) cv2.findHomography 計(jì)算投影變換矩陣。
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 進(jìn)行圖像投影變換的文章就介紹到這了,更多相關(guān)Python OpenCV 圖像投影內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
django模板語(yǔ)法學(xué)習(xí)之include示例詳解
寫(xiě)過(guò) Web 程序的都對(duì) include 包含文件很熟悉,那么在 Django,include 又是怎么一個(gè)機(jī)制呢?下面這篇文章主要給大家介紹了關(guān)于django模板語(yǔ)法學(xué)習(xí)之include的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12python實(shí)現(xiàn)音樂(lè)播放和下載小程序功能
這篇文章主要介紹了python實(shí)現(xiàn)音樂(lè)播放和下載小程序功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Python實(shí)現(xiàn)遞歸遍歷文件夾并刪除文件
本文給大家匯總了3個(gè)Python實(shí)現(xiàn)遍歷文件夾并刪除的代碼,主要是給大家分享下這3種方法的實(shí)現(xiàn)思路,有需要的小伙伴可以參考下2016-04-04python矩陣轉(zhuǎn)換為一維數(shù)組的實(shí)例
今天小編就為大家分享一篇python矩陣轉(zhuǎn)換為一維數(shù)組的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Python爬取網(wǎng)頁(yè)中的圖片(搜狗圖片)詳解
沒(méi)想到python是如此強(qiáng)大,令人著迷,以前看見(jiàn)圖片總是一張一張復(fù)制粘貼,現(xiàn)在好了,學(xué)會(huì)python就可以用程序?qū)⒁粡垙垐D片,保存下來(lái)。下面這篇文章主要給大家介紹了利用Python3.6爬取搜狗圖片網(wǎng)頁(yè)中圖片的相關(guān)資料,需要的朋友可以參考下。2017-03-03Python根據(jù)字典的值查詢出對(duì)應(yīng)的鍵的方法
這篇文章主要介紹了Python根據(jù)字典的值查詢出對(duì)應(yīng)的鍵的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python中的關(guān)鍵字參數(shù)*args和**kwargs詳解
這篇文章主要介紹了python中的關(guān)鍵字參數(shù)*args和**kwargs詳解,在定義類或函數(shù)時(shí),有時(shí)候會(huì)用到*args和**kwargs,前者叫位置參數(shù),后者叫關(guān)鍵字參數(shù),需要的朋友可以參考下2023-11-11