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_Mark_Det.cpp

378 lines
9.9 KiB

#include "AI_Edge_Algin.h"
#include "CheckErrorCodeDefine.hpp"
#include "AI_Mark_Det.h"
AI_Mark_Det::AI_Mark_Det()
{
m_bModelSucc = false;
CheckUtil::CreateDir("/home/aidlux/BOE/FOG/MarkLine/");
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_Mark_Det::~AI_Mark_Det()
{
}
int AI_Mark_Det::Detect(const cv::Mat &img, DetConfigResult *pDetConfig)
{
m_pdetlog = pDetConfig->pdetlog;
// printf("AI_Mark_Det::Detect \n");
cv::Rect searchroi = pDetConfig->searchroi;
if (searchroi.x < 0)
{
searchroi.x = 0;
}
if (searchroi.y < 0)
{
searchroi.y = 0;
}
if (searchroi.x + searchroi.width > img.cols)
{
searchroi.width = img.cols - searchroi.x;
}
if (searchroi.y + searchroi.width > img.rows)
{
searchroi.height = img.rows - searchroi.y;
}
// 每个小矩形的大小
int rectWidth = 512;
int rectHeight = 512;
// 重叠区域大小
int overlap = 50;
// 计算步长
int stepX = rectWidth - overlap;
int stepY = rectHeight - overlap;
int x_start = searchroi.x;
int x_end = searchroi.x + searchroi.width;
int y_start = searchroi.y;
int y_end = searchroi.y + searchroi.height;
if (searchroi.width < 512)
{
int cx = searchroi.x + searchroi.width * 0.5;
x_start = cx - rectWidth * 0.5;
x_end = x_start + rectWidth;
}
if (searchroi.height < 512)
{
int cy = searchroi.y + searchroi.height * 0.5;
y_start = cy - rectHeight * 0.5;
y_end = y_start + rectHeight;
}
if (x_start < 0)
{
x_start = 0;
}
if (y_start < 0)
{
y_start = 0;
}
if (y_start + searchroi.height > img.rows)
{
y_start = img.rows - searchroi.height;
}
if (x_start + searchroi.width > img.cols)
{
x_start = img.cols - searchroi.width;
}
if (y_end > img.rows)
{
y_end = img.rows;
}
if (x_end > img.cols)
{
x_end = img.cols;
}
// cv::Mat showImg;
// if (true)
// {
// cv::cvtColor(detSrcImg, showImg, cv::COLOR_GRAY2BGR);
// }
int cut_y_s = y_start;
int cut_y_e = cut_y_s + rectHeight;
int cut_x_s = x_start;
int cut_x_e = cut_x_s + rectWidth;
bool bb_y = false;
bool bb_x = false;
int w = x_end - x_start;
int h = y_end - y_start;
if (w < 512)
{
if (x_start == 0)
{
x_end = x_start + 512;
if (x_end > img.cols)
{
return 1;
}
}
else
{
x_start = x_end - 512;
if (x_start < 0)
{
return 1;
}
}
}
if (h < 512)
{
if (y_start == 0)
{
y_end = y_start + 512;
if (y_end > img.rows)
{
return 1;
}
}
else
{
y_start = y_end - 512;
if (y_start < 0)
{
return 1;
}
}
}
w = x_end - x_start;
h = y_end - y_start;
if (w < rectWidth)
{
return 1;
}
if (h < rectHeight)
{
return 1;
}
cv::Rect allroi(x_start, y_start, w, h);
cv::Mat allmask = cv::Mat(h, w, CV_8UC1, cv::Scalar(0));
// printf("y_start %d y_end %d\n", y_start, y_end);
// printf("x_start %d x_end %d\n", x_start, x_end);
std::shared_ptr<AIModel_Base> pAI_Model = AI_Factory->AI_defect_MarkLine;
static int ki = 0;
//
for (int y = y_start; y < y_end; y += stepY)
{
cut_y_s = y;
cut_y_e = cut_y_s + rectHeight;
if (cut_y_e > y_end)
{
cut_y_e = y_end;
cut_y_s = y_end - rectHeight;
bb_y = true;
}
bb_x = false;
for (int x = x_start; x < x_end; x += stepX)
{
cut_x_s = x;
cut_x_e = cut_x_s + rectWidth;
if (cut_x_e > x_end)
{
cut_x_e = x_end;
cut_x_s = x_end - rectWidth;
bb_x = true;
}
// 定义小矩形
cv::Rect smallRect(cut_x_s, cut_y_s, rectWidth, rectHeight);
cv::Rect maskrect = smallRect;
maskrect.x -= x_start;
maskrect.y -= y_start;
if (!CheckUtil::RoiInImg(smallRect, img))
{
// printf("img %d %d= \n", img.cols, img.rows);
// CheckUtil::printROI(smallRect, "smallRect");
continue;
}
cv::Mat outmask;
cv::Mat temdet = img(smallRect).clone();
if (m_bModelSucc)
{
int re = pAI_Model->AIDet(temdet, outmask);
// m_pTemCheck->AddCheckstr(PrintLevel_3, DET_LOG_LEVEL_3, "AICheck_RE Area", "AI Model POL");
// 推理是否成功
if (re != 0)
{
m_pdetlog->AddCheckstr(PrintLevel_2, "AI_Mark_Det ", "Error POL AI Model Error");
continue;
}
}
else
{
m_pdetlog->AddCheckstr(PrintLevel_2, "AI_Mark_Det ", "Error POL AI Model Error");
continue;
}
if (outmask.empty())
{
m_pdetlog->AddCheckstr(PrintLevel_2, "AI_Mark_Det ", "Error POL AI Error");
continue;
}
// printf("AI_Mark_Det::Detect ========3333============ \n");
// printf("img %d %d= \n", allmask.cols, allmask.rows);
// CheckUtil::printROI(maskrect, "maskrect---");
outmask.copyTo(allmask(maskrect), outmask);
if (pDetConfig->bDebugsaveimg || pDetConfig->bDetSaveImg)
{
std::string str12 = "/home/aidlux/BOE/FOG/MarkLine/" + pDetConfig->strCamName + "_" + std::to_string(ki) + "_in.png";
std::string strmask = "/home/aidlux/BOE/FOG/MarkLine/" + pDetConfig->strCamName + "_" + std::to_string(ki) + "_in_mask.png";
if (pDetConfig->bDebugsaveimg)
{
str12 = pDetConfig->strCamName + "_" + std::to_string(ki) + "_in.png";
strmask = pDetConfig->strCamName + "_" + std::to_string(ki) + "_in_mask.png";
}
ki++;
if (ki > 999999)
{
ki = 0;
}
cv::imwrite(str12, img(smallRect));
cv::imwrite(strmask, outmask);
}
if (bb_x)
{
break;
/* code */
}
}
if (bb_y)
{
break;
}
}
bool bf = false;
cv::Rect markroi = CheckUtil::getLargestContourROI(allmask, bf);
if (bf)
{
pDetConfig->nresult = 0;
pDetConfig->markRoi = markroi;
if (pDetConfig->bDebugsaveimg || pDetConfig->bDetSaveImg)
{
std::string str12 = "/home/aidlux/BOE/FOG/MarkLine/Check_big_" + pDetConfig->strCamName + "_in.png";
std::string strmask = "/home/aidlux/BOE/FOG/MarkLine/Check_big_" + pDetConfig->strCamName + "_in_mask.png";
if (pDetConfig->bDebugsaveimg)
{
str12 = "Check_big_" + pDetConfig->strCamName + "_" + std::to_string(ki) + "_in.png";
strmask = "Check_big_" + pDetConfig->strCamName + "_" + std::to_string(ki) + "_in_mask.png";
}
// CheckUtil::printROI(allroi, "allroi");
// printf("-------------------1111 33 img %d %d\n", img.cols, img.rows);
cv::Mat detimg = img(allroi).clone();
// printf("-------------------1111 33 \n");
cv::rectangle(detimg, pDetConfig->markRoi, cv::Scalar(180), 3);
// printf("-------------------1111 444\n");
cv::imwrite(str12, detimg);
cv::imwrite(strmask, allmask);
}
pDetConfig->markRoi.x += x_start;
pDetConfig->markRoi.y += y_start;
}
else
{
}
return 0;
}
int AI_Mark_Det::Det_img(const cv::Mat &img, DetConfigResult *pDetConfig)
{
return 0;
}
int AI_Mark_Det::Analysisy(const cv::Mat &maskImg, DetConfigResult *pDetConfig)
{
return 0;
}
int AI_Mark_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;
}