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.
BOE_FOG_DETECT/AlgorithmModule/src/AI_ZF_Det.cpp

158 lines
4.0 KiB

#include "AI_ZF_Det.h"
#include "CheckErrorCodeDefine.hpp"
static bool compareContourAreas123(const vector<Point> &contour1, const vector<Point> &contour2)
{
double i = contourArea(contour1);
double j = contourArea(contour2);
return (i > j);
}
AI_ZF_Det::AI_ZF_Det()
{
m_bModelSucc = false;
CheckUtil::CreateDir("/home/aidlux/BOE/FOG/zf/");
m_pImageStorage = ImageStorage::getInstance();
m_Show_Area = 0;
m_Show_Len = 0;
m_Len_P1 = cv::Point(0, 0);
m_Len_P2 = cv::Point(0, 0);
AI_Factory = AIFactory::GetInstance();
}
AI_ZF_Det::~AI_ZF_Det()
{
}
int AI_ZF_Det::Detect(const cv::Mat &img, DetConfigResult *pDetConfig)
{
std::shared_ptr<AIModel_Base> pAI_Model = AI_Factory->AI_defect_zf;
m_pdetlog = pDetConfig->pdetlog;
cv::Size sz;
sz.width = pAI_Model->input_0.width;
sz.height = pAI_Model->input_0.height;
cv::Mat inImg;
cv::Mat AIoutimg;
cv::resize(img, inImg, sz, 0, 0, cv::INTER_AREA);
pAI_Model->AIDet(inImg, AIoutimg);
if (pDetConfig->bDebugsaveimg)
{
if (!inImg.empty())
{
cv::imwrite("ZF_img_In.png", inImg);
}
if (!AIoutimg.empty())
{
cv::imwrite("ZF_img_out.png", AIoutimg);
}
}
{
float fx = img.cols * 1.0f / AIoutimg.cols;
float fy = img.rows * 1.0f / AIoutimg.rows;
// 轮廓检测
std::vector<std::vector<cv::Point>> contours;
findContours(AIoutimg, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// 根据面积大小对轮廓进行排序
sort(contours.begin(), contours.end(), compareContourAreas123);
int kd = 0;
for (size_t i = 0; i < contours.size(); ++i)
{
// 获取当前轮廓的边界矩形
cv::Rect boundingRect = cv::boundingRect(contours[i]);
cv::Rect roi;
roi.x = boundingRect.x * fx;
roi.y = boundingRect.y * fy;
roi.width = boundingRect.width * fx;
roi.height = boundingRect.height * fy;
pDetConfig->pZF_Result->pZF_roiList.push_back(roi);
kd++;
if (kd >= 2)
{
break;
}
}
}
return 0;
}
int AI_ZF_Det::Det_img(const cv::Mat &img, DetConfigResult *pDetConfig)
{
return 0;
}
int AI_ZF_Det::Analysisy(const cv::Mat &maskImg, DetConfigResult *pDetConfig)
{
return 0;
}
int AI_ZF_Det::SaveProcessImg(const cv::Mat &inImg, const cv::Mat &outImg, const cv::Mat &oldmask, DetConfigResult *pDetConfig)
{
// if (inImg.empty() || outImg.empty())
// {
// return 1;
// }
// static int saveimgIdx_pol = 0;
// static int saveimgIdx_ad = 0;
// // 循环存储
// std::string str_Root = "/home/aidlux/BOE/Second/";
// if (pDetConfig->qx_type == CONFIG_QX_NAME_POL_Cell)
// {
// saveimgIdx_pol++;
// if (saveimgIdx_pol > 1000)
// {
// saveimgIdx_pol = 0;
// }
// str_Root += "POL/POl_" + pDetConfig->strChannel + "_" + std::to_string(saveimgIdx_pol);
// }
// if (pDetConfig->qx_type == CONFIG_QX_NAME_AD)
// {
// saveimgIdx_ad++;
// if (saveimgIdx_ad > 1000)
// {
// saveimgIdx_ad = 0;
// }
// str_Root += "AD/AD_" + pDetConfig->strChannel + "_" + std::to_string(saveimgIdx_ad);
// }
// std::string strIn = str_Root + +"_in.png";
// int re = 0;
// if (!inImg.empty())
// {
// re = m_pImageStorage->addImage(strIn, inImg);
// }
// if (re == 0)
// {
// std::string strmask = str_Root + "_in_mask.png";
// if (!outImg.empty())
// {
// m_pImageStorage->addImage(strmask, outImg, true); // 强制 存储
// }
// std::string stroldmask = str_Root + "_old_mask.png";
// if (!oldmask.empty())
// {
// m_pImageStorage->addImage(stroldmask, oldmask, true); // 强制 存储
// }
// }
return 0;
}