基于c++和Python的单像空间后方交会

2024-01-27 14:10

本文主要是介绍基于c++和Python的单像空间后方交会,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        学习摄影测量之后一直都很想用代码实现一下这个特别经典的,难度又不是很大的课堂案例,但是由于一直在看其他开源项目的代码,一直被搁置,趁着暑假完成一下曾经的小目标。

        没想到这东西看起来步骤清晰且简单,实现起来竟然踩了特别多的坑,主要就是Python中的数组和基于OpenCV的c++Mat这两者用起来差别有点大,搞得我晕头转向的,很难受......

 一、Python实现:

我先用Python代码实现了一下,毕竟python代码比较......

#空间后方交会
import numpy as np
m = 50000#比例尺分母
limit = 0.0001#迭代限差
def Space_resection(f, X, Y, Z, x, y, H):#初始化x = np.array(x, dtype=np.float32)y = np.array(y, dtype=np.float32)X = np.array(X, dtype=np.float32)Y = np.array(Y, dtype=np.float32)Z = np.array(Z, dtype=np.float32)phi = 0omg = 0kappa = 0Xs = sum(X) / 4Ys = sum(Y) / 4Zs = m * fiteration_num = 50000for i in range(iteration_num):#计算旋转矩阵元素a1 = np.cos(phi) * np.cos(kappa) - np.sin(phi) * np.sin(omg) * np.sin(kappa)a2 = -np.cos(phi) * np.sin(kappa) - np.sin(phi) * np.sin(omg) * np.cos(kappa)a3 = -np.sin(phi) * np.cos(omg)b1 = np.cos(omg) * np.sin(kappa)b2 = np.cos(omg) * np.cos(kappa)b3 = -np.sin(omg)c1 = np.sin(phi) * np.cos(kappa) + np.cos(phi) * np.sin(omg) * np.sin(kappa)c2 = -np.sin(phi) * np.sin(kappa) + np.cos(phi) * np.sin(omg) * np.cos(kappa)c3 = np.cos(phi) * np.cos(omg)R = np.mat([[a1, a2, a3], [b1, b2, b3], [c1, c2, c3]])# print(R)num = len(x)#物点/像点数量# print(num)for j in range(num):#初始化系数矩阵a,常数项矩阵l和近似值x,ya11 = np.zeros(num, dtype=np.float32)a12 = np.zeros(num, dtype=np.float32)a13 = np.zeros(num, dtype=np.float32)a14 = np.zeros(num, dtype=np.float32)a15 = np.zeros(num, dtype=np.float32)a16 = np.zeros(num, dtype=np.float32)a21 = np.zeros(num, dtype=np.float32)a22 = np.zeros(num, dtype=np.float32)a23 = np.zeros(num, dtype=np.float32)a24 = np.zeros(num, dtype=np.float32)a25 = np.zeros(num, dtype=np.float32)a26 = np.zeros(num, dtype=np.float32)l11 = np.zeros(num, dtype=np.float32)l12 = np.zeros(num, dtype=np.float32)appro_x = np.zeros(num, dtype=np.float32)appro_y = np.zeros(num, dtype=np.float32)for j in range(num):#计算每个点的系数矩阵a,常数项矩阵l和近似值x,yappro_x[j] = -f * (a1 * (X[j] - Xs) + b1 * (Y[j] - Ys) + c1 * (Z[j] - Zs)) / (a3 * (X[j] - Xs) + b3 * (Y[j] - Ys) + c3 * (Z[j] - Zs))appro_y[j] = -f * (a2 * (X[j] - Xs) + b2 * (Y[j] - Ys) + c2 * (Z[j] - Zs)) / (a3 * (X[j] - Xs) + b3 * (Y[j] - Ys) + c3 * (Z[j] - Zs))# print(appro_x[j],appro_y[j])a11[j] = -f / Ha12[j] = 0a13[j] = -x[j] / Ha14[j] = -f * (1 + (x[j] * x[j]) / (f * f))a15[j] = -x[j] * y[j] / fa16[j] = y[j]a21[j] = 0a22[j] = -f / Ha23[j] = -y[j] / Ha24[j] = -x[j] * y[j] / fa25[j] = -f * (1 + (y[j] * y[j]) / (f * f))a26[j] = -x[j]#组合A,LA = np.zeros((2 * num, 6), dtype=np.float32)for j in range(num):A[2 * j:2 * j + 2, :] = np.array([[a11[j], a12[j], a13[j], a14[j], a15[j], a16[j]], [a21[j], a22[j], a23[j], a24[j], a25[j], a26[j]]])#print(A)L = np.zeros((2 * num, 1), dtype=np.float32)for j in range(num):L[2 * j:2 * j + 2, :] = np.mat([[x[j] - appro_x[j]], [y[j] - appro_y[j]]])#print(L)#计算ATA,ATL以及改正数X=(ATA)-1(ATL)ATA = np.dot(A.T, A)ATL = np.dot(A.T, L)X_mid = np.dot(np.linalg.inv(ATA), A.T)result = np.dot(X_mid, L)#求新值Xs += result[0,0]Ys += result[1,0]Zs += result[2,0]phi += result[3,0]omg += result[4,0]kappa += result[5,0]if (abs(result[0,0]) < limit and abs(result[1,0]) < limit and abs(result[2,0]) < limit and abs(result[3,0]) < limit and abs(result[4,0]) < limit and abs(result[5,0]) < limit):print("精度满足")return (Xs,Ys,Zs,phi,omg,kappa)else:print(i)print("继续")# print(result)
def main():f = 153.24 / 1000H = f * m#航高x = [-86.15 / 1000, -53.40 / 1000, -14.78 / 1000, 10.46 / 1000]y = [-68.99 / 1000, 82.21 / 1000, -76.63 / 1000, 64.43 / 1000]X = [36589.41, 37631.08, 39100.97, 40426.54]Y = [25273.32, 31324.51, 24934.98, 30319.81]Z = [2195.17, 728.69, 2386.50, 757.31]result = Space_resection(f, X, Y, Z, x, y, H)print(result)
if __name__ == '__main__':main()

运行结果是这样的:

二、c++实现:

#include <iostream>
#include<fstream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
const int m = 50000;//比例尺分母
const double limit = 0.000001;//迭代限差
const int N = 4;//物点/像点个数,根据实际情况修改
const int iteration_num = 50000;//设定的迭代次数
Mat Space_resection(double f, double* x, double* y, double* X, double* Y, double* Z, double H, Mat& result)
{double sumX = 0.0, sumY = 0.0;for (int i = 0; i < N; i++){sumX += *(X + i);}for (int i = 0; i < N; i++){sumY += *(Y + i);}//参数初始化double phi = 0.0, omg = 0.0, kappa = 0.0;double Xs = sumX / 4;double Ys = sumY / 4;double Zs = m * f;for (int times = 1; times <= iteration_num; times++)//开始迭代{//计算旋转矩阵double a1 = cos(phi) * cos(kappa) - sin(phi) * sin(omg) * sin(kappa);double a2 = -cos(phi) * sin(kappa) - sin(phi) * sin(omg) * cos(kappa);double a3 = -sin(phi) * cos(omg);double b1 = cos(omg) * sin(kappa);double b2 = cos(omg) * cos(kappa);double b3 = -sin(omg);double c1 = sin(phi) * cos(kappa) + cos(phi) * sin(omg) * sin(kappa);double c2 = -sin(phi) * sin(kappa) + cos(phi) * sin(omg) * cos(kappa);double c3 = cos(phi) * cos(omg);Mat R = (Mat_<double>(3, 3) << a1, a2, a3, b1, b2, b3, c1, c2, c3);//初始化A,Ldouble appro_x[N] = { 0 };double appro_y[N] = { 0 };double a11[N] = { 0 };double a12[N] = { 0 };double a13[N] = { 0 };double a14[N] = { 0 };double a15[N] = { 0 };double a16[N] = { 0 };double a21[N] = { 0 };double a22[N] = { 0 };double a23[N] = { 0 };double a24[N] = { 0 };double a25[N] = { 0 };double a26[N] = { 0 };double lx[N] = { 0 };double ly[N] = { 0 };//逐点计算A,Lfor (int j = 0; j < N; j++){appro_x[j] = -f * (a1 * (*(X+j) - Xs) + b1 * (*(Y+j) - Ys) + c1 * (*(Z + j) - Zs)) /(a3 * (*(X + j) - Xs) + b3 * (*(Y + j) - Ys) + c3 * (*(Z + j) - Zs));appro_y[j] = -f * (a2 * (*(X + j) - Xs) + b2 * (*(Y + j) - Ys) + c2 * (*(Z + j) - Zs)) /(a3 * (*(X + j) - Xs) + b3 * (*(Y + j) - Ys) + c3 * (*(Z + j) - Zs));lx[j] = *(x + j) - appro_x[j];ly[j] = *(y + j) - appro_y[j];a11[j] = -f / H;a12[j] = 0;a13[j] = -*(x + j) / H;a14[j] = -f * (1 + (*(x + j) *  *(x + j)) / (f * f));a15[j] = -*(x + j) * *(y + j) / f;a16[j] = *(y + j);a21[j] = 0;a22[j] = -f / H;a23[j] = -*(y + j) / H;a24[j] = -*(x + j) * *(y + j) / f;a25[j] = -f * (1 + (*(y + j) * *(y + j)) / (f * f));a26[j] = -*(x + j);}//组合总的A,LMat A(2 * N, 6,CV_64F);Mat L(2 * N, 1, CV_64F);int t = 0, t2 = 0;for (int j = 0; j < 2 * N; j += 2){Mat mid = (Mat_<double>(2, 6) << a11[t], a12[t], a13[t], a14[t], a15[t], a16[t], a21[t], a22[t], a23[t], a24[t], a25[t], a26[t]);for (int row = 0; row < 2; row++){for (int col = 0; col < 6; col++){A.at<double>(row + 2 * t, col) = mid.at<double>(row, col);}}t++;}for (int j = 0; j < 2 * N; j += 2){Mat mid = (Mat_<double>(2, 1) << lx[t2], ly[t2]);for (int row = 0; row < 2; row++){for (int col = 0; col < 1; col++){L.at<double>(row + 2 * t2, col) = mid.at<double>(row, col);}}t2++;}//计算ATA,ATL和XMat ATA = A.t() * A;Mat ATL = A.t() * L;Mat X = (ATA).inv() * A.t()*L;//求新值Xs += X.at<double>(0, 0);Ys += X.at<double>(1, 0);Zs += X.at<double>(2, 0);phi += X.at<double>(3, 0);omg += X.at<double>(4, 0);kappa += X.at<double>(5, 0);//与限差比较if (fabs(X.at<double>(0, 0)) < limit && fabs(X.at<double>(1, 0)) < limit && fabs(X.at<double>(2, 0)) < limit && fabs(X.at<double>(3, 0)) < limit && fabs(X.at<double>(4, 0)) < limit && fabs(X.at<double>(5, 0)) < limit){cout << times << endl;cout << "精度满足";result = (Mat_<double>(6, 1) << Xs, Ys, Zs, phi, omg, kappa);return result;}else{result = (Mat_<double>(6, 1) << X.at<double>(0, 0), X.at<double>(1, 0), X.at<double>(2, 0), X.at<double>(3, 0), X.at<double>(4, 0), X.at<double>(5, 0));}}//return result;
}
int main()
{double f = 153.24 / 1000;double H = f * m;ifstream fin1;ifstream fin2;//从文件中读取物点坐标,像点坐标fin1.open("D:\\Software\\VS2019\\source\\repos\\摄影测量与三维重建\\单片空间后方交会\\像点坐标.txt");fin2.open("D:\\Software\\VS2019\\source\\repos\\摄影测量与三维重建\\单片空间后方交会\\物点坐标.txt");double x[N];double y[N];double X[N];double Y[N];double Z[N];char strx, stry, strX, strY, strZ;fin1 >> strx >> stry;for (int i = 0; i < N; i++){fin1 >> x[i] >> y[i];x[i] /= 1000;y[i] /= 1000;}fin2 >> strX >> strY >> strZ;for (int i = 0; i < N; i++){fin2 >> X[i] >> Y[i] >> Z[i];}Mat result=Mat::zeros(Size(6,1), CV_64F);Mat final_result(6, 1, CV_64F);final_result = Space_resection(f, x, y, X, Y, Z, H, result);cout << final_result;return 0;
}

这个代码要跑起来需要配置OpenCV库,我用的OpenCV里面的Mat,具体配置过程参考这个吧VS配置OpenCV库。

然后就是考虑到这只是一个小案例,实际生产生活中我们有海量的物点和像点,所以我采用了读取文件的方式,这两个文本文件格式如下:

 然后对照着我的代码,应该很容易能看明白。

运行结果如下:和Python的结果差不多,但是就这么一个小程序我都能明显感觉到c++确实比Python运行的快很多。

这篇关于基于c++和Python的单像空间后方交会的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/650472

相关文章

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句