脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - python实现无人机航拍图片像素坐标转世界坐标的示例代码

python实现无人机航拍图片像素坐标转世界坐标的示例代码

2024-06-13 17:02GIS从业者 Python

已知相机参数在给定像素坐标的前提下,求世界坐标,大部分通过AI来实现,本文给大家分享实现脚本,感兴趣的朋友跟随小编一起看看吧

背景

已知相机参数(传感器宽度和高度、图像宽度和高度、焦距、相对航高、像主点坐标 ),在给定像素坐标的前提下,求世界坐标,大部分通过AI来实现,不知道哪个步骤有问题,望大家指正

脚本

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\对图像去畸变
    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]
# 相机坐标转世界坐标
def camera_to_world_coordinates(cam_coords, pos):
    # 获取相机到世界的转换参数
    pos_x, pos_y, pos_z, roll, pitch, yaw = pos
    # 将角度转换为弧度
    roll = np.radians(roll)
    pitch = np.radians(pitch)
    yaw = np.radians(yaw)
    # 计算旋转矩阵
    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
    # 相机坐标转换到世界坐标
    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__":
    ####################################################基本参数
    # 传感器宽度和高度(毫米)
    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
    # 相对航高(米)
    H=86.93
    #像主点坐标 (像素)
    xpoff_px=20.88973563438230485190
    ypoff_px=50.51977022866981315019
    #################################################### 1\计算空间分辨率
    # 传感器尺寸转换为米
    sensor_width_m = sensor_width_mm / 1000
    sensor_height_m = sensor_height_mm / 1000
    # 计算水平和垂直的 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\给定像素坐标,计算相机坐标
    # 像素坐标
    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
    # 计算相机坐标(假设无畸变)
    camera_x = pixel_x * GSD_x
    camera_y = pixel_y * GSD_y
    print("像素坐标 (", oripixel_x, ",", oripixel_y, ") 对应的相机坐标 (x, y): (", camera_x, "米, ", camera_y, "米)")
    #################################################### 3\计算畸变后坐标
    # 对称畸变系数
    sym_dist = [0, -0.00043396118129128110, 0.00000262222711982075, -0.00000001047488706013]
    # 径向畸变
    dec_dist = [0.00000205885592671873, -0.00000321714140091248, 0]
    # 进行畸变校正
    undistorted_camera_x, undistorted_camera_y = undistort_pixel(pixel_x, pixel_y, sym_dist, dec_dist)
    print("畸变校正后像素坐标 (", oripixel_x, ",", oripixel_y, ") 对应的相机坐标 (x, y): (", undistorted_camera_x, "米, ", undistorted_camera_y, "米)")
    #################################################### 4\计算世界坐标
    # POS数据
    pos = [433452.054688, 2881728.519704, 183.789696, 0.648220, -0.226028, 14.490357]
    # 计算世界坐标
    world_coords = camera_to_world_coordinates((undistorted_camera_x, undistorted_camera_y), pos)
    print("旋转平移变换后像素坐标 (", oripixel_x, ",", oripixel_y, ") 对应的世界坐标 (x, y): (", world_coords[0], "米, ", world_coords[1], "米)")

到此这篇关于python实现无人机航拍图片像素坐标转世界坐标的示例代码的文章就介绍到这了,更多相关python无人机航拍图片像素内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/yilvyangguang520/article/details/139598783

延伸 · 阅读

精彩推荐
  • Python浅谈Pandas Series 和 Numpy array中的相同点

    浅谈Pandas Series 和 Numpy array中的相同点

    今天小编就为大家分享一篇浅谈Pandas Series 和 Numpy array中的相同点,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    ARVRinChina4032021-07-25
  • Pythonpython爬虫如何解决图片验证码

    python爬虫如何解决图片验证码

    这篇文章主要介绍了python爬虫如何解决图片验证码,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下...

    青衫不改就人还12332021-09-04
  • Python深入解析python返回函数和匿名函数

    深入解析python返回函数和匿名函数

    这篇文章主要介绍了python返回函数和匿名函数的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的...

    礁之4782022-12-12
  • PythonPython+OpenCV图片局部区域像素值处理详解

    Python+OpenCV图片局部区域像素值处理详解

    这篇文章主要为大家详细介绍了Python+OpenCV图片局部区域像素值处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    零尾8442021-05-21
  • Pythonpython操作csv格式文件之csv.DictReader()方法

    python操作csv格式文件之csv.DictReader()方法

    这篇文章主要介绍了python操作csv格式文件之csv.DictReader()方法,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下...

    booze-J9512022-06-28
  • Python微信跳一跳游戏python脚本

    微信跳一跳游戏python脚本

    这篇文章主要为大家详细介绍了微信跳一跳游戏python脚本,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    sugar-shi11262021-01-01
  • PythonPython 如何调试程序崩溃错误

    Python 如何调试程序崩溃错误

    这篇文章主要介绍了Python 如何调试程序崩溃错误,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下 ...

    David Beazley6952020-08-04
  • Pythonpython如何获取Prometheus监控数据

    python如何获取Prometheus监控数据

    这篇文章主要介绍了python如何获取Prometheus监控数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    Jiang_James_L10592023-01-28