1
0

Common.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "Common.h"
  2. /// <summary>
  3. /// 将ColorType转成opencv支持的CV_8UC1这样的代号
  4. /// </summary>
  5. /// <param name="colorType"></param>
  6. /// <returns></returns>
  7. int ImageHelper::GetDepthFlag(ColorType colorType)
  8. {
  9. switch (colorType)
  10. {
  11. case Gray8:
  12. return CV_8UC1;
  13. case Gray16:
  14. return CV_16UC1;
  15. case Rgb:
  16. case Bgr:
  17. return CV_8UC3;
  18. case Bgra:
  19. case Rgba:
  20. return CV_8UC4;
  21. case GrayF32:
  22. return CV_32FC1;
  23. }
  24. char strMsgBuff[32];
  25. std::snprintf(strMsgBuff, 32, "unexpected colorType ( %i ).", colorType);
  26. throw std::invalid_argument(strMsgBuff);
  27. }
  28. /// <summary>
  29. /// 根据ColorType求bytesPerPixel
  30. /// </summary>
  31. /// <param name="colorType"></param>
  32. /// <returns></returns>
  33. int ImageHelper::GetBytesPerPixel(ColorType colorType)
  34. {
  35. switch (colorType)
  36. {
  37. case Gray8:
  38. return 1;
  39. case Gray16:
  40. return 2;
  41. case Rgb:
  42. case Bgr:
  43. return 3;
  44. case Bgra:
  45. case Rgba:
  46. case GrayF32:
  47. return 4;
  48. }
  49. char strMsgBuff[32];
  50. std::snprintf(strMsgBuff, 32, "unexpected colorType ( %i ).", colorType);
  51. throw std::invalid_argument(strMsgBuff);
  52. }
  53. /// <summary>
  54. /// 判断是否需要裁切
  55. /// </summary>
  56. /// <param name="cropRect"></param>
  57. /// <param name="width"></param>
  58. /// <param name="height"></param>
  59. /// <returns></returns>
  60. bool ImageHelper::NeedCrop(cv::Rect cropRect, int width, int height)
  61. {
  62. if (cropRect.x != 0)
  63. {
  64. return true;
  65. }
  66. if (cropRect.y != 0)
  67. {
  68. return true;
  69. }
  70. if (cropRect.width != width)
  71. {
  72. return true;
  73. }
  74. if (cropRect.height != height)
  75. {
  76. return true;
  77. }
  78. return false;
  79. }
  80. /// <summary>
  81. /// 析构函数
  82. /// </summary>
  83. ErrorMsg::~ErrorMsg()
  84. {
  85. if (_errorMsg)
  86. {
  87. delete _errorMsg;
  88. _errorMsg = nullptr;
  89. }
  90. }
  91. /// <summary>
  92. /// 设置错误信息
  93. /// </summary>
  94. /// <param name="code"></param>
  95. /// <param name="errorMsgs"></param>
  96. void ErrorMsg::SetErrorMsg(ErrorCode code, std::vector<const char*> errorMsgs)
  97. {
  98. if (_errorMsg)
  99. {
  100. delete _errorMsg;
  101. _errorMsg = NULL;
  102. }
  103. int count = errorMsgs.size();
  104. std::stringstream ss;
  105. for (int ni = 0; ni < count; ni++)
  106. {
  107. ss << errorMsgs[ni];
  108. }
  109. std::string msgCombined = ss.str();
  110. int len = msgCombined.length();
  111. _errorMsg = new char[msgCombined.length() + 1];
  112. msgCombined.copy(_errorMsg, len, 0);
  113. *(_errorMsg + len) = '\0';
  114. _errorCode = code;
  115. }
  116. /// <summary>
  117. /// 获取错误信息
  118. /// </summary>
  119. /// <param name="errorCode"></param>
  120. /// <param name="errorMsg"></param>
  121. /// <param name="errorMaxLen"></param>
  122. void ErrorMsg::GetErrorMsg(ErrorCode& errorCode, char* errorMsg, const int errorMaxLen)
  123. {
  124. errorCode = _errorCode;
  125. if (!_errorMsg)
  126. {
  127. return ;
  128. }
  129. int errorLen = strlen(_errorMsg);
  130. if (errorMaxLen > errorLen)
  131. {
  132. strcpy_s(errorMsg, errorLen + 1, _errorMsg);
  133. }
  134. else
  135. {
  136. strncpy_s(errorMsg, errorMaxLen, _errorMsg, errorMaxLen - 1);
  137. }
  138. }
  139. /// <summary>
  140. /// 初始化静态成员变量_errorMsg
  141. /// </summary>
  142. char* ErrorMsg::_errorMsg = nullptr;
  143. /// <summary>
  144. /// 初始化静态成员变量 _errorCode
  145. /// </summary>
  146. ErrorCode ErrorMsg::_errorCode = None;
  147. /// <summary>
  148. /// 计算两点之间的距离
  149. /// </summary>
  150. /// <param name="p1"></param>
  151. /// <param name="p2"></param>
  152. /// <returns></returns>
  153. float MathTools::Distance(cv::Point3f p1, cv::Point3f p2)
  154. {
  155. auto x = p1.x - p2.x;
  156. auto y = p1.y - p2.y;
  157. auto z = p1.z - p2.z;
  158. return std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2));
  159. }
  160. /// <summary>
  161. /// 将某个float值和index组成的pair,按float值的大小降序排列
  162. /// </summary>
  163. /// <param name="pair1"></param>
  164. /// <param name="pair2"></param>
  165. /// <returns></returns>
  166. bool MathTools::SortFloatIndexPairDescend(const std::pair<float, int>& pair1, const std::pair<float, int> pair2)
  167. {
  168. return pair1.first > pair2.first;
  169. }
  170. /// <summary>
  171. /// 将某个float值和index组成的pair,按float值的大小升序排列
  172. /// </summary>
  173. /// <param name="pair1"></param>
  174. /// <param name="pair2"></param>
  175. /// <returns></returns>
  176. bool MathTools::SortFloatIndexPairAscend(const std::pair<float, int>& pair1, const std::pair<float, int> pair2)
  177. {
  178. return pair1.first < pair2.first;
  179. }