python實(shí)現(xiàn)無(wú)人機(jī)航拍圖片像素坐標(biāo)轉(zhuǎn)世界坐標(biāo)的示例代碼
背景
已知相機(jī)參數(shù)(傳感器寬度和高度、圖像寬度和高度、焦距、相對(duì)航高、像主點(diǎn)坐標(biāo) ),在給定像素坐標(biāo)的前提下,求世界坐標(biāo),大部分通過(guò)AI來(lái)實(shí)現(xiàn),不知道哪個(gè)步驟有問(wèn)題,望大家指正
腳本
import numpy as np
import cv2
# 畸變校正
def undistort_pixel(pixel_x, pixel_y, sym_dist, dec_dist):
k0,k1,k2,k3=sym_dist
# k1, k2, p1, p2, k3 = sym_dist
p1,p2,p3=dec_dist
fx = focal_length_mm
fy = focal_length_mm
cx = xpoff_px
cy = ypoff_px
distCoeffs = np.array([k1, k2, p1, p2,k3])
cameraMatrix = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]])
distorted_points = np.array([[pixel_x, pixel_y]], dtype=np.float32)
undistorted_points = cv2.undistortPoints(distorted_points, cameraMatrix, distCoeffs)
#################################################### 4\對(duì)圖像去畸變
img = cv2.imread('./images/100_0004_0001.JPG')
img_undistored = cv2.undistort(img, cameraMatrix, distCoeffs)
cv2.imwrite('./images/100_0004_00011.JPG', img_undistored)
return undistorted_points[0][0][0], undistorted_points[0][0][1]
# 相機(jī)坐標(biāo)轉(zhuǎn)世界坐標(biāo)
def camera_to_world_coordinates(cam_coords, pos):
# 獲取相機(jī)到世界的轉(zhuǎn)換參數(shù)
pos_x, pos_y, pos_z, roll, pitch, yaw = pos
# 將角度轉(zhuǎn)換為弧度
roll = np.radians(roll)
pitch = np.radians(pitch)
yaw = np.radians(yaw)
# 計(jì)算旋轉(zhuǎn)矩陣
R_roll = np.array([
[1, 0, 0],
[0, np.cos(roll), -np.sin(roll)],
[0, np.sin(roll), np.cos(roll)]
])
R_pitch = np.array([
[np.cos(pitch), 0, np.sin(pitch)],
[0, 1, 0],
[-np.sin(pitch), 0, np.cos(pitch)]
])
R_yaw = np.array([
[np.cos(yaw), -np.sin(yaw), 0],
[np.sin(yaw), np.cos(yaw), 0],
[0, 0, 1]
])
R = R_yaw @ R_pitch @ R_roll
# 相機(jī)坐標(biāo)轉(zhuǎn)換到世界坐標(biāo)
cam_coords_homogeneous = np.array([cam_coords[0], cam_coords[1], -H, 1])
world_coords = R @ cam_coords_homogeneous[:3] + np.array([pos_x, pos_y, pos_z])
return world_coords
if __name__ == "__main__":
####################################################基本參數(shù)
# 傳感器寬度和高度(毫米)
sensor_width_mm = 12.83331744000000007588
sensor_height_mm = 8.55554496000000064271
# 圖像寬度和高度(像素)
image_width_px = 5472
image_height_px = 3648
# 焦距(毫米)
focal_length_mm = 8.69244671863242679422
# 焦距(米)
focal_length_m = 8.69244671863242679422/1000
# 相對(duì)航高(米)
H=86.93
#像主點(diǎn)坐標(biāo) (像素)
xpoff_px=20.88973563438230485190
ypoff_px=50.51977022866981315019
#################################################### 1\計(jì)算空間分辨率
# 傳感器尺寸轉(zhuǎn)換為米
sensor_width_m = sensor_width_mm / 1000
sensor_height_m = sensor_height_mm / 1000
# 計(jì)算水平和垂直的 GSD
GSD_x = (sensor_width_m/image_width_px) * (H / focal_length_m )
GSD_y = (sensor_height_m /image_height_px) * (H / focal_length_m)
# 水平和垂直方向的 GSD
print("水平方向的 GSD:", GSD_x, "米/像素")
print("垂直方向的 GSD:", GSD_y, "米/像素")
#################################################### 2\給定像素坐標(biāo),計(jì)算相機(jī)坐標(biāo)
# 像素坐標(biāo)
oripixel_x = image_width_px
oripixel_y = image_height_px
# oripixel_x = image_width_px/2
# oripixel_y = image_height_px/2
# oripixel_x = 0
# oripixel_y = 0
pixel_x=oripixel_x-xpoff_px-image_width_px/2
pixel_y=oripixel_y-ypoff_px-image_height_px/2
# 計(jì)算相機(jī)坐標(biāo)(假設(shè)無(wú)畸變)
camera_x = pixel_x * GSD_x
camera_y = pixel_y * GSD_y
print("像素坐標(biāo) (", oripixel_x, ",", oripixel_y, ") 對(duì)應(yīng)的相機(jī)坐標(biāo) (x, y): (", camera_x, "米, ", camera_y, "米)")
#################################################### 3\計(jì)算畸變后坐標(biāo)
# 對(duì)稱(chēng)畸變系數(shù)
sym_dist = [0, -0.00043396118129128110, 0.00000262222711982075, -0.00000001047488706013]
# 徑向畸變
dec_dist = [0.00000205885592671873, -0.00000321714140091248, 0]
# 進(jìn)行畸變校正
undistorted_camera_x, undistorted_camera_y = undistort_pixel(pixel_x, pixel_y, sym_dist, dec_dist)
print("畸變校正后像素坐標(biāo) (", oripixel_x, ",", oripixel_y, ") 對(duì)應(yīng)的相機(jī)坐標(biāo) (x, y): (", undistorted_camera_x, "米, ", undistorted_camera_y, "米)")
#################################################### 4\計(jì)算世界坐標(biāo)
# POS數(shù)據(jù)
pos = [433452.054688, 2881728.519704, 183.789696, 0.648220, -0.226028, 14.490357]
# 計(jì)算世界坐標(biāo)
world_coords = camera_to_world_coordinates((undistorted_camera_x, undistorted_camera_y), pos)
print("旋轉(zhuǎn)平移變換后像素坐標(biāo) (", oripixel_x, ",", oripixel_y, ") 對(duì)應(yīng)的世界坐標(biāo) (x, y): (", world_coords[0], "米, ", world_coords[1], "米)")到此這篇關(guān)于python實(shí)現(xiàn)無(wú)人機(jī)航拍圖片像素坐標(biāo)轉(zhuǎn)世界坐標(biāo)的示例代碼的文章就介紹到這了,更多相關(guān)python無(wú)人機(jī)航拍圖片像素內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python實(shí)現(xiàn)WGS84火星百度及web墨卡托四種坐標(biāo)系相互轉(zhuǎn)換
- Python實(shí)現(xiàn)常見(jiàn)坐標(biāo)系的相互轉(zhuǎn)換
- Python實(shí)現(xiàn)常見(jiàn)的4種坐標(biāo)互相轉(zhuǎn)換
- 使用Python和GDAL給圖片加坐標(biāo)系的實(shí)現(xiàn)思路(坐標(biāo)投影轉(zhuǎn)換)
- Python經(jīng)緯度坐標(biāo)轉(zhuǎn)換為距離及角度的實(shí)現(xiàn)
- 解決python gdal投影坐標(biāo)系轉(zhuǎn)換的問(wèn)題
- 代碼分析Python地圖坐標(biāo)轉(zhuǎn)換
相關(guān)文章
Python基于pywinauto實(shí)現(xiàn)的自動(dòng)化采集任務(wù)
這篇文章主要介紹了Python基于pywinauto實(shí)現(xiàn)的自動(dòng)化采集任務(wù),模擬了輸入單詞, 復(fù)制例句, 獲取例句, 清空剪切板, 然后重復(fù)這個(gè)操作,需要的朋友可以參考下2023-04-04
利用Matlab繪制各類(lèi)特殊圖形的實(shí)例代碼
作為一個(gè)功能強(qiáng)大的工具軟件,Matlab具有很強(qiáng)的圖形處理功能,提供了大量的二維、三 維圖形函數(shù),這篇文章主要給大家介紹了關(guān)于如何利用Matlab繪制各類(lèi)特殊圖形的相關(guān)資料,需要的朋友可以參考下2021-07-07
Python結(jié)合PyWebView庫(kù)打造跨平臺(tái)桌面應(yīng)用
隨著Web技術(shù)的發(fā)展,將HTML/CSS/JavaScript與Python結(jié)合構(gòu)建桌面應(yīng)用成為可能,本文將系統(tǒng)講解如何使用PyWebView庫(kù)實(shí)現(xiàn)這一創(chuàng)新方案,希望對(duì)大家有一定的幫助2025-04-04
python測(cè)試驅(qū)動(dòng)開(kāi)發(fā)實(shí)例
這篇文章主要介紹了python測(cè)試驅(qū)動(dòng)開(kāi)發(fā)實(shí)例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
Python 實(shí)現(xiàn)網(wǎng)課實(shí)時(shí)監(jiān)控自動(dòng)簽到、打卡功能
這篇文章主要介紹了Python實(shí)現(xiàn)網(wǎng)課實(shí)時(shí)監(jiān)控自動(dòng)簽到,打卡功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Python使用paramiko連接遠(yuǎn)程服務(wù)器執(zhí)行Shell命令的實(shí)現(xiàn)
這篇文章主要介紹了Python使用paramiko連接遠(yuǎn)程服務(wù)器執(zhí)行Shell命令的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Keras搭建分類(lèi)網(wǎng)絡(luò)平臺(tái)VGG16?MobileNet?ResNet50
這篇文章主要為大家介紹了Keras搭建分類(lèi)網(wǎng)絡(luò)平臺(tái)VGG16?MobileNet?ResNet50,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Anaconda2下實(shí)現(xiàn)Python2.7和Python3.5的共存方法
今天小編就為大家分享一篇Anaconda2下實(shí)現(xiàn)Python2.7和Python3.5的共存方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Python多元非線性回歸及繪圖的實(shí)現(xiàn)
本文主要介紹了Python多元非線性回歸及繪圖的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04

