Opencv-Python圖像透視變換cv2.warpPerspective的示例
Opencv-Python圖像透視變換cv2.warpPerspective
代碼如下:
# -*- coding:utf-8 -*-
import cv2
import numpy as np
import sys
img = cv2.imread('test.jpg')
# 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
r = 0
def rad(x):
return x * np.pi / 180
def get_warpR():
global anglex,angley,anglez,fov,w,h,r
# 鏡頭與圖像間的距離,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)
# 四對點(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)
return warpR
def control():
global anglex,angley,anglez,fov,r
# 鍵盤控制
if 27 == c: # Esc quit
sys.exit()
if c == ord('w'):
anglex += 1
if c == ord('s'):
anglex -= 1
if c == ord('a'):
angley += 1
print(angley)
# 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('e'):
print("======================================")
print('Rotation Matrix:')
print(r)
print('angle alpha(anglex):')
print(anglex)
print('angle beta(angley):')
print(angley)
print('dz(anglez):')
print(anglez)
while True:
warpR = get_warpR()
result = cv2.warpPerspective(img, warpR, (h, w))
cv2.namedWindow('result',2)
cv2.imshow("result", result)
c = cv2.waitKey(30)
control()
cv2.destroyAllWindows()
運(yùn)行效果:

控制:
- s控制垂直方向上的形變
- a和d控制水平方向上的行變
- u和p控制角度旋轉(zhuǎn)
- e 輸出當(dāng)前旋轉(zhuǎn)矩陣參數(shù)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
python3 實(shí)現(xiàn)mysql數(shù)據(jù)庫連接池的示例代碼
這篇文章主要介紹了python3 實(shí)現(xiàn)mysql數(shù)據(jù)庫連接池的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
使用Python進(jìn)行數(shù)據(jù)清洗和預(yù)處理的實(shí)現(xiàn)代碼
Python作為數(shù)據(jù)科學(xué)領(lǐng)域的熱門編程語言,提供了豐富的庫和工具來處理和清洗數(shù)據(jù),本文將介紹如何使用Python進(jìn)行數(shù)據(jù)清洗和預(yù)處理,并提供相應(yīng)的代碼示例,需要的朋友可以參考下2024-05-05
python編寫實(shí)現(xiàn)抽獎(jiǎng)器
這篇文章主要為大家詳細(xì)介紹了python編寫實(shí)現(xiàn)抽獎(jiǎng)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
Django中反向生成models.py的實(shí)例講解
今天小編就為大家分享一篇Django中反向生成models.py的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
使用python分析統(tǒng)計(jì)自己微信朋友的信息
這篇文章主要介紹了python分析統(tǒng)計(jì)自己微信朋友的信息,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07

