diff --git a/AlgorithmModule/include/CheckUtil.hpp b/AlgorithmModule/include/CheckUtil.hpp index 27f693c..3df5b2d 100644 --- a/AlgorithmModule/include/CheckUtil.hpp +++ b/AlgorithmModule/include/CheckUtil.hpp @@ -49,6 +49,8 @@ public: static float CalImgBrightness(cv::Mat imgRoi); // 计算角度 static float Cal2PointAngle(cv::Point p_left, cv::Point p_right); + // 获取 RotatedRect 长轴的方向角(归一化到 [0, 180) 度) + static float getLongAxisAngle(const cv::RotatedRect rect); // 找图片的最大外轮廓 static std::vector getLargestContourROI(const cv::Mat &binaryImg, bool &found); diff --git a/AlgorithmModule/src/CheckUtil.cpp b/AlgorithmModule/src/CheckUtil.cpp index e937248..1945c10 100644 --- a/AlgorithmModule/src/CheckUtil.cpp +++ b/AlgorithmModule/src/CheckUtil.cpp @@ -402,6 +402,23 @@ float CheckUtil::Cal2PointAngle(cv::Point p_left, cv::Point p_right) return angle * 180 / 3.1415926; } +float CheckUtil::getLongAxisAngle(cv::RotatedRect rect) { + // 获取宽度边对应的角度 + float angle = rect.angle; + + // 判断长轴:若 width >= height,长轴是 width 边;否则是 height 边(垂直于 width) + float long_axis_angle = (rect.size.width >= rect.size.height) ? + angle : + angle + 90.0f; + + // 将角度归一化到 [0, 180) 范围(因为直线方向没有正反) + long_axis_angle = std::fmod(long_axis_angle, 180.0f); + if (long_axis_angle < 0) { + long_axis_angle += 180.0f; + } + return long_axis_angle; +} + std::vector CheckUtil::getLargestContourROI(const cv::Mat &binaryImg, bool &found) { std::vector> contours; diff --git a/AlgorithmModule/src/ImgCheckAnalysisy.cpp b/AlgorithmModule/src/ImgCheckAnalysisy.cpp index 578c466..43a62d2 100644 --- a/AlgorithmModule/src/ImgCheckAnalysisy.cpp +++ b/AlgorithmModule/src/ImgCheckAnalysisy.cpp @@ -633,7 +633,7 @@ int ImgCheckAnalysisy::CheckRun() m_CheckResult_shareP->basicResult.strChannel = m_CheckResult_shareP->in_shareImage->strChannel; m_strCurDetChannel = m_CheckResult_shareP->basicResult.strChannel; - // 2、参数检查 + /*参数检查*/ int rec = ConfigCheck(DetImgInfo_shareP->img); if (rec != CHECK_OK) { @@ -643,9 +643,11 @@ int ImgCheckAnalysisy::CheckRun() return m_nErrorCode; } long time_edge_s = CheckUtil::getcurTime(); - // 3、AI 边缘定位(内外边缘) + + /*AI 边缘定位(内外边缘)*/ cv::RotatedRect outer_roi; 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) @@ -658,14 +660,23 @@ int ImgCheckAnalysisy::CheckRun() long time_edge_e = CheckUtil::getcurTime(); m_pdetlog->AddCheckstr(PrintLevel_0, "2、pre detect", "-------------------------AI_Edge------------succ---time %ld----\n", time_edge_e - time_edge_s); + // 计算内外边缘夹角 + float aouter = CheckUtil::getLongAxisAngle(outer_roi); + float ainner = CheckUtil::getLongAxisAngle(inner_roi); + + float diff_align = std::fabs(aouter - ainner); + diff_align = std::fmod(diff_align, 180.0f); // 模 180 度 + /*投影对齐模板*/ Adapt_Config(m_CheckResult_shareP->in_shareImage->img, m_outer_roi, m_pbaseCheckFunction->markLine.badapt_region); m_Crop_Roi_paramImg = m_outer_roi; m_pImageAllResult->pDetResult->CutRoi = m_outer_roi; m_pImageAllResult->pDetResult->Param_CropRoi = m_Crop_Roi_paramImg; + long time_calan_e = CheckUtil::getcurTime(); + m_pdetlog->AddCheckstr(PrintLevel_0, "2.1、pre detect", "-------------------------CalAn------------succ---time %ld----\n", time_calan_e - time_edge_s); - // 生成 检测的图片 + /*生成 检测的图片*/ cv::Mat image; image = m_CheckResult_shareP->in_shareImage->img; if (image.channels() == 3) @@ -676,19 +687,19 @@ int ImgCheckAnalysisy::CheckRun() { m_pImageAllResult->detImg = image(m_outer_roi).clone(); } - // 多线程开启 AI 推理检测 + /*多线程开启 AI 推理检测*/ long time_AI_s = CheckUtil::getcurTime(); m_AItask = std::make_shared(); m_AItask->taskname = Task_AI; m_task.sendTask(m_AItask); ImgPreDet(); - // 更新检测区域 + /*更新检测区域*/ Update_DetRoiList(); m_pdetlog->AddCheckstr(PrintLevel_0, "3、pre detect", "-------------------------pre Det--------------- \n"); - // 实现PIN脚检测 + /*实现PIN脚检测*/ { long t41 = CheckUtil::getcurTime(); int reedge1111 = Edge_Qx_Det(m_pImageAllResult->detImg); @@ -698,10 +709,10 @@ int ImgCheckAnalysisy::CheckRun() m_CheckResult_shareP->nresult = 0; - // 把临时可以绘制的结果都绘制出来。 + /*把临时可以绘制的结果都绘制出来。*/ DrawResult_Step_1(); long time_AI_e; - // AI 推理生成 + /*AI 推理生成*/ { long t211 = CheckUtil::getcurTime(); // 对AI的结果进行 处理,等待 AI 推理全部完成。