Python实现三边测量定位与OpenCV三角测量代码解析

闻溪
闻溪
发布时间:2026-10-14| 5人浏览过
原创

本文介绍了基于Python的三边测量定位实现方法及OpenCV三角测量代码。通过随机生成参考节点与盲节点,计算欧式距离并求解定位坐标,结合matplotlib可视化结果。同时提供了使用OpenCV进行三角测量的示例,涉及相机投影矩阵与像素坐标转换,旨在为相关技术学习提供参考。

定位原理相对基础,此处不再赘述,直接展示包含详细注释的源代码。(若内容对您的学习有帮助,欢迎点赞支持,感谢)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May 16 10:50:29 2018
@author: dag
"""
import sympy
import numpy as np
import math
from matplotlib.pyplot import plot
from matplotlib.pyplot import show
import matplotlib.pyplot as plt
import matplotlib
#解决无法显示中文问题,fname是加载字体路径,根据自身pc实际确定,具体请百度
zhfont1 = matplotlib.font_manager.FontProperties(fname='/System/Library/Fonts/Hiragino Sans GB W3.ttc')
 
#随机产生3个参考节点坐标
maxy = 1000
maxx = 1000
cx = maxx*np.random.rand(3)
cy = maxy*np.random.rand(3)
dot1 = plot(cx,cy,'k^')
 
#生成盲节点,以及其与参考节点欧式距离
mtx = maxx*np.random.rand()
mty = maxy*np.random.rand()
plt.hold('on')
dot2 = plot(mtx,mty,'go')
da = math.sqrt(np.square(mtx-cx[0])+np.square(mty-cy[0]))
db = math.sqrt(np.square(mtx-cx[1])+np.square(mty-cy[1])) 
dc = math.sqrt(np.square(mtx-cx[2])+np.square(mty-cy[2]))
 
#计算定位坐标  
def triposition(xa,ya,da,xb,yb,db,xc,yc,dc): 
    x,y = sympy.symbols('x y')
    f1 = 2*x*(xa-xc)+np.square(xc)-np.square(xa)+2*y*(ya-yc)+np.square(yc)-np.square(ya)-(np.square(dc)-np.square(da))
    f2 = 2*x*(xb-xc)+np.square(xc)-np.square(xb)+2*y*(yb-yc)+np.square(yc)-np.square(yb)-(np.square(dc)-np.square(db))
    result = sympy.solve([f1,f2],[x,y])
    locx,locy = result[x],result[y]
    return [locx,locy]
    
#解算得到定位节点坐标
[locx,locy] = triposition(cx[0],cy[0],da,cx[1],cy[1],db,cx[2],cy[2],dc)
plt.hold('on')
dot3 = plot(locx,locy,'r*')
 
#显示脚注
x = [[locx,cx[0]],[locx,cx[1]],[locx,cx[2]]]
y = [[locy,cy[0]],[locy,cy[1]],[locy,cy[2]]]
for i in range(len(x)):
    plt.plot(x[i],y[i],linestyle = '--',color ='g' )
plt.title('三边测量法的定位',fontproperties=zhfont1)  
plt.legend(['参考节点','盲节点','定位节点'], loc='lower right',prop=zhfont1)
show() 
derror = math.sqrt(np.square(locx-mtx) + np.square(locy-mty)) 
print(derror) 

运行效果展示如下:

补充内容:利用Python和OpenCV实现三角测量(triangulation)

具体代码实现如下:


import cv2
import numpy as np
import scipy.io as scio
if __name__ == '__main__':
    print("main function.")
    #验证点
    point = np.array([1.0 ,2.0, 3.0])
    #获取相机参数
    cams_data = scio.loadmat('/data1/dy/SuperSMPL/data/AMAfMvS_Dataset/cameras_I_crane.mat')
    Pmats = cams_data['Pmats']  # Pmats(8, 3, 4) 投影矩阵 
    P1 = Pmats[0,::]
    P3 = Pmats[2,::]
    #通过投影矩阵将点从世界坐标投到像素坐标
    pj1 = np.dot(P1, np.vstack([point.reshape(3,1),np.array([1])]))
    pj3 = np.dot(P3, np.vstack([point.reshape(3,1),np.array([1])]))
    point1 = pj1[:2,:]/pj1[2,:]#两行一列,齐次坐标转化
    point3 = pj3[:2,:]/pj3[2,:]
    #利用投影矩阵以及对应像素点,进行三角测量
    points = cv2.triangulatePoints(P1,P3,point1,point3)
    #齐次坐标转化并输出
    print(points[0:3,:]/points[3,:])

以上内容为个人经验总结,希望能为您提供参考。感谢大家的支持,如有疏漏或错误之处,敬请指正。

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。