python opencv實現任意角度的透視變換執行個體代碼,pythonopencv

來源:互聯網
上載者:User

python opencv實現任意角度的透視變換執行個體代碼,pythonopencv

本文主要分享的是一則python+opencv實現任意角度的透視變換的執行個體,具體如下:

# -*- coding:utf-8 -*-import cv2import numpy as npdef rad(x):  return x * np.pi / 180img = cv2.imread("6.jfif")cv2.imshow("original", img)# 擴充映像,保證內容不超出可視範圍img = cv2.copyMakeBorder(img, 200, 200, 200, 200, cv2.BORDER_CONSTANT, 0)w, h = img.shape[0:2]anglex = 0angley = 30anglez = 0 #是旋轉fov = 42while 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)  # 四對點的產生  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  # 鍵盤控制  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('旋轉矩陣:\n', r)    print("angle alpha: ", anglex, 'angle beta: ', angley, "dz: ", anglez, ": ", z)cv2.destroyAllWindows()

總結

以上就是本文關於python opencv實現任意角度的透視變換執行個體代碼的全部內容,希望對大家有所協助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.