From 3938c9b1daa66344d6e2e98d544179e36d737b96 Mon Sep 17 00:00:00 2001 From: liusiyang Date: Mon, 14 Sep 2026 09:52:51 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96edge=E8=80=97?= =?UTF-8?q?=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AlgorithmModule/include/AI_Edge_Algin.h | 2 +- AlgorithmModule/src/AI_Edge_Algin.cpp | 54 +++++++++++++++-------- AlgorithmModule/src/ImageResultJudge.cpp | 2 - AlgorithmModule/src/ImgCheckAnalysisy.cpp | 14 +++--- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/AlgorithmModule/include/AI_Edge_Algin.h b/AlgorithmModule/include/AI_Edge_Algin.h index 11735ad..8fff4ec 100644 --- a/AlgorithmModule/include/AI_Edge_Algin.h +++ b/AlgorithmModule/include/AI_Edge_Algin.h @@ -160,7 +160,7 @@ public: private: int InitModel_Big(); - int Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDetConfig, std::string strChannel, std::vector &RoiList, Mat &outmask); + int Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDetConfig, std::string strChannel, std::vector &RoiList, Mat &outmask, bool bResizeOut = false); int creatsavedir(); diff --git a/AlgorithmModule/src/AI_Edge_Algin.cpp b/AlgorithmModule/src/AI_Edge_Algin.cpp index 173c2c3..6ad53e7 100644 --- a/AlgorithmModule/src/AI_Edge_Algin.cpp +++ b/AlgorithmModule/src/AI_Edge_Algin.cpp @@ -9,6 +9,7 @@ #include "AI_Edge_Algin.h" #include "CheckErrorCodeDefine.hpp" +#include #define EDGE_GPU 0 @@ -231,8 +232,10 @@ int AI_Edge_Algin::Detect(const cv::Mat &img, DetConfig *pDetConfig, std::shared return 1; } - // 1、初步定位 找到产品大致区域 - int re = 0; + // 1、初步定位 找到产品大致区域(三个模型输入输出尺寸不同,各自 resize;推理相互独立,并行执行) + int re0 = 0; + int re1 = 0; + int re2 = 0; std::vector Big_roi; std::vector Small_roi; @@ -241,35 +244,41 @@ int AI_Edge_Algin::Detect(const cv::Mat &img, DetConfig *pDetConfig, std::shared cv::Mat Small_outimg; cv::Mat Tag_outimg; - re = Get_Edge(0, img, pDetConfig, m_pDetConfig->strChannel, Big_roi, Big_outimg); - if (re != 0) + std::vector vecThreads; + vecThreads.emplace_back([&]() { re0 = Get_Edge(0, img, pDetConfig, m_pDetConfig->strChannel, Big_roi, Big_outimg, true); }); + vecThreads.emplace_back([&]() { re1 = Get_Edge(1, img, pDetConfig, m_pDetConfig->strChannel, Small_roi, Small_outimg, false); }); + vecThreads.emplace_back([&]() { re2 = Get_Edge(2, img, pDetConfig, m_pDetConfig->strChannel, Tag_roi, Tag_outimg, false); }); + for (auto &t : vecThreads) + { + t.join(); + } + + if (re0 != 0) { - m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Edge_Big----error %d ", re); + m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Edge_Big----error %d ", re0); if (m_pDetConfig->IsSaveProcessImg()) { cv::imwrite(str_error, img); } - return re; + return re0; } - re = Get_Edge(1, img, pDetConfig, m_pDetConfig->strChannel, Small_roi, Small_outimg); - if (re != 0) + if (re1 != 0) { - m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Edge_Small----error %d ", re); + m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Edge_Small----error %d ", re1); if (m_pDetConfig->IsSaveProcessImg()) { cv::imwrite(str_error, img); } - return re; + return re1; } - re = Get_Edge(2, img, pDetConfig, m_pDetConfig->strChannel, Tag_roi, Tag_outimg); - if (re != 0) + if (re2 != 0) { - m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Tag----error %d ", re); + m_pdetlog->AddCheckstr(PrintLevel_2, DET_LOG_LEVEL_3, "AI_Edge_Algin ", "AICheck_Tag----error %d ", re2); if (m_pDetConfig->IsSaveProcessImg()) { cv::imwrite(str_error, img); } - return re; + return re2; } m_pCheckResult_Aling->bigroi = Big_roi; @@ -348,7 +357,7 @@ int AI_Edge_Algin::InitModel_ALL() return 0; } -int AI_Edge_Algin::Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDetConfig, std::string strChannel, std::vector &RoiList, Mat &outmask) +int AI_Edge_Algin::Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDetConfig, std::string strChannel, std::vector &RoiList, Mat &outmask, bool bResizeOut) { std::shared_ptr pBackPlate_Align; switch (AIModel_type) @@ -370,8 +379,8 @@ int AI_Edge_Algin::Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDe sz.width = pBackPlate_Align->input_0.width; sz.height = pBackPlate_Align->input_0.height; cv::Mat detImg; - cout<< pDetConfig->strChannel << ": " << "---Get_Edge-resize-" << to_string(AIModel_type) <<"-- "; - cout << "imgSize: " << img.size() << ", " << "detImgSize: " << detImg.size() << ", " << "szSize: " << sz << endl; + // cout<< pDetConfig->strChannel << ": " << "---Get_Edge-resize-" << to_string(AIModel_type) <<"-- "; + // cout << "imgSize: " << img.size() << ", " << "detImgSize: " << detImg.size() << ", " << "szSize: " << sz << endl; cv::resize(img, detImg, sz); int re = 0; @@ -413,8 +422,15 @@ int AI_Edge_Algin::Get_Edge(int AIModel_type, const cv::Mat &img, DetConfig *pDe cv::imwrite(strChannel +"_edge_"+ to_string(AIModel_type) +"_in.png", detImg); cv::imwrite(strChannel +"_edge_"+ to_string(AIModel_type) +"_out_mask.png", mask); } - // resize out_mask并返回 - cv::resize(mask, outmask, img.size()); + // resize out_mask并返回(仅大轮廓 mask 需要原图尺寸,后续用于把手检测;其余保持小图) + if (bResizeOut) + { + cv::resize(mask, outmask, img.size()); + } + else + { + outmask = mask; + } // 检查模型输入尺寸 if (sz.width <= 0 || sz.height <= 0) diff --git a/AlgorithmModule/src/ImageResultJudge.cpp b/AlgorithmModule/src/ImageResultJudge.cpp index afd8359..9223fdd 100644 --- a/AlgorithmModule/src/ImageResultJudge.cpp +++ b/AlgorithmModule/src/ImageResultJudge.cpp @@ -566,9 +566,7 @@ int ImageResultJudge::ResultJudge(std::shared_ptr pImageResult) GetAIDetImg(pImageResult, pCenter, tem.AI_in_Img, tem.AI_out_img); m_CheckResult_shareP->qxImageResult.push_back(tem); - pQxLog->bPrintStr = true; pQxLog->AddCheckstr(PrintLevel_4, DET_LOG_LEVEL_3, " result ", " ---name: %s ---code: %s ---srcImgroi.x: %d ---srcImgroi.y: %d\n", tem.strTypeName.c_str(), tem.qx_Code.c_str(), tem.srcImgroi.x, tem.srcImgroi.y); - pQxLog->bPrintStr = false; } else { diff --git a/AlgorithmModule/src/ImgCheckAnalysisy.cpp b/AlgorithmModule/src/ImgCheckAnalysisy.cpp index 60efa66..57149f7 100644 --- a/AlgorithmModule/src/ImgCheckAnalysisy.cpp +++ b/AlgorithmModule/src/ImgCheckAnalysisy.cpp @@ -425,7 +425,7 @@ int ImgCheckAnalysisy::CheckRun() { m_pdetlog->bPrintStr = true; } - m_pdetlog->bPrintStr = true; + // m_pdetlog->bPrintStr = true; m_pdetlog->AddCheckstr(PrintLevel_0, "1、basic Info", "---------------------------1、basic Info---------------------------------"); m_pdetlog->AddCheckstr(PrintLevel_0, "Version", "%s", GetVersion().c_str()); @@ -2065,13 +2065,17 @@ int ImgCheckAnalysisy::AI_Edge(const cv::Mat &img, cv::RotatedRect &outerRoi, cv } /*使用m_pEdge_Align_Result->bigmask检测把手是否缺失*/ - // 截取把手大致区域 + // 截取把手大致区域(使用把手模板完整 boundingRect,而非整图高度) RotatedRect tpl_handle_rroi = m_pFuntion->function.f_supportDet.handleRect; - Rect handle_rect = Rect(tpl_handle_rroi.boundingRect().x, 0, tpl_handle_rroi.boundingRect().width, m_pEdge_Align_Result->bigmask.rows); - Mat handle_roi = m_pEdge_Align_Result->bigmask(handle_rect & Rect(0, 0, m_pEdge_Align_Result->bigmask.cols, m_pEdge_Align_Result->bigmask.rows)).clone(); + Rect handle_rect = tpl_handle_rroi.boundingRect() & Rect(0, 0, m_pEdge_Align_Result->bigmask.cols, m_pEdge_Align_Result->bigmask.rows); + if (handle_rect.width <= 0 || handle_rect.height <= 0) + { + return re; + } + Mat handle_roi = m_pEdge_Align_Result->bigmask(handle_rect).clone(); // 对handle_roi做一下开运算 - Mat element = getStructuringElement(MORPH_RECT, Size(tpl_handle_rroi.boundingRect().width / 3, tpl_handle_rroi.boundingRect().height / 3)); + Mat element = getStructuringElement(MORPH_RECT, Size(std::max(1, tpl_handle_rroi.boundingRect().width / 3), std::max(1, tpl_handle_rroi.boundingRect().height / 3))); morphologyEx(handle_roi, handle_roi, MORPH_OPEN, element); // imwrite("handle_roi.png", handle_roi);