123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #include "Common.h"
- /// <summary>
- /// 将ColorType转成opencv支持的CV_8UC1这样的代号
- /// </summary>
- /// <param name="colorType"></param>
- /// <returns></returns>
- int ImageHelper::GetDepthFlag(ColorType colorType)
- {
- switch (colorType)
- {
- case Gray8:
- return CV_8UC1;
- case Gray16:
- return CV_16UC1;
- case Rgb:
- case Bgr:
- return CV_8UC3;
- case Bgra:
- case Rgba:
- return CV_8UC4;
- case GrayF32:
- return CV_32FC1;
- }
- char strMsgBuff[32];
- std::snprintf(strMsgBuff, 32, "unexpected colorType ( %i ).", colorType);
- throw std::invalid_argument(strMsgBuff);
- }
- /// <summary>
- /// 根据ColorType求bytesPerPixel
- /// </summary>
- /// <param name="colorType"></param>
- /// <returns></returns>
- int ImageHelper::GetBytesPerPixel(ColorType colorType)
- {
- switch (colorType)
- {
- case Gray8:
- return 1;
- case Gray16:
- return 2;
- case Rgb:
- case Bgr:
- return 3;
- case Bgra:
- case Rgba:
- case GrayF32:
- return 4;
- }
- char strMsgBuff[32];
- std::snprintf(strMsgBuff, 32, "unexpected colorType ( %i ).", colorType);
- throw std::invalid_argument(strMsgBuff);
- }
- /// <summary>
- /// 判断是否需要裁切
- /// </summary>
- /// <param name="cropRect"></param>
- /// <param name="width"></param>
- /// <param name="height"></param>
- /// <returns></returns>
- bool ImageHelper::NeedCrop(cv::Rect cropRect, int width, int height)
- {
- if (cropRect.x != 0)
- {
- return true;
- }
- if (cropRect.y != 0)
- {
- return true;
- }
- if (cropRect.width != width)
- {
- return true;
- }
- if (cropRect.height != height)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 析构函数
- /// </summary>
- ErrorMsg::~ErrorMsg()
- {
- if (_errorMsg)
- {
- delete _errorMsg;
- _errorMsg = nullptr;
- }
- }
- /// <summary>
- /// 设置错误信息
- /// </summary>
- /// <param name="code"></param>
- /// <param name="errorMsgs"></param>
- void ErrorMsg::SetErrorMsg(ErrorCode code, std::vector<const char*> errorMsgs)
- {
- if (_errorMsg)
- {
- delete _errorMsg;
- _errorMsg = NULL;
- }
- int count = errorMsgs.size();
- std::stringstream ss;
- for (int ni = 0; ni < count; ni++)
- {
- ss << errorMsgs[ni];
- }
- std::string msgCombined = ss.str();
- int len = msgCombined.length();
- _errorMsg = new char[msgCombined.length() + 1];
- msgCombined.copy(_errorMsg, len, 0);
- *(_errorMsg + len) = '\0';
- _errorCode = code;
- }
- /// <summary>
- /// 获取错误信息
- /// </summary>
- /// <param name="errorCode"></param>
- /// <param name="errorMsg"></param>
- /// <param name="errorMaxLen"></param>
- void ErrorMsg::GetErrorMsg(ErrorCode& errorCode, char* errorMsg, const int errorMaxLen)
- {
- errorCode = _errorCode;
- if (!_errorMsg)
- {
- return ;
- }
- int errorLen = strlen(_errorMsg);
- if (errorMaxLen > errorLen)
- {
- strcpy_s(errorMsg, errorLen + 1, _errorMsg);
- }
- else
- {
- strncpy_s(errorMsg, errorMaxLen, _errorMsg, errorMaxLen - 1);
- }
- }
- /// <summary>
- /// 初始化静态成员变量_errorMsg
- /// </summary>
- char* ErrorMsg::_errorMsg = nullptr;
- /// <summary>
- /// 初始化静态成员变量 _errorCode
- /// </summary>
- ErrorCode ErrorMsg::_errorCode = None;
- /// <summary>
- /// 计算两点之间的距离
- /// </summary>
- /// <param name="p1"></param>
- /// <param name="p2"></param>
- /// <returns></returns>
- float MathTools::Distance(cv::Point3f p1, cv::Point3f p2)
- {
- auto x = p1.x - p2.x;
- auto y = p1.y - p2.y;
- auto z = p1.z - p2.z;
- return std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2));
- }
- /// <summary>
- /// 将某个float值和index组成的pair,按float值的大小降序排列
- /// </summary>
- /// <param name="pair1"></param>
- /// <param name="pair2"></param>
- /// <returns></returns>
- bool MathTools::SortFloatIndexPairDescend(const std::pair<float, int>& pair1, const std::pair<float, int> pair2)
- {
- return pair1.first > pair2.first;
- }
- /// <summary>
- /// 将某个float值和index组成的pair,按float值的大小升序排列
- /// </summary>
- /// <param name="pair1"></param>
- /// <param name="pair2"></param>
- /// <returns></returns>
- bool MathTools::SortFloatIndexPairAscend(const std::pair<float, int>& pair1, const std::pair<float, int> pair2)
- {
- return pair1.first < pair2.first;
- }
|