python opencv實(shí)現(xiàn)任意角度的透視變換實(shí)例代碼
本文主要分享的是一則python+opencv實(shí)現(xiàn)任意角度的透視變換的實(shí)例,具體如下:
# -*- coding:utf-8 -*- import cv2 import numpy as np def rad(x): return x * np.pi / 180 img = cv2.imread("6.jfif") cv2.imshow("original", img) # 擴(kuò)展圖像,保證內(nèi)容不超出可視范圍 img = cv2.copyMakeBorder(img, 200, 200, 200, 200, cv2.BORDER_CONSTANT, 0) w, h = img.shape[0:2] anglex = 0 angley = 30 anglez = 0 #是旋轉(zhuǎn) fov = 42 while 1: # 鏡頭與圖像間的距離,21為半可視角,算z的距離是為了保證在此可視角度下恰好顯示整幅圖像 z = np.sqrt(w ** 2 + h ** 2) / 2 / np.tan(rad(fov / 2)) # 齊次變換矩陣 rx = np.array([[1, 0, 0, 0], [0, np.cos(rad(anglex)), -np.sin(rad(anglex)), 0], [0, -np.sin(rad(anglex)), np.cos(rad(anglex)), 0, ], [0, 0, 0, 1]], np.float32) ry = np.array([[np.cos(rad(angley)), 0, np.sin(rad(angley)), 0], [0, 1, 0, 0], [-np.sin(rad(angley)), 0, np.cos(rad(angley)), 0, ], [0, 0, 0, 1]], np.float32) rz = np.array([[np.cos(rad(anglez)), np.sin(rad(anglez)), 0, 0], [-np.sin(rad(anglez)), np.cos(rad(anglez)), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32) r = rx.dot(ry).dot(rz) # 四對(duì)點(diǎn)的生成 pcenter = np.array([h / 2, w / 2, 0, 0], np.float32) p1 = np.array([0, 0, 0, 0], np.float32) - pcenter p2 = np.array([w, 0, 0, 0], np.float32) - pcenter p3 = np.array([0, h, 0, 0], np.float32) - pcenter p4 = np.array([w, h, 0, 0], np.float32) - pcenter dst1 = r.dot(p1) dst2 = r.dot(p2) dst3 = r.dot(p3) dst4 = r.dot(p4) list_dst = [dst1, dst2, dst3, dst4] org = np.array([[0, 0], [w, 0], [0, h], [w, h]], np.float32) dst = np.zeros((4, 2), np.float32) # 投影至成像平面 for i in range(4): dst[i, 0] = list_dst[i][0] * z / (z - list_dst[i][2]) + pcenter[0] dst[i, 1] = list_dst[i][1] * z / (z - list_dst[i][2]) + pcenter[1] warpR = cv2.getPerspectiveTransform(org, dst) result = cv2.warpPerspective(img, warpR, (h, w)) cv2.imshow("result", result) c = cv2.waitKey(30) # anglex += 3 #auto rotate # anglez += 1 #auto rotate # angley += 2 #auto rotate # 鍵盤(pán)控制 if 27 == c: # Esc quit break; if c == ord('w'): anglex += 1 if c == ord('s'): anglex -= 1 if c == ord('a'): angley += 1 # dx=0 if c == ord('d'): angley -= 1 if c == ord('u'): anglez += 1 if c == ord('p'): anglez -= 1 if c == ord('t'): fov += 1 if c == ord('r'): fov -= 1 if c == ord(' '): anglex = angley = anglez = 0 if c == ord('q'): print("======================================") print('旋轉(zhuǎn)矩陣:\n', r) print("angle alpha: ", anglex, 'angle beta: ', angley, "dz: ", anglez, ": ", z) cv2.destroyAllWindows()
總結(jié)
以上就是本文關(guān)于python opencv實(shí)現(xiàn)任意角度的透視變換實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
BatchNorm2d原理、作用及pytorch中BatchNorm2d函數(shù)的參數(shù)使用
這篇文章主要介紹了BatchNorm2d原理、作用及pytorch中BatchNorm2d函數(shù)的參數(shù)使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12為2021年的第一場(chǎng)雪錦上添花:用matplotlib繪制雪花和雪景
這篇文章主要介紹了為2021年的第一場(chǎng)雪錦上添花:用matplotlib繪制雪花和雪景,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01使用Python實(shí)現(xiàn)在Word文檔中進(jìn)行郵件合并
郵件合并是現(xiàn)代辦公中一項(xiàng)顯著提升效率的技術(shù),它巧妙地將大量個(gè)體數(shù)據(jù)與預(yù)設(shè)的文檔模板相結(jié)合,實(shí)現(xiàn)了一次性批量生成定制化文檔,下面我們就來(lái)看看如何使用Python實(shí)現(xiàn)在Word文檔中進(jìn)行郵件合并吧2024-04-04python 字典 setdefault()和get()方法比較詳解
這篇文章主要介紹了python 字典 setdefault()和get()方法比較詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Python調(diào)用系統(tǒng)命令os.system()和os.popen()的實(shí)現(xiàn)
這篇文章主要介紹了Python調(diào)用系統(tǒng)命令os.system()和os.popen()的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12本地文件上傳到七牛云服務(wù)器示例(七牛云存儲(chǔ))
這篇文章主要介紹了使用PYTHON把本地文件上傳到七牛云服務(wù)的方法,開(kāi)發(fā)環(huán)境是Python 2.7,大家參考使用吧2014-01-01Python如何telnet到網(wǎng)絡(luò)設(shè)備
這篇文章主要介紹了Python如何telnet到網(wǎng)絡(luò)設(shè)備,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-02-02