|
|
/*
|
|
|
* @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 为输入图像素,内部按降采样比例 nScale(1/2/4) 缩小后使用;nMaxRun 不换算,直接用工作图像素):
|
|
|
// nJumpThresh:边界突变阈值,相邻行/列边界值一次变化超过它才算"进入/离开"台阶;
|
|
|
// 调大:只认陡峭台阶,圆角/噪声/锯齿被过滤,过大时边缘圆滑的真把手会漏检;
|
|
|
// 调小:敏感,噪声/斜边易触发假配对(可能形成跨度很大的假凸起)。
|
|
|
// nMinRun:凸起最小长度("离开"突变搜索从 s+nMinRun 开始;profile 比它短的边直接跳过);
|
|
|
// 调大:短凸起被排除,若大于真把手长度,真"离开"突变被跳过、会配到更远突变 → 出更大的框;
|
|
|
// 调小:小抖动/碎框变多。
|
|
|
// nMinDepth:凸起最小深度,区间内 ≥80% 采样点相对首尾连线基线外偏 ≥ 它(且最大外偏 ≥ 它)才保留(滤斜边/圆角);
|
|
|
// 调大:只保留明显凸出的,浅凸起漏检,可挡"浅而长"的假配对;
|
|
|
// 调小:浅波纹/边缘抖动也过检,假凸起变多。
|
|
|
// nMaxRun:凸起最大长度(框沿边长度的硬上限);<=0 时取 max(100, 掩膜短边/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 int SaveImgAsync(const std::string &path, const cv::Mat &img);
|
|
|
|
|
|
// 点的距离是否过小
|
|
|
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);
|
|
|
|
|
|
// 依目标图像通道数返回可用的绘制颜色:
|
|
|
// 1 通道(灰度) -> 统一白色,保证可见
|
|
|
// 4 通道(BGRA) -> 补 alpha = 255
|
|
|
// 3 通道 -> 原样返回
|
|
|
static inline cv::Scalar GetDrawColor(const cv::Mat &img, const cv::Scalar &color)
|
|
|
{
|
|
|
if (img.channels() == 1)
|
|
|
{
|
|
|
return cv::Scalar(255);
|
|
|
}
|
|
|
if (img.channels() == 4)
|
|
|
{
|
|
|
return cv::Scalar(color[0], color[1], color[2], 255);
|
|
|
}
|
|
|
return color;
|
|
|
}
|
|
|
};
|
|
|
// 无参数重载:直接返回原字符串,避免 -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_
|