You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
5.7 KiB
233 lines
5.7 KiB
/*
|
|
//实现对部分缺陷 需要进行 数量 和距离上分析的
|
|
*/
|
|
#ifndef AI_Edge_Algin_H_
|
|
#define AI_Edge_Algin_H_
|
|
#include <opencv2/opencv.hpp>
|
|
#include "CheckUtil.hpp"
|
|
#include "OtherDetBaseDefine.h"
|
|
#include "CheckErrorCodeDefine.hpp"
|
|
#include "ImageDetConfig.h"
|
|
#include "CheckConfigDefine.h"
|
|
#include "ImageStorage.h"
|
|
#include "AI_Factory.h"
|
|
#include "DetLog.h"
|
|
using namespace std;
|
|
|
|
using namespace std;
|
|
using namespace cv;
|
|
|
|
class AI_Edge_Algin
|
|
{
|
|
public:
|
|
enum Mark_Result_Status
|
|
{
|
|
Mark_Result_Status_NULL,
|
|
Mark_Result_Status_OK,
|
|
Mark_Result_Status_Error,
|
|
};
|
|
struct MarK_Result
|
|
{
|
|
Mark_Result_Status status;
|
|
cv::Rect SearchROI_DetImg; // 相对于 检测图的 搜索区域;
|
|
cv::Point param_Local_DetImg; // mark的参数位置,相对于检测图片
|
|
cv::Point det_Local_DetImg; // 检测结果 相对于 检测图片
|
|
|
|
cv::Rect SearchROI_SrcImg; // 相对于 检测图的 搜索区域;
|
|
cv::Point param_Local_SrcImg; // mark的参数位置,相对于检测图片
|
|
cv::Point det_Local_SrcImg; // 检测结果 相对于 检测图片
|
|
MarK_Result()
|
|
{
|
|
status = Mark_Result_Status_NULL;
|
|
SearchROI_DetImg = cv::Rect(0, 0, 0, 0);
|
|
param_Local_DetImg = cv::Point(0, 0);
|
|
det_Local_DetImg = cv::Point(0, 0);
|
|
|
|
SearchROI_SrcImg = cv::Rect(0, 0, 0, 0);
|
|
param_Local_SrcImg = cv::Point(0, 0);
|
|
det_Local_SrcImg = cv::Point(0, 0);
|
|
}
|
|
};
|
|
// 边缘搜索定位结果
|
|
struct Edge_AI_Result
|
|
{
|
|
int nresult;
|
|
cv::RotatedRect bigroi;
|
|
cv::RotatedRect smallroi;
|
|
bool buseOfft;
|
|
int offt_x;
|
|
int offt_y;
|
|
cv::Mat H;
|
|
|
|
std::vector<MarK_Result> markresulList;
|
|
|
|
Edge_AI_Result()
|
|
{
|
|
Init();
|
|
}
|
|
void Init()
|
|
{
|
|
buseOfft = false;
|
|
nresult = 0;
|
|
offt_x = 0;
|
|
offt_y = 0;
|
|
bigroi = cv::RotatedRect(cv::Point2f(0, 0), cv::Size2f(0, 0), 0);
|
|
smallroi = cv::RotatedRect(cv::Point2f(0, 0), cv::Size2f(0, 0), 0);
|
|
markresulList.clear();
|
|
if (!H.empty())
|
|
{
|
|
H.release();
|
|
/* code */
|
|
}
|
|
}
|
|
};
|
|
|
|
enum SaveProcessType
|
|
{
|
|
Save_Close, // 不保存
|
|
Save_Filter, // 过滤的
|
|
Save_ALL, // 全部
|
|
};
|
|
|
|
/// @brief 检测过程的参数
|
|
struct DetConfig
|
|
{
|
|
int ncamId; // 相机ID
|
|
BaseCheckFunction *pBaseCheckFunction;
|
|
std::string strChannel; // 通道
|
|
int nthresholdvalue; // 背景阈值
|
|
int nAIErodesize; // 边缘腐蚀强度
|
|
bool bSaveResultImg; // 保存结果图片
|
|
SaveProcessType saveProcessImg; // 保存过程图片
|
|
bool bUseDrawRoi_Check; // 是否用绘制的ROI进行校验
|
|
cv::Rect drawRoi; // 绘制的 ROi;
|
|
cv::Mat drawMask; // 绘制的maksk
|
|
DetConfig()
|
|
{
|
|
Init();
|
|
}
|
|
void Init()
|
|
{
|
|
pBaseCheckFunction = NULL;
|
|
ncamId = 0;
|
|
nthresholdvalue = 1;
|
|
nAIErodesize = 7;
|
|
bSaveResultImg = false;
|
|
saveProcessImg = Save_Close;
|
|
bUseDrawRoi_Check = false;
|
|
drawRoi = cv::Rect(0, 0, 0, 0);
|
|
if (!drawMask.empty())
|
|
{
|
|
drawMask.release();
|
|
/* code */
|
|
}
|
|
}
|
|
void Print()
|
|
{
|
|
printf("nthresholdvalue:%d;nAIErodesize %d;bSaveResultImg %s SaveProcessImg %d\n",
|
|
nthresholdvalue, nAIErodesize, BOOL_TO_STR(bSaveResultImg), saveProcessImg);
|
|
|
|
printf("bUseDrawRoi_Check %s roi %s \n",
|
|
BOOL_TO_STR(bUseDrawRoi_Check), CheckUtil::GetRectString(drawRoi).c_str());
|
|
}
|
|
bool IsSaveProcessImg()
|
|
{
|
|
if (saveProcessImg != Save_Close)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
};
|
|
|
|
public:
|
|
AI_Edge_Algin(std::shared_ptr<DetLog>& log_ref);
|
|
~AI_Edge_Algin();
|
|
// 初始化检测模型
|
|
int Init(OtherDet_Config *pOtherDet_Config);
|
|
int InitModel_ALL();
|
|
int Detect(const cv::Mat &img, DetConfig *pDetConfig, std::shared_ptr<Edge_AI_Result> &pCheckResult_Aling);
|
|
int SaveSmallImg(const cv::Mat &img, const cv::Mat &mask, cv::Rect roi);
|
|
|
|
private:
|
|
int InitModel_Big();
|
|
int Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDetConfig, std::string strChannel, cv::RotatedRect &Roi);
|
|
|
|
int creatsavedir();
|
|
|
|
private:
|
|
bool m_bInitSucc; // 是否初始化成功
|
|
// 检测结果
|
|
std::shared_ptr<Edge_AI_Result> m_pCheckResult_Aling;
|
|
|
|
OtherDet_Config *m_pOtherDet_Config;
|
|
DetConfig *m_pDetConfig;
|
|
|
|
std::shared_ptr<DetLog>& m_pdetlog;
|
|
|
|
bool m_bInitialized;
|
|
bool m_bModelSucc;
|
|
bool m_bModel_Mark_Succ;
|
|
|
|
bool m_bshowimg;
|
|
cv::Mat showimg;
|
|
std::string m_strRootPath_Big;
|
|
std::string m_strSavePath_Big;
|
|
std::string m_strRootPath_Mark;
|
|
std::string m_strSavePath_Mark;
|
|
std::string m_strLastDate;
|
|
|
|
ImageStorage *m_pImageStorage;
|
|
|
|
std::shared_ptr<AIFactory> AI_Factory;
|
|
|
|
private:
|
|
/* data */
|
|
};
|
|
|
|
// 图片特征定位
|
|
class Image_Feature_Algin
|
|
{
|
|
public:
|
|
struct DetConfig
|
|
{
|
|
bool bSaveImg;
|
|
bool bSave_Process; // 存储过程图片
|
|
|
|
float fscore;
|
|
cv::Mat TemplateImg; // 模版图片
|
|
cv::Mat DetImg; // 检测图片
|
|
|
|
cv::Rect Search_Roi; // 搜索 范围
|
|
cv::Rect feature_Roi; // 特征区域
|
|
cv::Rect param_CropRoi; // 裁剪区域,在参数图片上
|
|
cv::Rect DetImg_CropROi; // 裁剪区域,在检测图片上
|
|
DetConfig()
|
|
{
|
|
bSaveImg = false;
|
|
bSave_Process = false;
|
|
fscore = 0.9;
|
|
Search_Roi = cv::Rect(0, 0, 0, 0);
|
|
feature_Roi = cv::Rect(0, 0, 0, 0);
|
|
param_CropRoi = cv::Rect(0, 0, 0, 0);
|
|
DetImg_CropROi = cv::Rect(0, 0, 0, 0);
|
|
}
|
|
};
|
|
|
|
public:
|
|
Image_Feature_Algin(/* args */);
|
|
~Image_Feature_Algin();
|
|
int Detect(DetConfig *pDetConfig, Align_Result *pResult, std::vector<std::string> &LogList);
|
|
|
|
private:
|
|
cv::Point findBestTemplateMatch(const cv::Mat &detectionImage, const cv::Mat &templateImage, double &bestScore, int method = cv::TM_CCOEFF_NORMED);
|
|
|
|
private:
|
|
PRINT_LOG_ m_PrintLog;
|
|
|
|
private:
|
|
/* data */
|
|
};
|
|
|
|
#endif |