|
|
/*
|
|
|
* @Descripttion:
|
|
|
* @version:
|
|
|
* @Author: sueRimn
|
|
|
* @Date: 2022-04-28 10:41:42
|
|
|
* @LastEditors: xiewenji 527774126@qq.com
|
|
|
* @LastEditTime: 2025-08-03 21:32:02
|
|
|
*/
|
|
|
/*
|
|
|
* FileName:CoreLogicFactory.hpp
|
|
|
* Version:V1.0
|
|
|
* Description:
|
|
|
* Created On:Mon Sep 10 11:13:13 UTC 2018
|
|
|
* Modified date:
|
|
|
* Author:Sky
|
|
|
*/
|
|
|
#ifndef _CheckUtil_HPP_
|
|
|
#define _CheckUtil_HPP_
|
|
|
#include <iostream>
|
|
|
#include <stdio.h>
|
|
|
#include <string.h>
|
|
|
#include <sys/time.h>
|
|
|
#include <mutex>
|
|
|
#include <vector>
|
|
|
#include <thread>
|
|
|
#include <opencv2/opencv.hpp>
|
|
|
using namespace std;
|
|
|
|
|
|
// 掩膜主体(近似矩形)四周凸起(如把手)的描述
|
|
|
struct Mask_Protrusion_
|
|
|
{
|
|
|
int nSide; // 所在边:0 左 1 右 2 上 3 下
|
|
|
cv::Rect roi; // 凸起外接矩形(输入图坐标系,已裁剪到图像范围内)
|
|
|
float fLen; // 沿该边的长度(像素)
|
|
|
float fDepth; // 伸出主体的深度(像素)
|
|
|
float fArea; // 凸起面积(roi 内掩膜像素数)
|
|
|
float fFillRatio; // roi 内掩膜像素占比(用于判定是否"类似矩形块")
|
|
|
Mask_Protrusion_()
|
|
|
{
|
|
|
nSide = 0;
|
|
|
fLen = 0.0f;
|
|
|
fDepth = 0.0f;
|
|
|
fArea = 0.0f;
|
|
|
fFillRatio = 0.0f;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
class CheckUtil
|
|
|
{
|
|
|
public:
|
|
|
static long getcurTime();
|
|
|
static std::string getCurrentDate();
|
|
|
static std::string getCurTimeHMS();
|
|
|
static std::string Op_float2String(float nvalue);
|
|
|
static int64_t getSnowId();
|
|
|
static bool JudgRect(cv::Rect roi, int img_w, int img_h);
|
|
|
static bool JudgRect_SZ(cv::Rect roi, int w, int h);
|
|
|
static bool compareIgnoreCase(const std::string &str1, const std::string &str2);
|
|
|
static bool RoiInImg(cv::Rect roi, cv::Mat img);
|
|
|
static int printROI(cv::Rect roi, std::string str = "");
|
|
|
static float CalIoU(cv::Rect rect1, cv::Rect rect2);
|
|
|
static float CalIoU_t(cv::Rect rect1, cv::Rect rect2);
|
|
|
static int CheckRect(cv::Rect &roi, int img_w, int img_h);
|
|
|
static int SizeRect(cv::Rect &roi, int img_w, int img_h, int addw, int addh);
|
|
|
static int CalHj(const cv::Mat &img, const cv::Mat &mask,int b_value);
|
|
|
static int CalHj(const cv::Mat &img, const cv::Mat &mask,const cv::Mat &backgroundimg);
|
|
|
static int CalHjWeighted(const cv::Mat &img, const cv::Mat &mask, int b_value, float power = 2.0f);
|
|
|
// 计算平均灰度
|
|
|
static float CalImgBrightness(cv::Mat imgRoi);
|
|
|
// 计算角度
|
|
|
static float Cal2PointAngle(cv::Point p_left, cv::Point p_right);
|
|
|
// 获取 RotatedRect 长轴的方向角(归一化到 [0, 180) 度)
|
|
|
static float getLongAxisAngle(const cv::RotatedRect rect);
|
|
|
// 排序RotatedRect的四个顶点
|
|
|
static std::vector<cv::Point2f> sort_vertices(cv::RotatedRect rrect);
|
|
|
|
|
|
|
|
|
// 找图片的最大外轮廓
|
|
|
static std::vector<cv::Point> getLargestContourROI(const cv::Mat &binaryImg, bool &found);
|
|
|
|
|
|
// 抠出掩膜四周的凸起:主体近似矩形,凸起在该边表现为边界值的一次突变(进入)与一次反向突变(离开)
|
|
|
// binMask:掩膜(非零为前景,兼容 0/1 与 0/255);protrusions:输出的凸起列表
|
|
|
// nJumpThresh:边界突变阈值(输入图像素);nMinRun:凸起最小长度(输入图像素);nMinDepth:凸起最小深度(输入图像素)
|
|
|
// nMaxRun:凸起最大长度(输入图像素),<=0 时取 掩膜短边/3
|
|
|
// 返回:0 成功、1 掩膜为空、2 无有效前景
|
|
|
static int GetMaskProtrusions(const cv::Mat &binMask, std::vector<Mask_Protrusion_> &protrusions,
|
|
|
int nJumpThresh = 40, int nMinRun = 40, int nMinDepth = 40, int nMaxRun = 0);
|
|
|
|
|
|
static std::string GetRectString(cv::Rect rect);
|
|
|
//创建目录
|
|
|
static int CreateDir(const std::string &dir);
|
|
|
|
|
|
// 点的距离是否过小
|
|
|
static bool bcalDis(cv::Point p1, cv::Point p2, int disT);
|
|
|
static double calDis(cv::Point p1, cv::Point p2);
|
|
|
static void PrintRect(cv::Rect roi, std::string str = "");
|
|
|
static int cutSmallImg(cv::Mat img, std::vector<cv::Rect> &samllRoiList, cv::Rect config_roi, int config_SmallImg_Width, int config_SmallImg_Height, int config_MinOverlap_Width, int config_MinOverlap_Height);
|
|
|
static cv::Point2f transformPoint(const cv::Point2f &point, const cv::Mat &transform_matrix);
|
|
|
};
|
|
|
// 无参数重载:直接返回原字符串,避免 -Wformat-security 警告
|
|
|
static inline std::string str_Format(const std::string &format)
|
|
|
{
|
|
|
return format;
|
|
|
}
|
|
|
|
|
|
template <typename... Args>
|
|
|
static std::string str_Format(const std::string &format, Args... args)
|
|
|
{
|
|
|
auto size_buf = std::snprintf(nullptr, 0, format.c_str(), args...) + 1;
|
|
|
std::unique_ptr<char[]> buf(new (std::nothrow) char[size_buf]);
|
|
|
|
|
|
if (!buf)
|
|
|
return std::string("");
|
|
|
|
|
|
std::snprintf(buf.get(), size_buf, format.c_str(), args...);
|
|
|
return std::string(buf.get(), buf.get() + size_buf - 1);
|
|
|
|
|
|
}
|
|
|
#endif //_CORELOGICFACTORY_HPP_
|