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.
338 lines
9.3 KiB
338 lines
9.3 KiB
|
|
#include "AI_Edge_QX_Det.h"
|
|
#include "CheckErrorCodeDefine.hpp"
|
|
|
|
AI_Edge_QX_Det::AI_Edge_QX_Det()
|
|
{
|
|
|
|
m_bInitSucc = false;
|
|
m_strSaveImgRootPath = "/home/aidlux/BOE/CELL_ET/Edge_QX/";
|
|
CheckUtil::CreateDir(m_strSaveImgRootPath);
|
|
m_strSaveImgRootPath_OK = m_strSaveImgRootPath + "ok/";
|
|
CheckUtil::CreateDir(m_strSaveImgRootPath_OK);
|
|
m_strSaveImgRootPath_Qx = m_strSaveImgRootPath + "qx/";
|
|
CheckUtil::CreateDir(m_strSaveImgRootPath_Qx);
|
|
m_pImageStorage = ImageStorage::getInstance();
|
|
m_nImge_num = 0;
|
|
m_strCamChannel = "";
|
|
AI_Factory = AIFactory::GetInstance();
|
|
}
|
|
|
|
AI_Edge_QX_Det::~AI_Edge_QX_Det()
|
|
{
|
|
}
|
|
|
|
int AI_Edge_QX_Det::SaveProcessImg(const cv::Mat &inImg, const cv::Mat &outImg, bool isALLzeros, DetConfig *pDetConfig, int idx, int nruntype)
|
|
{
|
|
// printf("nruntype %d %d %d\n", nruntype, pDetConfig->pdege_Det_config->bsave_all, pDetConfig->pdege_Det_config->bsave_qx);
|
|
|
|
// m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_QX_Det ", "nruntype %d %d %d\n", nruntype, pDetConfig->pdege_Det_config->bsave_all, pDetConfig->pdege_Det_config->bsave_qx);
|
|
// 正常模式
|
|
if (nruntype == 0)
|
|
{
|
|
if (pDetConfig->pdege_Det_config->bsave_all || pDetConfig->pdege_Det_config->bsave_qx)
|
|
{
|
|
// printf("==s1 \n");
|
|
}
|
|
else
|
|
{
|
|
// printf("==s2 \n");
|
|
return 0;
|
|
}
|
|
if (pDetConfig->pdege_Det_config->bsave_qx && isALLzeros)
|
|
{
|
|
// printf("==s3 \n");
|
|
return 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 只存 有问题的。
|
|
if (nruntype == 1 && isALLzeros)
|
|
{
|
|
// printf("==s4 \n");
|
|
return 0;
|
|
}
|
|
}
|
|
// printf("==s5\n");
|
|
// 循环存储
|
|
int flage = 1; // 0:正常 1:OK 2:qx
|
|
if (isALLzeros)
|
|
{
|
|
flage = 1;
|
|
}
|
|
else
|
|
{
|
|
flage = 2;
|
|
}
|
|
std::string str_root;
|
|
str_root = GetImgSaveName(flage) + "_" + std::to_string(idx);
|
|
// printf("str_root = %s \n", str_root.c_str());
|
|
|
|
int re = 0;
|
|
if (!inImg.empty())
|
|
{
|
|
std::string strIn = str_root + "_in.png";
|
|
|
|
if (nruntype == 0)
|
|
{
|
|
re = m_pImageStorage->addImage(strIn, inImg);
|
|
}
|
|
else
|
|
{
|
|
cv::imwrite(strIn, inImg);
|
|
}
|
|
}
|
|
|
|
if (re == 0)
|
|
{
|
|
std::string strmask = str_root + "_in_mask.png";
|
|
if (nruntype == 0)
|
|
{
|
|
if (!outImg.empty())
|
|
{
|
|
m_pImageStorage->addImage(strmask, outImg, true); // 强制 存储
|
|
}
|
|
}
|
|
else
|
|
{
|
|
cv::imwrite(strmask, outImg);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
std::string AI_Edge_QX_Det::GetImgSaveName(int flag)
|
|
{
|
|
|
|
std::string str_root;
|
|
if (flag == 0)
|
|
{
|
|
str_root = m_strSaveImgRootPath;
|
|
}
|
|
else if (flag == 1)
|
|
{
|
|
str_root = m_strSaveImgRootPath_OK;
|
|
}
|
|
else
|
|
{
|
|
str_root = m_strSaveImgRootPath_Qx;
|
|
}
|
|
str_root += m_strCamChannel + "_" + std::to_string(m_nImge_num);
|
|
|
|
return str_root;
|
|
}
|
|
int AI_Edge_QX_Det::Detect(const cv::Mat &img, cv::Mat &detmask, DetConfig *pDetConfig, std::shared_ptr<DetLog> pdetlog)
|
|
{
|
|
static int n_count = 0;
|
|
int nRuntype = 0; // 0:检测 1:测试。。。。。
|
|
m_pdetlog = pdetlog;
|
|
if (pDetConfig->ninstruct == 767)
|
|
{
|
|
nRuntype = 1;
|
|
}
|
|
else if (pDetConfig->ninstruct == 768)
|
|
{
|
|
nRuntype = 2;
|
|
}
|
|
|
|
bool bdege = false;
|
|
// 二次求面积长度 功能关闭
|
|
if (!pDetConfig->pdege_Det_config->bOpen)
|
|
{
|
|
m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_QX_Det ", "function colse");
|
|
return 1;
|
|
}
|
|
// if (!m_bInitialized || !m_bModelSucc)
|
|
// {
|
|
// m_pTemCheck->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_QX_Det ", "m_bModelSucc error");
|
|
// return 1;
|
|
// }
|
|
|
|
std::shared_ptr<AIModel_Base> m_pAIDeal = AI_Factory->AI_defect_Edge_QX;
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_QX_Det ", "AI start %s", pDetConfig->strCamChannel.c_str());
|
|
m_strCamChannel = pDetConfig->strCamChannel;
|
|
n_count++;
|
|
if (n_count > 1000)
|
|
{
|
|
m_nImge_num = 0;
|
|
}
|
|
m_nImge_num = n_count;
|
|
// printf("pDetConfig->ninstruct ========== %d channel %s imgname %d \n", pDetConfig->ninstruct, m_strCamChannel.c_str(), m_nImge_num);
|
|
cv::Mat showImg;
|
|
cv::Mat cut_show_R;
|
|
cv::Mat cut_show_G;
|
|
cv::Mat cut_show_B;
|
|
cv::Mat edge_mask;
|
|
std::vector<cv::Mat> cut_show_channels;
|
|
|
|
m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_QX_Det ", "nruntype %d %d\n", pDetConfig->pdege_Det_config->bsave_all, pDetConfig->pdege_Det_config->bsave_qx);
|
|
|
|
if (bdege || nRuntype != 0)
|
|
{
|
|
showImg = img.clone();
|
|
cut_show_R = showImg(pDetConfig->CutRoi).clone();
|
|
cut_show_B = showImg(pDetConfig->CutRoi).clone();
|
|
cut_show_G = showImg(pDetConfig->CutRoi).clone();
|
|
|
|
cut_show_B += detmask * 0.2;
|
|
edge_mask = cv::Mat::zeros(detmask.size(), CV_8UC1);
|
|
}
|
|
for (int i = 0; i < pDetConfig->pEdgeDet_roiList->size(); i++)
|
|
{
|
|
cv::Rect r = pDetConfig->pEdgeDet_roiList->at(i);
|
|
cv::Mat outMask;
|
|
if (!CheckUtil::RoiInImg(r, img))
|
|
{
|
|
|
|
continue;
|
|
}
|
|
cv::Mat detimg = img(r).clone();
|
|
if (bdege || nRuntype != 0)
|
|
{
|
|
|
|
cv::rectangle(showImg, r, cv::Scalar(255, 0, 0));
|
|
}
|
|
|
|
int re = m_pAIDeal->AIDet(detimg, outMask);
|
|
if (re != 0)
|
|
{
|
|
|
|
continue;
|
|
}
|
|
if (outMask.empty())
|
|
{
|
|
|
|
continue;
|
|
}
|
|
bool isAllZero = true;
|
|
if (cv::countNonZero(outMask) > 0)
|
|
{
|
|
isAllZero = false;
|
|
if (bdege || nRuntype != 0)
|
|
{
|
|
|
|
outMask.copyTo(showImg(r), outMask);
|
|
}
|
|
}
|
|
|
|
SaveProcessImg(detimg, outMask, isAllZero, pDetConfig, i, nRuntype);
|
|
|
|
// if (!isAllZero)
|
|
// {
|
|
// cv::imwrite("edge_AI_outMask.png", outMask);
|
|
// cv::imwrite("edge_AI_pre_detmask(r).png", detmask(r));
|
|
// }
|
|
|
|
cv::Point p_smallImg_lt;
|
|
cv::Point p_smallImg_rb;
|
|
p_smallImg_lt.x = 0;
|
|
p_smallImg_lt.y = 0;
|
|
p_smallImg_rb.x = r.width;
|
|
p_smallImg_rb.y = r.height;
|
|
|
|
cv::Point p_mask_lt;
|
|
cv::Point p_mask_rb;
|
|
|
|
p_mask_lt.x = r.x - pDetConfig->CutRoi.x;
|
|
p_mask_lt.y = r.y - pDetConfig->CutRoi.y;
|
|
p_mask_rb.x = p_mask_lt.x + r.width;
|
|
p_mask_rb.y = p_mask_lt.y + r.height;
|
|
|
|
if (p_mask_lt.x < 0)
|
|
{
|
|
p_smallImg_lt.x = -p_mask_lt.x;
|
|
|
|
p_mask_lt.x = 0;
|
|
}
|
|
if (p_mask_lt.y < 0)
|
|
{
|
|
p_smallImg_lt.y = -p_mask_lt.y;
|
|
p_mask_lt.y = 0;
|
|
}
|
|
if (p_mask_rb.x > detmask.cols)
|
|
{
|
|
p_smallImg_rb.x -= (p_mask_rb.x - detmask.cols);
|
|
p_mask_rb.x = detmask.cols;
|
|
}
|
|
if (p_mask_rb.y > detmask.rows)
|
|
{
|
|
p_smallImg_rb.y -= (p_mask_rb.y - detmask.rows);
|
|
p_mask_rb.y = detmask.rows;
|
|
}
|
|
|
|
cv::Rect mask_roi;
|
|
mask_roi.x = p_mask_lt.x;
|
|
mask_roi.y = p_mask_lt.y;
|
|
mask_roi.width = p_mask_rb.x - p_mask_lt.x;
|
|
mask_roi.height = p_mask_rb.y - p_mask_lt.y;
|
|
|
|
cv::Rect smallImg_roi;
|
|
smallImg_roi.x = p_smallImg_lt.x;
|
|
smallImg_roi.y = p_smallImg_lt.y;
|
|
smallImg_roi.width = p_smallImg_rb.x - p_smallImg_lt.x;
|
|
smallImg_roi.height = p_smallImg_rb.y - p_smallImg_lt.y;
|
|
|
|
if (!CheckUtil::RoiInImg(smallImg_roi, outMask))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!CheckUtil::RoiInImg(mask_roi, detmask))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
outMask *= 0.5;
|
|
if (nRuntype != 0)
|
|
{
|
|
outMask(smallImg_roi).copyTo(edge_mask(mask_roi), outMask(smallImg_roi));
|
|
cv::rectangle(cut_show_G, mask_roi, cv::Scalar(255, 0, 0));
|
|
}
|
|
else
|
|
{
|
|
if (pDetConfig->pdege_Det_config->type == Function_Edge_Det::Edge_type_Add)
|
|
{
|
|
outMask(smallImg_roi).copyTo(detmask(mask_roi), outMask(smallImg_roi));
|
|
}
|
|
else if (pDetConfig->pdege_Det_config->type == Function_Edge_Det::Edge_type_And)
|
|
{
|
|
cv::bitwise_and(detmask(mask_roi), outMask(smallImg_roi), detmask(mask_roi));
|
|
}
|
|
else
|
|
{
|
|
outMask(smallImg_roi).copyTo(detmask(mask_roi));
|
|
}
|
|
}
|
|
}
|
|
if (nRuntype != 0)
|
|
{
|
|
cut_show_R += edge_mask * 0.5;
|
|
std::vector<cv::Mat> chs = {cut_show_B, cut_show_G, cut_show_R};
|
|
cv::Mat merged;
|
|
cv::merge(chs, merged);
|
|
cv::resize(merged, merged, cv::Size(merged.cols * 0.5, merged.rows * 0.5));
|
|
std::string str = GetImgSaveName(0) + "_mask.png";
|
|
cv::imwrite(str, merged);
|
|
// std::cout<<str;
|
|
// getchar();
|
|
}
|
|
|
|
if (bdege)
|
|
{
|
|
cut_show_R += edge_mask * 0.5;
|
|
std::vector<cv::Mat> chs = {cut_show_B, cut_show_G, cut_show_R};
|
|
cv::Mat merged;
|
|
cv::merge(chs, merged);
|
|
cv::resize(merged, merged, cv::Size(merged.cols * 0.5, merged.rows * 0.5));
|
|
std::string str = GetImgSaveName(0) + "_mask.png";
|
|
cv::imwrite(str, merged);
|
|
|
|
str = GetImgSaveName(0) + "_showImg.png";
|
|
cv::imwrite(str, showImg);
|
|
}
|
|
|
|
return 0;
|
|
}
|