203 lines
5.1 KiB
Markdown
203 lines
5.1 KiB
Markdown
|
|
# ImageReproject Python版本
|
|||
|
|
|
|||
|
|
这是原C++项目`ImageReproject_linux`的Python实现版本,提供图像重投影、目标定位和红线重投影功能。
|
|||
|
|
|
|||
|
|
## 项目结构
|
|||
|
|
|
|||
|
|
- `img_types.py`: 定义了各种数据结构,如点坐标、相机参数、外方位元素等
|
|||
|
|
- `dpal.py`: 数字摄影测量算法库,实现坐标转换和旋转矩阵计算等功能
|
|||
|
|
- `image_reproject.py`: 主要功能实现,包括目标定位、红线重投影和图像重投影
|
|||
|
|
- `test_image_reproject.py`: 测试脚本,用于测试各项功能
|
|||
|
|
|
|||
|
|
## 功能说明
|
|||
|
|
|
|||
|
|
### 1. 目标定位 (Target Positioning)
|
|||
|
|
|
|||
|
|
根据图像上的点坐标,结合相机参数、云台角度、位置信息和DEM数据,计算该点在地面上的经纬度和高度。
|
|||
|
|
|
|||
|
|
### 2. 红线重投影 (Red Line Reproject)
|
|||
|
|
|
|||
|
|
将地面上的点坐标投影到图像上,并在图像上绘制这些点。
|
|||
|
|
|
|||
|
|
### 3. 图像重投影 (Image Reproject)
|
|||
|
|
|
|||
|
|
将地面上的点坐标投影到图像上,计算其在图像上的像素坐标。
|
|||
|
|
|
|||
|
|
## 依赖库
|
|||
|
|
|
|||
|
|
- NumPy: 用于数值计算
|
|||
|
|
- OpenCV (cv2): 用于图像处理
|
|||
|
|
- PyProj: 用于坐标转换
|
|||
|
|
- GDAL: 用于读取DEM数据
|
|||
|
|
|
|||
|
|
## 安装依赖
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
pip install numpy opencv-python pyproj gdal
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 使用示例
|
|||
|
|
|
|||
|
|
### 目标定位
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from img_types import CamParas
|
|||
|
|
from image_reproject import ir_target_positioning
|
|||
|
|
|
|||
|
|
# 设置参数
|
|||
|
|
u = 1520 # 图像x坐标
|
|||
|
|
v = 2512 # 图像y坐标
|
|||
|
|
interval = 0.05 # 高程迭代间隔
|
|||
|
|
gimbal_yaw = 66.40 * math.pi / 180 # 云台偏航角(弧度)
|
|||
|
|
gimbal_pitch = -90 * math.pi / 180 # 云台俯仰角(弧度)
|
|||
|
|
gimbal_roll = 0 # 云台横滚角(弧度)
|
|||
|
|
h = 590.834 # 高度
|
|||
|
|
l = (103 + 59 / 60.0 + 26.07 / 3600.0) * math.pi / 180 # 经度(弧度)
|
|||
|
|
b = (30 + 45 / 60.0 + 46.48 / 3600.0) * math.pi / 180 # 纬度(弧度)
|
|||
|
|
img_width = 4032 # 图像宽度
|
|||
|
|
img_height = 3024 # 图像高度
|
|||
|
|
|
|||
|
|
# 相机参数
|
|||
|
|
cam_paras = CamParas(
|
|||
|
|
fx=2795.68899,
|
|||
|
|
fy=2795.68899,
|
|||
|
|
cx=4.63568,
|
|||
|
|
cy=198.7387,
|
|||
|
|
k1=0.0199131,
|
|||
|
|
k2=-0.068595,
|
|||
|
|
k3=0.0568642,
|
|||
|
|
p1=-0.00119,
|
|||
|
|
p2=0.00124049
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# DEM文件路径
|
|||
|
|
dem_file = "dem.tif"
|
|||
|
|
|
|||
|
|
# 执行目标定位
|
|||
|
|
x, y, z = ir_target_positioning(
|
|||
|
|
u, v, interval, gimbal_pitch, gimbal_yaw, gimbal_roll,
|
|||
|
|
h, l, b, cam_paras, img_width, img_height, dem_file
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 输出结果
|
|||
|
|
print(f"目标位置: 经度={x * 180 / math.pi:.6f}°, 纬度={y * 180 / math.pi:.6f}°, 高度={z:.2f}m")
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 红线重投影
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from img_types import CamParas, Point
|
|||
|
|
from image_reproject import ir_red_line_reproject
|
|||
|
|
|
|||
|
|
# 设置参数
|
|||
|
|
gimbal_yaw = 66.40 * math.pi / 180 # 云台偏航角(弧度)
|
|||
|
|
gimbal_pitch = -90 * math.pi / 180 # 云台俯仰角(弧度)
|
|||
|
|
gimbal_roll = 0 # 云台横滚角(弧度)
|
|||
|
|
h = 590.834 # 高度
|
|||
|
|
l = (103 + 59 / 60.0 + 26.07 / 3600.0) * math.pi / 180 # 经度(弧度)
|
|||
|
|
b = (30 + 45 / 60.0 + 46.48 / 3600.0) * math.pi / 180 # 纬度(弧度)
|
|||
|
|
img_width = 4032 # 图像宽度
|
|||
|
|
img_height = 3024 # 图像高度
|
|||
|
|
|
|||
|
|
# 相机参数
|
|||
|
|
cam_paras = CamParas(
|
|||
|
|
fx=2795.68899,
|
|||
|
|
fy=2795.68899,
|
|||
|
|
cx=4.63568,
|
|||
|
|
cy=198.7387,
|
|||
|
|
k1=0.0199131,
|
|||
|
|
k2=-0.068595,
|
|||
|
|
k3=0.0568642,
|
|||
|
|
p1=-0.00119,
|
|||
|
|
p2=0.00124049
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 图像文件路径
|
|||
|
|
img_file = "test_image.jpg"
|
|||
|
|
img_save_file = "test_image_reprojected.jpg"
|
|||
|
|
|
|||
|
|
# 点坐标列表
|
|||
|
|
points = [
|
|||
|
|
Point(103.990109 * math.pi / 180, 30.762915 * math.pi / 180, 481.64),
|
|||
|
|
# 添加更多点...
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
# 执行红线重投影
|
|||
|
|
ir_red_line_reproject(
|
|||
|
|
gimbal_pitch, gimbal_yaw, gimbal_roll, h, l, b,
|
|||
|
|
cam_paras, img_width, img_height, points, img_file, img_save_file
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 图像重投影
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from img_types import CamParas
|
|||
|
|
from image_reproject import ir_img_reproject
|
|||
|
|
|
|||
|
|
# 设置参数
|
|||
|
|
obj_longitude = 103.990109 # 目标点经度(度)
|
|||
|
|
obj_latitude = 30.762915 # 目标点纬度(度)
|
|||
|
|
obj_height = 481.64 # 目标点高度
|
|||
|
|
|
|||
|
|
gimbal_yaw = 66.40 * math.pi / 180 # 云台偏航角(弧度)
|
|||
|
|
gimbal_pitch = -90 * math.pi / 180 # 云台俯仰角(弧度)
|
|||
|
|
gimbal_roll = 0 # 云台横滚角(弧度)
|
|||
|
|
h = 590.834 # 高度
|
|||
|
|
l = (103 + 59 / 60.0 + 26.07 / 3600.0) * math.pi / 180 # 经度(弧度)
|
|||
|
|
b = (30 + 45 / 60.0 + 46.48 / 3600.0) * math.pi / 180 # 纬度(弧度)
|
|||
|
|
img_width = 4032 # 图像宽度
|
|||
|
|
img_height = 3024 # 图像高度
|
|||
|
|
|
|||
|
|
# 相机参数
|
|||
|
|
cam_paras = CamParas(
|
|||
|
|
fx=2795.68899,
|
|||
|
|
fy=2795.68899,
|
|||
|
|
cx=4.63568,
|
|||
|
|
cy=198.7387,
|
|||
|
|
k1=0.0199131,
|
|||
|
|
k2=-0.068595,
|
|||
|
|
k3=0.0568642,
|
|||
|
|
p1=-0.00119,
|
|||
|
|
p2=0.00124049
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 执行图像重投影
|
|||
|
|
pixel_x, pixel_y = ir_img_reproject(
|
|||
|
|
gimbal_pitch, gimbal_yaw, gimbal_roll, h, l, b,
|
|||
|
|
cam_paras, img_width, img_height, obj_longitude, obj_latitude, obj_height
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 输出结果
|
|||
|
|
print(f"目标点在图像上的坐标: x={pixel_x:.2f}, y={pixel_y:.2f}")
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 打包为动态库
|
|||
|
|
|
|||
|
|
本项目支持通过 setuptools 打包为 Python 动态库(.so/.pyd)。
|
|||
|
|
|
|||
|
|
### 打包命令
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
python setup.py bdist_wheel
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
或直接安装到本地环境:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
pip install .
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 导入与调用
|
|||
|
|
|
|||
|
|
安装后可直接在 Python 中导入:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from imagereproject import ir_target_positioning, ir_red_line_reproject, ir_img_reproject, Point, CamParas
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 注意事项
|
|||
|
|
|
|||
|
|
1. 使用前请确保已安装所有依赖库
|
|||
|
|
2. DEM文件和图像文件路径需要根据实际情况修改
|
|||
|
|
3. 相机参数和位置信息需要根据实际情况设置
|