From f0eb78686450fb3d8ea35dcbf6d31c4a57d4fa7b Mon Sep 17 00:00:00 2001 From: xiewenji <527774126@qq.com> Date: Tue, 18 Aug 2026 15:35:17 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=95=B4=E7=90=86Pin=E8=84=9A?= =?UTF-8?q?=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AlgorithmModule/include/ImgCheckAnalysisy.hpp | 1 + AlgorithmModule/include/Pin_QX_Det.h | 116 -------------- AlgorithmModule/src/ALLImgCheckAnalysisy.cpp | 13 +- AlgorithmModule/src/ImgCheckAnalysisy.cpp | 142 ++++-------------- AlgorithmModule/src/Pin_QX_Det.cpp | 60 +------- 5 files changed, 40 insertions(+), 292 deletions(-) diff --git a/AlgorithmModule/include/ImgCheckAnalysisy.hpp b/AlgorithmModule/include/ImgCheckAnalysisy.hpp index 01c0ad2..bed1fb7 100644 --- a/AlgorithmModule/include/ImgCheckAnalysisy.hpp +++ b/AlgorithmModule/include/ImgCheckAnalysisy.hpp @@ -295,6 +295,7 @@ private: // 模板信息 cv::RotatedRect m_tplOuterRect; // 模板外接矩形 std::vector> m_tplPinContours; // 模板 pin blob 轮廓点集 + std::vector> m_curPinContours; // 当前 pin blob 轮廓点集 }; #endif \ No newline at end of file diff --git a/AlgorithmModule/include/Pin_QX_Det.h b/AlgorithmModule/include/Pin_QX_Det.h index b1c4615..e00eca5 100644 --- a/AlgorithmModule/include/Pin_QX_Det.h +++ b/AlgorithmModule/include/Pin_QX_Det.h @@ -59,10 +59,6 @@ public: BaseCheckFunction *pBaseCheckFunction; std::string strChannel; - std::vector qx_result; - std::vector pin_det_roi; - - std::vector Det_region; bool bSaveResultImg; DetConfigResult() { @@ -72,106 +68,7 @@ public: { pBaseCheckFunction = NULL; strChannel = ""; - qx_result.clear(); - pin_det_roi.clear(); - Det_region.clear(); - bSaveResultImg = false; - } - }; - - enum Det_ROI_Type - { - Det_ROI_Type_UP, - Det_ROI_Type_DOWN, - Det_ROI_Type_LEFT, - Det_ROI_Type_RIGHT, - }; - - // 边缘搜索定位结果 - struct Pin_AI_Result - { - int nresult; - cv::RotatedRect bigroi; - cv::RotatedRect smallroi; - bool buseOfft; - int offt_x; - int offt_y; - cv::Mat H; - - Pin_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); - if (!H.empty()) - { - H.release(); - /* code */ - } - } - }; - - enum SaveProcessType - { - Save_Close, // 不保存 - Save_Filter, // 过滤的 - Save_ALL, // 全部 - }; - 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; } }; @@ -180,30 +77,17 @@ public: ~Pin_QX_Det(); int GetPinMask(const cv::Mat &img, DetConfigResult *pDetConfig, Mat &mask); - int Detect(const cv::Mat &img, DetConfigResult *pDetConfig); private: int InitModel_Pin(); - int creatsavedir(); private: bool m_bInitSucc; // 是否初始化成功 - // 检测结果 - std::shared_ptr m_pCheckResult_Pin; std::shared_ptr& m_pdetlog; - bool m_bInitialized; - bool m_bModelSucc; - bool m_bModel_Mark_Succ; - bool m_bshowimg; cv::Mat showimg; - std::string m_strRootPath_Pin; - std::string m_strSavePath_Pin; - std::string m_strLastDate; - - ImageStorage *m_pImageStorage; std::shared_ptr AI_Factory; diff --git a/AlgorithmModule/src/ALLImgCheckAnalysisy.cpp b/AlgorithmModule/src/ALLImgCheckAnalysisy.cpp index f90277f..9af3878 100644 --- a/AlgorithmModule/src/ALLImgCheckAnalysisy.cpp +++ b/AlgorithmModule/src/ALLImgCheckAnalysisy.cpp @@ -428,8 +428,6 @@ int ALLImgCheckAnalysisy::InitTemplateMask() // mask 尺寸 -> 裁剪图(pinImg) 尺寸 的缩放比例 const float pin_scale_x = (tplPinMask.cols > 0) ? static_cast(pinImg.cols) / tplPinMask.cols : 0.0f; const float pin_scale_y = (tplPinMask.rows > 0) ? static_cast(pinImg.rows) / tplPinMask.rows : 0.0f; - // 裁剪区域在 tplImg 上的偏移 - const cv::Point pin_offset = tplOuterRect.boundingRect().tl(); // 将轮廓点从 mask 坐标系 映射回 tplImg 坐标系 m_tplPinContours.clear(); m_tplPinContours.reserve(pin_contours.size()); @@ -439,8 +437,8 @@ int ALLImgCheckAnalysisy::InitTemplateMask() mapped.reserve(pin_contours[i].size()); for (const auto &pt : pin_contours[i]) { - mapped.emplace_back(cvRound(pt.x * pin_scale_x) + pin_offset.x, - cvRound(pt.y * pin_scale_y) + pin_offset.y); + mapped.emplace_back(cvRound(pt.x * pin_scale_x), + cvRound(pt.y * pin_scale_y)); } m_tplPinContours.push_back(mapped); } @@ -449,10 +447,9 @@ int ALLImgCheckAnalysisy::InitTemplateMask() // 绘制到 tplImg 上 { - Mat showImg = tplImg.clone(); - rectangle(showImg, tplOuterRect.boundingRect(), Scalar(0, 0, 255), 2); - cv::drawContours(showImg, m_tplPinContours, -1, Scalar(0, 255, 0), 2); - cv::imwrite("/home/aidlux/BOE/ZB_SOCKET/Tpl/tpl_pin_contours.png", showImg); + Mat showImg = pinImg.clone(); + cv::drawContours(pinImg, m_tplPinContours, -1, Scalar(0, 255, 0), 2); + cv::imwrite("/home/aidlux/BOE/ZB_SOCKET/Tpl/tpl_pin_contours.png", pinImg); } m_pdetlog->AddCheckstr(PrintLevel_1, DET_LOG_LEVEL_1, "---Init Tpl---" , "end"); diff --git a/AlgorithmModule/src/ImgCheckAnalysisy.cpp b/AlgorithmModule/src/ImgCheckAnalysisy.cpp index 92e1ca5..ee7a3fa 100644 --- a/AlgorithmModule/src/ImgCheckAnalysisy.cpp +++ b/AlgorithmModule/src/ImgCheckAnalysisy.cpp @@ -442,7 +442,6 @@ int ImgCheckAnalysisy::CheckRun() cv::RotatedRect inner_roi; // 模型定位内外边缘 int reedge = AI_Edge(m_CheckResult_shareP->in_shareImage->img, outer_roi, inner_roi); - m_outer_roi = outer_roi.boundingRect(); if (reedge != 0) { m_pdetlog->AddCheckstr(PrintLevel_0, "Error", "AI_Edge is error type = %d", reedge); @@ -462,7 +461,7 @@ int ImgCheckAnalysisy::CheckRun() /*投影对齐模板*/ Adapt_Config(m_tplOuterRect, outer_roi, m_CheckResult_shareP->in_shareImage->img); - + m_outer_roi = m_tplOuterRect.boundingRect(); m_Crop_Roi_paramImg = m_outer_roi; m_pImageAllResult->pDetResult->CutRoi = m_outer_roi; m_pImageAllResult->pDetResult->Param_CropRoi = m_Crop_Roi_paramImg; @@ -488,7 +487,7 @@ int ImgCheckAnalysisy::CheckRun() ImgPreDet(); /*更新检测区域*/ - Update_DetRoiList(); + // Update_DetRoiList(); m_pdetlog->AddCheckstr(PrintLevel_0, "3、pre detect", "-------------------------pre Det--------------- \n"); @@ -1682,43 +1681,6 @@ int ImgCheckAnalysisy::DrawResult_Step_1() float fs_resize_x = m_pImageAllResult->resultImg.cols * 1.0f / m_pImageAllResult->detImg.cols; float fs_resize_y = m_pImageAllResult->resultImg.rows * 1.0f / m_pImageAllResult->detImg.rows; - // 要绘制结果 - if (m_pbaseCheckFunction && m_pbaseCheckFunction->edgeDet.bDrawResult) - { - for (const auto &r : m_Edge_DetConfig.pin_det_roi) - { - cv::Rect droi; - droi.x = r.roi.x * fs_resize_x; - droi.y = r.roi.y * fs_resize_y; - droi.width = r.roi.width * fs_resize_x; - droi.height = r.roi.height * fs_resize_y; - - cv::rectangle(m_CheckResult_shareP->resultimg, droi, cv::Scalar(255, 0, 255), 1); // 黄色角点 - } - - if (m_Edge_DetConfig.Det_region.size() > 2) - { - for (int i = 0; i < m_Edge_DetConfig.Det_region.size() - 1; i++) - { - cv::Point p1; - p1.x = m_Edge_DetConfig.Det_region[i].x * fs_resize_x; - p1.y = m_Edge_DetConfig.Det_region[i].y * fs_resize_y; - cv::Point p2; - p2.x = m_Edge_DetConfig.Det_region[i + 1].x * fs_resize_x; - p2.y = m_Edge_DetConfig.Det_region[i + 1].y * fs_resize_y; - cv::line(m_CheckResult_shareP->resultimg, p1, p2, cv::Scalar(0, 255, 255)); - } - { - cv::Point p1; - p1.x = m_Edge_DetConfig.Det_region[0].x * fs_resize_x; - p1.y = m_Edge_DetConfig.Det_region[0].y * fs_resize_y; - cv::Point p2; - p2.x = m_Edge_DetConfig.Det_region[m_Edge_DetConfig.Det_region.size() - 1].x * fs_resize_x; - p2.y = m_Edge_DetConfig.Det_region[m_Edge_DetConfig.Det_region.size() - 1].y * fs_resize_y; - cv::line(m_CheckResult_shareP->resultimg, p1, p2, cv::Scalar(0, 255, 255)); - } - } - } if (m_pbaseCheckFunction && m_pbaseCheckFunction->markLine.region.size() > 1) { std::vector product_roi_show; @@ -1828,77 +1790,39 @@ int ImgCheckAnalysisy::Pin_Qx_Det(const cv::Mat &img) m_Edge_DetConfig.bSaveResultImg = true; } // m_pbaseCheckFunction->print("Pin_Qx_Det"); - int re = m_Pin_QX_Det.Detect(img, &m_Edge_DetConfig); - if (re == 0) - { - for (const auto r : m_Edge_DetConfig.qx_result) + Mat pin_mask; + int re = m_Pin_QX_Det.GetPinMask(img, &m_Edge_DetConfig, pin_mask); + + // 统计tplPinMask的blob边缘点,并绘制到tplImg上 + vector> pin_contours; + vector hierarchy; + cv::findContours(pin_mask, pin_contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE); + //缩放比例 + const float pin_scale_x = (pin_mask.cols > 0) ? static_cast(img.cols) / pin_mask.cols : 0.0f; + const float pin_scale_y = (pin_mask.rows > 0) ? static_cast(img.rows) / pin_mask.rows : 0.0f; + // 将轮廓点从 mask 坐标系 映射回 tplImg 坐标系 + m_curPinContours.reserve(pin_contours.size()); + for (size_t i = 0; i < pin_contours.size(); i++) + { + std::vector mapped; + mapped.reserve(pin_contours[i].size()); + for (const auto &pt : pin_contours[i]) { - QX_ERROR_INFO_ temerror; - temerror.roi = r.roi_src; - temerror.Idx = m_pDetResult->pQx_ErrorList->size(); - temerror.area = r.area_pixel; - temerror.JudgArea = r.area_pixel * m_fImgage_Scale_X * m_fImgage_Scale_Y; - temerror.JudgArea_second = temerror.JudgArea; - temerror.energy = 99999999; - temerror.flen = std::max(r.roi_src.width, r.roi_src.height); - temerror.fbreadth = std::min(r.roi_src.width, r.roi_src.height); - temerror.nconfig_qx_type = CONFIG_QX_NAME_cell_edge; - temerror.qx_name = CONFIG_QX_NAME_Names[CONFIG_QX_NAME_cell_edge]; - temerror.maxValue = 0; - temerror.grayDis = 255; - temerror.density = 0; - temerror.fUpIou = 0; - - { - cv::Point pCenter; - pCenter.x = temerror.roi.x + temerror.roi.width * 0.5; - pCenter.y = temerror.roi.y + temerror.roi.height * 0.5; - int nmaxregionIdx = 0; - for (int iregion = 0; iregion < m_DetRoiList.roiList_Src.size(); iregion++) - { - const std::vector &polygon = m_DetRoiList.roiList_Src[iregion]; - double result = cv::pointPolygonTest(polygon, pCenter, false); - if (result < 0) - { - continue; - } - nmaxregionIdx = iregion; - } - - temerror.detRegionidxList.push_back(nmaxregionIdx); - } - - // { - // cv::Point pCenter; - // pCenter.x = temerror.roi.x + temerror.roi.width * 0.5; - // pCenter.y = temerror.roi.y + temerror.roi.height * 0.5; - // int nmaxregionIdx = 0; - // for (int iregion = 0; iregion < m_DetRoiList.roiList_Src.size(); iregion++) - // { - // const std::vector &polygon = m_DetRoiList.roiList_Src[iregion]; - // double result = cv::pointPolygonTest(polygon, pCenter, false); - // if (result < 0) - // { - // continue; - // } - // temerror.detRegionidxList.push_back(iregion); - // } - // if (temerror.detRegionidxList.size() <= 0) - // { - // temerror.detRegionidxList.push_back(0); - // } - // } - - m_pDetResult->pQx_ErrorList->push_back(temerror); + mapped.emplace_back(cvRound(pt.x * pin_scale_x), + cvRound(pt.y * pin_scale_y)); } - - m_pdetlog->AddCheckstr(PrintLevel_1, "Pin_Qx_Det", " succ qx num %ld", m_Edge_DetConfig.qx_result.size()); - // printf("- %s idx %d a %f v %d h %f l %f\n", DetImgInfo_shareP->strChannel.c_str(), i, JudgArea, blobs.blobTab[i].maxValue, blobs.blobTab[i].grayDis, flen); - } - else - { - m_pdetlog->AddCheckstr(PrintLevel_1, "Pin_Qx_Det", " error %d", re); - } + m_curPinContours.push_back(mapped); + } + + + // if (m_Edge_DetConfig.bSaveResultImg) + { + Mat showimg; + cv::cvtColor(img, showimg, cv::COLOR_GRAY2BGR); + cv::drawContours(showimg, m_curPinContours, -1, cv::Scalar(0, 0, 255), 1); + cv::drawContours(showimg, m_tplPinContours, -1, cv::Scalar(0, 255, 0), 1); + imwrite("pin_contours.png", showimg); + } return 0; } diff --git a/AlgorithmModule/src/Pin_QX_Det.cpp b/AlgorithmModule/src/Pin_QX_Det.cpp index 3805e42..475bba4 100644 --- a/AlgorithmModule/src/Pin_QX_Det.cpp +++ b/AlgorithmModule/src/Pin_QX_Det.cpp @@ -5,34 +5,10 @@ #include #include "CheckErrorCodeDefine.hpp" -int Pin_QX_Det::creatsavedir() -{ - std::string curDate = CheckUtil::getCurrentDate(); - if (curDate == m_strLastDate) - { - return 0; - } - m_strLastDate = curDate; - m_strSavePath_Pin = m_strRootPath_Pin + curDate + "/"; - - CheckUtil::CreateDir(m_strSavePath_Pin); - - return 0; - return 0; -} - Pin_QX_Det::Pin_QX_Det(std::shared_ptr &log_ref) : m_pdetlog(log_ref) { - m_bInitialized = false; - m_bModelSucc = false; - m_bModel_Mark_Succ = false; - m_bshowimg = false; - m_strRootPath_Pin = "/home/aidlux/BOE/Pin/"; - creatsavedir(); std::string m_strSavePath; - - m_pImageStorage = ImageStorage::getInstance(); AI_Factory = AIFactory::GetInstance(); } Pin_QX_Det::~Pin_QX_Det() @@ -65,43 +41,9 @@ int Pin_QX_Det::GetPinMask(const cv::Mat &img, DetConfigResult *pDetConfig, Mat return re123; } - if (pDetConfig->pBaseCheckFunction->saveImg.bSaveAlginImg) - { - - } - if (pDetConfig->bSaveResultImg) { cv::imwrite(pDetConfig->strChannel +"_pin_" +"in.png", detImg); cv::imwrite(pDetConfig->strChannel +"_pin_" +"out_mask.png", mask); } -} - -int Pin_QX_Det::Detect(const cv::Mat &img, DetConfigResult *pDetConfig) -{ - if (!pDetConfig->pBaseCheckFunction->edgeDet.bOpen) - { - return 0; - } - - m_bshowimg = false; - if (pDetConfig->bSaveResultImg) - { - m_bshowimg = true; - } - - // 获取pin检测mask - Mat pin_mask; - int re = GetPinMask(img, pDetConfig, pin_mask); - - // 获取pin模板blob信息 - - - if (m_bshowimg) - { - cv::cvtColor(img, showimg, cv::COLOR_GRAY2BGR); - } - - - return 0; -} +} \ No newline at end of file