diff --git a/AlgorithmModule/include/ImgCheckAnalysisy.hpp b/AlgorithmModule/include/ImgCheckAnalysisy.hpp index b9acbe5..ed407d8 100644 --- a/AlgorithmModule/include/ImgCheckAnalysisy.hpp +++ b/AlgorithmModule/include/ImgCheckAnalysisy.hpp @@ -291,6 +291,7 @@ private: Rect m_old_productROI = Rect(0, 0, 0, 0); std::vector m_old_cur_edgeDet_region; std::vector m_old_cur_markLine_region; + std::vector m_old_cur_traditional_region; cv::Point m_old_cur_markLine_mark1; cv::Point m_old_cur_markLine_mark2; std::vector m_old_cur_regionConfigArr; diff --git a/AlgorithmModule/src/ImgCheckAnalysisy.cpp b/AlgorithmModule/src/ImgCheckAnalysisy.cpp index faa6221..6fb0b54 100644 --- a/AlgorithmModule/src/ImgCheckAnalysisy.cpp +++ b/AlgorithmModule/src/ImgCheckAnalysisy.cpp @@ -528,6 +528,7 @@ int ImgCheckAnalysisy::Adapt_Config(Mat img, Rect cur_roi, bool b_update){ m_old_cur_markLine_mark1 = m_AnalysisyConfig.baseFunction.markLine.mark_local_1; m_old_cur_markLine_mark2 = m_AnalysisyConfig.baseFunction.markLine.mark_local_2; m_old_cur_regionConfigArr = m_AnalysisyConfig.commonCheckConfig.nodeConfigArr[0].regionConfigArr; + m_old_cur_traditional_region = m_pbaseCheckFunction->traditionDet.detArea; } Rect old_roi = m_old_productROI; Point old_center(old_roi.x + old_roi.width / 2, old_roi.y + old_roi.height / 2); @@ -553,6 +554,8 @@ int ImgCheckAnalysisy::Adapt_Config(Mat img, Rect cur_roi, bool b_update){ cv::Point& cur_markLine_mark1 = m_AnalysisyConfig.baseFunction.markLine.mark_local_1; cv::Point& cur_markLine_mark2 = m_AnalysisyConfig.baseFunction.markLine.mark_local_2; std::vector& cur_regionConfigArr = m_AnalysisyConfig.commonCheckConfig.nodeConfigArr[0].regionConfigArr; + cv::Rect& cur_traditional_rect = m_pbaseCheckFunction->traditionDet.detArea_ROI; + std::vector& cur_traditional_region = m_pbaseCheckFunction->traditionDet.detArea; /*进行修改*/ // rect @@ -582,7 +585,11 @@ int ImgCheckAnalysisy::Adapt_Config(Mat img, Rect cur_roi, bool b_update){ cur_regionConfigArr[i].basicInfo.pointArry[j] = Point((cur_regionConfigArr[i].basicInfo.pointArry[j].x - new_center.x) * scale_x + new_center.x, (cur_regionConfigArr[i].basicInfo.pointArry[j].y - new_center.y) * scale_y + new_center.y); } } - + for(int i = 0; i < cur_traditional_region.size(); i++){ + cur_traditional_region[i].x = m_old_cur_traditional_region[i].x + x_offset; + cur_traditional_region[i].y = m_old_cur_traditional_region[i].y + y_offset; + cur_traditional_region[i] = Point((cur_traditional_region[i].x - new_center.x) * scale_x + new_center.x, (cur_traditional_region[i].y - new_center.y) * scale_y + new_center.y); + } /*show*/ // Mat show_img = img.clone(); // cv::rectangle(show_img, m_AnalysisyConfig.baseFunction.markLine.productROI, Scalar(255), 5); @@ -1631,6 +1638,14 @@ int ImgCheckAnalysisy::Traditional_Detect_Thread(const cv::Mat &img, cv::Mat &Re std::string strBaseLog = "Traditional_Detect"; m_pdetlog->AddCheckstr(PrintLevel_0, DET_LOG_LEVEL_3, strBaseLog, "Traditional_Detect Start"); + // 输入校验 + if (img.empty()) + { + m_pdetlog->AddCheckstr(PrintLevel_0, DET_LOG_LEVEL_3, strBaseLog, "Traditional_Detect FAILED (empty image)"); + ResultImg = cv::Mat(); + return -1; + } + Base_Function_TraditionDet traditionParam = m_pbaseCheckFunction->traditionDet; // 首次调用时初始化传统检测参数(从 m_AnalysisyConfig 映射) static bool bTcsInited = false; @@ -1652,7 +1667,10 @@ int ImgCheckAnalysisy::Traditional_Detect_Thread(const cv::Mat &img, cv::Mat &Re bTcsInited = true; } - cv::Rect detroi = traditionParam.detArea_ROI; + cv::Rect detroi = cv::boundingRect(traditionParam.detArea); + detroi.x -= m_Crop_Roi_paramImg.x; + detroi.y -= m_Crop_Roi_paramImg.y; + detroi = detroi & cv::Rect(0, 0, img.cols, img.rows); if(!traditionParam.bdetArea) { detroi = cv::Rect(0, 0, img.cols, img.rows); @@ -2254,7 +2272,7 @@ int ImgCheckAnalysisy::Edge_Qx_Det(const cv::Mat &img) 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; + int nmaxregionIdx = -1; for (int iregion = 0; iregion < m_DetRoiList.roiList_Src.size(); iregion++) { const std::vector &polygon = m_DetRoiList.roiList_Src[iregion]; @@ -2266,7 +2284,10 @@ int ImgCheckAnalysisy::Edge_Qx_Det(const cv::Mat &img) nmaxregionIdx = iregion; } - temerror.detRegionidxList.push_back(nmaxregionIdx); + if (nmaxregionIdx >= 0) + { + temerror.detRegionidxList.push_back(nmaxregionIdx); + } } // { @@ -2347,7 +2368,7 @@ int ImgCheckAnalysisy::BLobToDetResult() cv::Point pCenter; pCenter.x = roi.x + roi.width * 0.5; pCenter.y = roi.y + roi.height * 0.5; - int nmaxregionIdx = 0; + int nmaxregionIdx = -1; for (int iregion = 0; iregion < m_DetRoiList.roiList_Src.size(); iregion++) { const std::vector &polygon = m_DetRoiList.roiList_Src[iregion]; @@ -2359,7 +2380,10 @@ int ImgCheckAnalysisy::BLobToDetResult() nmaxregionIdx = iregion; } - temerror.detRegionidxList.push_back(nmaxregionIdx); + if (nmaxregionIdx >= 0) + { + temerror.detRegionidxList.push_back(nmaxregionIdx); + } } // { diff --git a/TcsCheckModule/src/TcsCheck.cpp b/TcsCheckModule/src/TcsCheck.cpp index 0947c6c..2874acb 100644 --- a/TcsCheckModule/src/TcsCheck.cpp +++ b/TcsCheckModule/src/TcsCheck.cpp @@ -318,14 +318,19 @@ cv::Mat CTcsCheck::DrawBlobInfoImage(const cv::Mat& imgCrop, const cv::Mat& imgB // ============================================================ int CTcsCheck::TraditionalDetect(const cv::Mat& img, cv::Rect detRoi, cv::Mat& blobImg) { - // if (img.empty()) return -1; + if (img.empty()) + { + blobImg = cv::Mat(); + return -1; + } m_matLoad = img; + // 确保 m_sizeImage 始终与 m_matLoad 同步 + m_sizeImage = img.size(); + cv::Rect rtCrop = detRoi; if(detRoi.size() == img.size()) { - m_sizeImage = img.size(); - // 1. 全局阈值 → 产品区域定位 cv::Mat matBinary; cv::threshold(m_matLoad, matBinary, m_cpCfg.nAreaLowFilter, 255, cv::THRESH_BINARY); @@ -341,6 +346,17 @@ int CTcsCheck::TraditionalDetect(const cv::Mat& img, cv::Rect detRoi, cv::Mat& b // 3. 裁剪边缘 rtCrop = GetCropArea(rtValid); } + + // 安全裁剪: 确保 rtCrop 不超出 m_matLoad 边界 + { + const cv::Rect imgRect(0, 0, m_matLoad.cols, m_matLoad.rows); + rtCrop = rtCrop & imgRect; + if (rtCrop.width <= 0 || rtCrop.height <= 0) + { + blobImg = cv::Mat(); + return -1; + } + } cv::Mat matCrop = m_matLoad(rtCrop).clone(); diff --git a/example/Read_Image.cpp b/example/Read_Image.cpp index 6d37a9a..87511d3 100644 --- a/example/Read_Image.cpp +++ b/example/Read_Image.cpp @@ -287,7 +287,7 @@ std::string Extract_ALL::Extract_Channel_Name(std::string strPath, int userflag) } int Extract_ALL::Read_Image_List(std::string strPath, std::string strproduct) { - std::string strSearchImgPath = strPath + "/*.png"; + std::string strSearchImgPath = strPath + "/*.jpg"; Read_Camera_Product_Image(strSearchImgPath, 0, strproduct); strSearchImgPath = strPath + "/*.ytimage"; @@ -298,12 +298,12 @@ int Extract_ALL::Read_Image_List(std::string strPath, std::string strproduct) int Extract_ALL::Read_Image_List(std::string strPath_left, std::string strPath_right, std::string strproduct) { - std::string strSearchImgPath = strPath_left + "/*.png"; + std::string strSearchImgPath = strPath_left + "/*.jpg"; Read_Camera_Product_Image(strSearchImgPath, 0, strproduct); - strSearchImgPath = strPath_right + "/*.png"; + strSearchImgPath = strPath_right + "/*.jpg"; Read_Camera_Product_Image(strSearchImgPath, 0, strproduct); - // strSearchImgPath = strPath + "/*.png"; + // strSearchImgPath = strPath + "/*.jpg"; // Read_Camera_Product_Image(strSearchImgPath, 1); return 0; diff --git a/example/deal.cpp b/example/deal.cpp index 7a0e407..d0c1b32 100644 --- a/example/deal.cpp +++ b/example/deal.cpp @@ -1191,7 +1191,7 @@ int deal::GetDetImageInfo(std::string strProductID, std::string strSearchImg, st std::string strs1 = strSearchImg + "/*.ytimage"; if(!runConfig.bdecode) { - strs1 = strSearchImg + "/*.png"; + strs1 = strSearchImg + "/*.jpg"; cv::glob(strs1, img_paths, true); } cv::glob(strs1, img_paths, true);