From 9c01db3682d24d8219029d1fbcc5adaef1ce8b4e Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期四, 10 七月 2025 17:25:53 +0800
Subject: [PATCH] 优化:修正 Notch 缺陷检测中 mask 膨胀次数与 offset 无限制问题,优化代码稳健性 - 限制 Offset 最大值为 50,防止因过大导致耗时过长或内存异常; - 限制 Dilate 膨胀次数不超过 50,避免异常耗时,超出时记录日志警告; - 修复内存释放遗漏,补充释放 pDilatedMaskImage,防止内存泄漏;

---
 EdgeInspector_App/Process/InspectCamera.cpp |   33 ++++++++++++++++++++++++++-------
 1 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/EdgeInspector_App/Process/InspectCamera.cpp b/EdgeInspector_App/Process/InspectCamera.cpp
index 67c6767..abc17f0 100644
--- a/EdgeInspector_App/Process/InspectCamera.cpp
+++ b/EdgeInspector_App/Process/InspectCamera.cpp
@@ -4331,9 +4331,13 @@
 	nOffset = m_nChamferOffset_um / m_pGlassData->GetSideData(emDim)->m_dPixelSizeX;
 	nOffset += pNotchParam->m_nNotch_Inspect_Defect_Offset;
 	m_nChamferOffset_um = 0;
-#else
-	nOffset = pNotchParam->m_nNotch_Inspect_Defect_Offset;
 #endif // MINI_LED
+
+	// 防止过大导致耗时或内存问题
+	if (nOffset > 50) {
+		g_pLog->DisplayMessage(_T("[WARN] Notch defect offset capped to 50 pixels, original: %d"), nOffset);
+		nOffset = 50;
+	}
 
 	CRect rcIns = rtROI;
 	rcIns.OffsetRect(-rcIns.left,-rcIns.top);
@@ -4389,8 +4393,9 @@
 	strTemp.Format(_T("Notch\\Notch_%d_Defect_MaskPreImage"), nNotchIdx);
 	SaveDebugImage(emDim, stFrame, pMaskImage, strTemp);
 
-	if(0 < nOffset)
+	if (0 < nOffset) {
 		cvDilate(pMaskImage, pMaskImage, 0, nOffset);
+	}
 
 	strTemp.Format(_T("Notch\\Notch_%d_Defect_MaskDilateImage"), nNotchIdx);
 	SaveDebugImage(emDim, stFrame, pMaskImage, strTemp);
@@ -4408,13 +4413,26 @@
 	strTemp.Format(_T("Notch\\Notch_%d_Defect_ProcBinImage"), nNotchIdx);
 	SaveDebugImage(emDim, stFrame, pProcBinImage, strTemp);
 
-#if	MINI_LED == 0	
-	cvDilate(pMaskImage, pDilatedMaskImage, 0, pNotchParam->m_nNotch_Inspect_Defect_dilate);
-	strTemp.Format(_T("Notch\\Notch_%d_Defect_pMaskImage2"), nNotchIdx);
+#if	MINI_LED == 0
+	// 限制膨胀次数,防止过大导致耗时或内存问题
+	int nDilateCount = pNotchParam->m_nNotch_Inspect_Defect_dilate;
+	nDilateCount = std::max(0, std::min(nDilateCount, 50));
+	if (pNotchParam->m_nNotch_Inspect_Defect_dilate != nDilateCount) {
+		g_pLog->DisplayMessage(_T("[WARN] Dilate count capped: Original=%d, Applied=%d"), pNotchParam->m_nNotch_Inspect_Defect_dilate, nDilateCount);
+	}
+
+	if (nDilateCount > 0) {
+		cvDilate(pMaskImage, pDilatedMaskImage, 0, nDilateCount);
+	}
+	else {
+		cvCopy(pMaskImage, pDilatedMaskImage);
+	}
+
+	strTemp.Format(_T("Notch\\Notch_%d_Defect_Mask_Dilated"), nNotchIdx);
 	SaveDebugImage(emDim, stFrame, pDilatedMaskImage, strTemp);
 
 	cvAnd(pDilatedMaskImage, pProcBinImage, pDilatedMaskImage);
-	strTemp.Format(_T("Notch\\Notch_%d_Defect_pMaskImage3"), nNotchIdx);
+	strTemp.Format(_T("Notch\\Notch_%d_Defect_Mask_And_Bin"), nNotchIdx);
 	SaveDebugImage(emDim, stFrame, pDilatedMaskImage, strTemp);
 #endif	
 
@@ -4441,6 +4459,7 @@
 	
 	m_pDefectControl->ExtractDefect_Notch(emDim, m_iCamera, stFrame.nScanIdx, nNotchIdx, &vecDefectCandidateList);
 
+	cvReleaseImage(&pDilatedMaskImage);
 	cvReleaseImage(&pMaskImage);
 	cvReleaseImage(&pProcImage);
 	cvReleaseImage(&pProcBinImage);

--
Gitblit v1.9.3