摄影测量外方位元素代码

航摄相片的外方位元素表示的是摄影摄影瞬间相片上的点对于地面上的点之间的关系的一些参数,在测绘工作中,如果求出了一张航拍相片的外方位元素,那么就可以根据像素点的坐标计算出对应的地面点的坐标,而这些解算过程计算量是非常大的。所以,编写一个能够计算航空摄影测量外方位元素的程序就很有必要了。今天,小博根据所学专业知识完成了该程序的编写,下面是该程序的详细算法,希望对大家有所帮助。

为了方便大家在测绘技术上的交流,小博创建了一个QQ群,欢迎大家加入测绘技术交流QQ群234035436

程序图片


程序计算原理

单张影像的空间后方交会:利用已知地面控制点数据及相应像点坐标根据共线方程反求影像的外方位元素。  

数学模型为共线条件方程式:

求解过程: 

(1)获取已知数据。从航摄资料中查取平均航高与摄影机主距;获取控制点的地面测量坐标并转换为地面摄影测量坐标。 

(2)量测控制点的像点坐标并做系统改正。 

(3)确定未知数的初始值。在竖直摄影且地面控制点大致分布均匀的情况下,按如下方法确定初始值,即:


             q=w=k=0 

             式中:m为摄影比例尺分母;n为控制点个数。  

(4)用三个角元素的初始值,计算个方向余弦,组成旋转矩阵R。 

(5)逐点计算像点坐标的近似值。利用未知数的近似值和控制点的地面

坐标代入共线方程式,逐点计算像点坐标的近似值(x)、(y)。 

(6)逐点计算误差方程式的系数和常数项,组成误差方程式。 

(7)计算法方程的系数矩阵A和常数项L,组成法方程式。 

(8)解法方程,求得外方位元素的改正数dXs,dYs,dZs,dq,dw,dk。 

(9)用前次迭代取得的近似值,加本次迭代的改正数,计算外方位元素

的新值。

(10)将求得的外方位元素改正数与规定的限差比较,若小于限差则迭代结束。否则用新的近似值重复(4)~(9),直到满足要求为止。

程序设计及代码

(1)导入数据时读取文件的代码
其中要导入的txt文件数据结构如图所示:

代码:

     
     
  1. #include "stdafx.h"
  2. #include "KongJianHouFangJiaoHui.h"
  3. #include "KongJianHouFangJiaoHuiDlg.h"
  4. #include "afxdialogex.h"
  5. #include "CeHuiLei.h"
  6. #include "Matrix.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. CMatrix xyXYZ;
  11. int ButtonClickeOk = 1;
  12. void CKongJianHouFangJiaoHuiDlg::OnBnClickedOk()//导入测量数据按钮代码
  13. {
  14. CFileDialog dlgOpenFile(TRUE, _T("txt"), NULL,
  15. OFN_FILEMUSTEXIST, _T("(文本文件)|*.txt|(所有文件)|*.*)||"));
  16. if (dlgOpenFile.DoModal() == IDCANCEL)
  17. return; //如果选择取消按钮,则退出
  18. CString strPathName = dlgOpenFile.GetPathName(); //获取选择的文件的完整路径
  19. CString strFileTitle = dlgOpenFile.GetFileTitle(); //获取文件名
  20. CString strExtName = dlgOpenFile.GetFileExt(); //获取文件扩展名
  21. CStdioFile sf; //创建文件对象
  22. //以读的形式打开文件,如果打开失败
  23. if (!sf.Open(strPathName, CFile::modeRead))
  24. {
  25. MessageBox(_T( "读取文件出错!"));
  26. return;
  27. }
  28. CString strLine; //存放每一行文本
  29. CStringArray strSplit;
  30. CeHuiLei SplitString;
  31. int hang = 0;
  32. while (sf.ReadString(strLine))
  33. {
  34. if (hang <= 1)
  35. {
  36. hang++;
  37. }
  38. else {
  39. int n = SplitString.SplitStringArray(strLine, ',', strSplit);
  40. int index1 = m_list1.GetItemCount();
  41. CString sno1 = _T( "");
  42. sno1.Format(_T( "%d"), index1 + 1);
  43. int nR = m_list1.InsertItem(index1, sno1);
  44. for ( int i = 0; i < 5; i++)
  45. {
  46. m_list1.SetItemText(index1, i + 1, strSplit[i]);
  47. }
  48. }
  49. }
  50. sf.Close();
  51. int Line = m_list1.GetItemCount();
  52. CMatrix listXYZ(Line, 5);
  53. for ( int i = 0; i < Line; i++)
  54. for ( int j = 0; j < 5;j++)
  55. listXYZ(i,j)=_wtof(m_list1.GetItemText(i, j + 1));
  56. xyXYZ = listXYZ;
  57. ButtonClickeOk = 0; //表示导入数据按钮已被点击并且已导入数据
  58. }
(1)主要的计算代码


     
     
  1. CMatrix CKongJianHouFangJiaoHuiDlg::GetX(CMatrix A, CMatrix L)//计算外方位元素改正数矩阵
  2. {
  3. CMatrix X,_A,_AA,N_AA;
  4. _A = ~A; //A的转置
  5. _AA = _A*A;
  6. N_AA = _AA.Inv(); //_AA的逆矩阵
  7. X = N_AA*_A*L;
  8. return X;
  9. }
  10. CMatrix CKongJianHouFangJiaoHuiDlg::GetA(CMatrix xyXYZ, double f, CMatrix XX)//计算系数矩阵A
  11. {
  12. int iRow = xyXYZ.Row();
  13. CMatrix A(2 * iRow, 6);
  14. double XS = XX( 0, 0);
  15. double YS = XX( 0, 1);
  16. double ZS = XX( 0, 2);
  17. double w = XX( 0, 4);
  18. double k = XX( 0, 5);
  19. double q = XX( 0, 3);;
  20. double a1 = cos(q)* cos(k) - sin(q)* sin(w)* sin(k);
  21. double a2 = - cos(q)* sin(k) - sin(q)* sin(w)* cos(k);
  22. double a3 = - sin(q)* cos(w);
  23. double b1 = cos(w)* sin(k);
  24. double b2 = cos(w)* cos(k);
  25. double b3 = - sin(w);
  26. double c1 = sin(q)* cos(k) + cos(q)* sin(w)* sin(k);
  27. double c2 = - sin(q)* sin(k) + cos(q)* sin(w)* cos(k);
  28. double c3 = cos(q)* cos(w);
  29. f = f / 1000.0;
  30. for ( int i = 0; i < iRow; i++)
  31. {
  32. double ZA = xyXYZ(i, 4);
  33. double XA = xyXYZ(i, 2);
  34. double YA = xyXYZ(i, 3);
  35. double x = (xyXYZ(i, 0)-_wtof(strx0))/ 1000.0;
  36. double y = (xyXYZ(i, 1)-_wtof(stry0))/ 1000.0;
  37. double _Z = a3*(XA - XS) + b3*(YA - YS) + c3*(ZA - ZS);
  38. A( 2*i, 0) = (a1*f + a3*x) / _Z;
  39. A( 2*i, 1) = (b1*f + b3*x) / _Z;
  40. A( 2*i, 2) = (c1*f + c3*x) / _Z;
  41. A( 2*i, 3) = y* sin(w) - (x*(x* cos(k) - y* sin(k)) / f + f* cos(k))* cos(w);
  42. A( 2*i, 4) = -f* sin(k) - x*(x* sin(k) + y* cos(k)) / f;
  43. A( 2*i, 5) = y;
  44. A( 2*i+ 1, 0) = (a2*f + a3*y) / _Z;
  45. A( 2 * i + 1, 1) = (b2*f + b3*y) / _Z;
  46. A( 2 * i + 1, 2) = (c2*f + c3*y) / _Z;
  47. A( 2 * i + 1, 3) = -x* sin(w) - (y*(x* cos(k) - y* sin(k)) / f - f* sin(k))* cos(w);
  48. A( 2 * i + 1, 4) = -f* cos(k) - y/ f*(x* sin(k) + y* cos(k));
  49. A( 2 * i + 1, 5) = -x;
  50. }
  51. return A;
  52. }
  53. CMatrix CKongJianHouFangJiaoHuiDlg::GetL(CMatrix xyXYZ, double f, CMatrix XX)//计算L矩阵
  54. {
  55. int iRow = xyXYZ.Row();
  56. CMatrix L(2 * iRow, 1);
  57. double XS = XX( 0, 0);
  58. double YS = XX( 0, 1);
  59. double ZS = XX( 0, 2);
  60. double w = XX( 0, 4);
  61. double k = XX( 0, 5);
  62. double q = XX( 0, 3);;
  63. double a1 = cos(q)* cos(k) - sin(q)* sin(w)* sin(k);
  64. double a2 = - cos(q)* sin(k) - sin(q)* sin(w)* cos(k);
  65. double a3 = - sin(q)* cos(w);
  66. double b1 = cos(w)* sin(k);
  67. double b2 = cos(w)* cos(k);
  68. double b3 = - sin(w);
  69. double c1 = sin(q)* cos(k) + cos(q)* sin(w)* sin(k);
  70. double c2 = - sin(q)* sin(k) + cos(q)* sin(w)* cos(k);
  71. double c3 = cos(q)* cos(w);
  72. f = f / 1000.0;
  73. for ( int i = 0; i < iRow; i++)
  74. {
  75. double ZA = xyXYZ(i, 4);
  76. double XA = xyXYZ(i, 2);
  77. double YA = xyXYZ(i, 3);
  78. double x = (xyXYZ(i, 0) - _wtof(strx0)) / 1000.0;
  79. double y = (xyXYZ(i, 1) - _wtof(stry0)) / 1000.0;
  80. double x0 = -f*(a1*(XA - XS) + b1*(YA - YS) + c1*(ZA - ZS)) / (a3*(XA - XS) + b3*(YA - YS) + c3*(ZA - ZS));
  81. double y0 = -f*(a2*(XA - XS) + b2*(YA - YS) + c2*(ZA - ZS)) / (a3*(XA - XS) + b3*(YA - YS) + c3*(ZA - ZS));
  82. L( 2*i, 0) =x - x0;
  83. L( 2*i+ 1, 0) =y - y0;
  84. }
  85. return L;
  86. }
  87. void CKongJianHouFangJiaoHuiDlg::OnBnClickedButton1()//计算按钮
  88. {
  89. UpdateData( true);
  90. if (ButtonClickeOk == 1)
  91. {
  92. MessageBox(_T( "请先点击“导入已知坐标”按钮导入数据!"), _T( "提示"));
  93. return;
  94. }
  95. else if (strf == _T( "") || strm == _T( "") || strx0 == _T( "") || stry0 == _T( ""))
  96. {
  97. MessageBox(_T( "请输入数据!"), _T( "提示"));
  98. return;
  99. }
  100. else
  101. {
  102. int c = strm.GetLength();
  103. double m = _wtof(strm.Right(c - 2));
  104. int iRow = xyXYZ.Row();
  105. double sumX = 0;
  106. double sumY = 0;
  107. double sumZ = 0;
  108. for ( int i = 0; i < iRow; i++)
  109. {
  110. sumX += xyXYZ(i, 2);
  111. sumY += xyXYZ(i, 3);
  112. sumZ += xyXYZ(i, 4);
  113. }
  114. double XS = sumX / iRow;
  115. double YS = sumY / iRow;
  116. double ZS = sumZ / iRow + m*_wtof(strf) / 1000;
  117. double w = 0;
  118. double k = 0;
  119. double q = 0;
  120. CMatrix XX(1, 6);
  121. XX( 0, 0) = XS;
  122. XX( 0, 1) = YS;
  123. XX( 0, 2) = ZS;
  124. XX( 0, 3) = q;
  125. XX( 0, 4) = w;
  126. XX( 0, 5) = k;
  127. CMatrix dX(6, 1);
  128. //迭代计算:
  129. do {
  130. CMatrix A = GetA(xyXYZ, _wtof(strf), XX);
  131. CMatrix L = GetL(xyXYZ, _wtof(strf), XX);
  132. dX = GetX(A, L);
  133. for ( int n = 0; n < 6; n++)
  134. XX( 0, n) = XX( 0, n) + dX(n, 0);
  135. } while (dX( 3, 0) >= 2.908882087e-5 || dX( 4, 0) >= 2.908882087e-5 || dX( 5, 0) >= 2.908882087e-5); //各角元素迭代计算至其改正值小于6秒,6s=2.908882087e-5 弧度。
  136. CString strarray[ 6]; //输出外方位元素计算结果
  137. strarray[ 0] = _T( "XS");
  138. strarray[ 1] = _T( "YS");
  139. strarray[ 2] = _T( "ZS");
  140. strarray[ 3] = _T( "q");
  141. strarray[ 4] = _T( "w");
  142. strarray[ 5] = _T( "k");
  143. CString strOutPut = _T( "外方位元素为:\r\n");
  144. CString str = _T( "");
  145. for ( int i = 0; i < 6; i++)
  146. {
  147. str.Format(_T( "%s=%.4f "), strarray[i], XX( 0, i));
  148. strOutPut += str;
  149. if (i == 2)
  150. strOutPut += _T( "\r\n");
  151. }
  152. MessageBox(strOutPut, _T( "提示"), 1);
  153. }
  154. }


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值