OTSClassifyOnCurveCompEng.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "OTSSTDLib.h"
  4. #include "OTSClassifyOnCurveCompEng.h"
  5. #include "OTSHelper.h"
  6. using namespace std;
  7. using namespace OTSTools;
  8. namespace OTSClassifyEngine
  9. {
  10. CClassifyOnCurveCompEng::CClassifyOnCurveCompEng(int iStartChannel, CSTDLibPtr stdLib ) // constructor
  11. {
  12. m_iStartChannel = iStartChannel;
  13. m_stdLib = stdLib;
  14. }
  15. CClassifyOnCurveCompEng::~CClassifyOnCurveCompEng() // detractor
  16. {
  17. }
  18. int CClassifyOnCurveCompEng::GetMatchingSTDId( CPosXrayPtr posxray, double& dValue)
  19. {
  20. if (m_stdLib->GetSTDItemCount() < 1)
  21. {
  22. return -1;
  23. }
  24. int id = 0; // 存储最相似的那个mineralid
  25. double dcos = 0;
  26. dValue = -100000; // 获取最大的相似值
  27. for (unsigned int i = 0; i < m_stdLib->GetSTDItemCount(); i++)
  28. {
  29. dcos = GetCosValue( posxray, m_stdLib->GetSTDItem(i), m_stdLib->GetSTDItem (i)->GetChannelsNum());
  30. if (dcos > dValue)
  31. {
  32. dValue = dcos;
  33. id = m_stdLib->GetSTDItem (i)->GetID();
  34. }
  35. }
  36. return id;
  37. }
  38. CStandardItemPtr CClassifyOnCurveCompEng::GetMatchingSTD( CPosXrayPtr posxray, double& dValue)
  39. {
  40. CStandardItemPtr itm; // 存储最相似的那个mineral
  41. int stdItemCount = m_stdLib->GetSTDItemCount();
  42. if (stdItemCount < 1)
  43. {
  44. return itm;
  45. }
  46. double dcos = 0;
  47. double dSimilarity = -1000;
  48. // 获取最大的相似值
  49. int idx = -1;
  50. for (unsigned int i = 0; i < stdItemCount; i++)
  51. {
  52. dcos = GetCosValue(posxray, m_stdLib->GetSTDItem(i), m_stdLib->GetSTDItem(i)->GetChannelsNum());
  53. if (dcos > dSimilarity)
  54. {
  55. dSimilarity = dcos;
  56. idx = i;
  57. }
  58. }
  59. dValue = dSimilarity;
  60. itm = m_stdLib->GetSTDItem(idx);
  61. return itm;
  62. }
  63. // 20190903:增加参数iStartChannel哪个位置开始比较, vecIgnoreChannel里面的通道不处理,
  64. double CClassifyOnCurveCompEng::GetCosValue( CPosXrayPtr posXray,CPosXrayPtr posXray1, int iDataLen1)
  65. {
  66. if (posXray->GetChannelsNum() != iDataLen1)
  67. {
  68. return 0;
  69. }
  70. DWORD* pXrayData = posXray->GetXrayData();
  71. DWORD* pXrayData1 = posXray1->GetXrayData();
  72. // 公式: (x1y1+x2y2+x3y3+...x2000y2000) / (sqrt(x1^2 + x2^2 + ...x2000^2) * sqrt(y1^2 + y2^2 + ...y2000^2))
  73. double dotProduct = 0;
  74. double d1 = 0;
  75. double d2 = 0;
  76. for (int i = m_iStartChannel; i < iDataLen1; i++)
  77. {
  78. double r1 = pXrayData[i];
  79. double r2= pXrayData1[i];
  80. r1 *= r2;
  81. dotProduct = dotProduct + r1;
  82. }
  83. d1 = posXray->GetXrayDataVectorNorm();
  84. d2 = posXray1->GetXrayDataVectorNorm();
  85. return (0 == d1 || 0 == d2) ? 0 : dotProduct / (d1 * d2);
  86. }
  87. // 20190903:增加参数iStartChannel哪个位置开始比较, vecIgnoreChannel里面的通道不处理
  88. double CClassifyOnCurveCompEng::GetStdEvp( CPosXrayPtr posxray, DWORD* pXrayData1, int iDataLen1)
  89. {
  90. if (posxray->GetChannelsNum() != iDataLen1)
  91. {
  92. return 0;
  93. }
  94. DWORD* pXrayData = posxray->GetXrayData();
  95. //bool bignore = false;
  96. DWORD iSum1 = 0;
  97. DWORD iSum2 = 0;
  98. double dAva1 = 0;
  99. double dAva2 = 0;
  100. double dSquare1 = 0;
  101. double dSquare2 = 0;
  102. int num1= iDataLen1 - m_iStartChannel;
  103. // 求出能谱的平均值
  104. for (int i = m_iStartChannel; i < iDataLen1; i++)
  105. {
  106. // 能谱差之和
  107. iSum1 = iSum1 + pXrayData1[i];
  108. iSum2 = iSum2 + pXrayData[i];
  109. }
  110. dAva1 = iSum1 /num1;
  111. dAva2 = iSum2 / num1;
  112. for (int i = m_iStartChannel; i < iDataLen1; i++)
  113. {
  114. dSquare1 = dSquare1 + (pXrayData1[i] - dAva1) * (pXrayData1[i] - dAva1);
  115. dSquare2 = dSquare2 + (pXrayData[i] - dAva1) * (pXrayData[i] - dAva1);
  116. }
  117. dSquare1 = dSquare1 / num1;
  118. dSquare2 = dSquare2 / num1;
  119. dSquare1 = sqrt(dSquare1);
  120. dSquare2 = sqrt(dSquare2);
  121. dSquare1 = dSquare1 * dAva2 / dAva1; // 归一化
  122. return (dSquare1 < dSquare2) ? (dSquare1 / dSquare2) : (dSquare2 / dSquare1);
  123. }
  124. void CClassifyOnCurveCompEng::GetElementChannel(const vector<CString>& vecstrElementNames, vector<int>& veciChannel)
  125. {
  126. veciChannel.clear();
  127. for (unsigned int i = 0; i < (int)vecstrElementNames.size(); i++)
  128. {
  129. GetElementChannel(vecstrElementNames[i], veciChannel);
  130. }
  131. }
  132. void CClassifyOnCurveCompEng::GetElementChannel(CString strElementName, vector<int>& veciChannel)
  133. {
  134. CElement* pElement = NULL;
  135. pElement = new CElement(strElementName);
  136. if (pElement)
  137. {
  138. AddChannel(pElement->GetEnergyValueK(), veciChannel);
  139. AddChannel(pElement->GetEnergyValueL(), veciChannel);
  140. AddChannel(pElement->GetEnergyValueM(), veciChannel);
  141. delete pElement;
  142. pElement = NULL;
  143. }
  144. }
  145. void CClassifyOnCurveCompEng::AddChannel(double dEnergy, vector<int>& veciChannel)
  146. {
  147. int i = 0;
  148. int iChannel = 0;
  149. if (dEnergy >= 0 && dEnergy < 20)
  150. {
  151. dEnergy = dEnergy * 100;
  152. iChannel = (int)dEnergy;
  153. for (i = 0; i < (int)veciChannel.size(); i++)
  154. {
  155. if (veciChannel[i] == iChannel)
  156. {
  157. break;
  158. }
  159. }
  160. if (i >= (int)veciChannel.size())
  161. {
  162. veciChannel.push_back(iChannel);
  163. }
  164. }
  165. }
  166. }