#include "Common.h"
///
/// 将ColorType转成opencv支持的CV_8UC1这样的代号
///
///
///
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);
}
///
/// 根据ColorType求bytesPerPixel
///
///
///
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);
}
///
/// 判断是否需要裁切
///
///
///
///
///
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;
}
///
/// 析构函数
///
ErrorMsg::~ErrorMsg()
{
if (_errorMsg)
{
delete _errorMsg;
_errorMsg = nullptr;
}
}
///
/// 设置错误信息
///
///
///
void ErrorMsg::SetErrorMsg(ErrorCode code, std::vector 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;
}
///
/// 获取错误信息
///
///
///
///
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);
}
}
///
/// 初始化静态成员变量_errorMsg
///
char* ErrorMsg::_errorMsg = nullptr;
///
/// 初始化静态成员变量 _errorCode
///
ErrorCode ErrorMsg::_errorCode = None;
///
/// 计算两点之间的距离
///
///
///
///
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));
}
///
/// 将某个float值和index组成的pair,按float值的大小降序排列
///
///
///
///
bool MathTools::SortFloatIndexPairDescend(const std::pair& pair1, const std::pair pair2)
{
return pair1.first > pair2.first;
}
///
/// 将某个float值和index组成的pair,按float值的大小升序排列
///
///
///
///
bool MathTools::SortFloatIndexPairAscend(const std::pair& pair1, const std::pair pair2)
{
return pair1.first < pair2.first;
}