From aef42eef51d1b86ac7217a88ce17c5156c30fe6d Mon Sep 17 00:00:00 2001
From: LWQ <1806950222@qq.com>
Date: 星期一, 14 七月 2025 11:42:53 +0800
Subject: [PATCH] Merge branch 'master' into LWQ

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

diff --git a/EdgeInspector_App/Process/InspectCamera.cpp b/EdgeInspector_App/Process/InspectCamera.cpp
index 795ba91..909e2f7 100644
--- a/EdgeInspector_App/Process/InspectCamera.cpp
+++ b/EdgeInspector_App/Process/InspectCamera.cpp
@@ -4214,11 +4214,14 @@
 			double dChamfer_Diff_um = dChamfer_std_um - dChamfer_um;
 
 			pSideData->m_nNotch_Chamfer_Edge_Judge[nNotchIdx][nMeasureIdx] = 1;
+
+#if MINI_LED
 			//选取测量研磨尺寸最大值
-			if (m_nChamferOffset_um < dChamfer_um)
-			{
+			if (m_nChamferOffset_um < dChamfer_um) {
 				m_nChamferOffset_um = dChamfer_um;
 			}
+#endif
+
 			pSideData->m_dNotch_Chamfer_Edge_Result_Diff_um[nNotchIdx][nMeasureIdx] = dChamfer_std_um - dChamfer_um;
 			pSideData->m_dNotch_Chamfer_Edge_Judge_Std_um[nNotchIdx][nMeasureIdx] = dChamfer_std_um;
 			pSideData->m_dNotch_Chamfer_Edge_Judge_Min_um[nNotchIdx][nMeasureIdx] = dChamfer_min_um;
@@ -4325,10 +4328,19 @@
 		vecDefectCandidateList.push_back(defectInfo);
 	}
 	m_pDefectControl->ExtractDefect_Notch(emDim, m_iCamera, stFrame.nScanIdx, nNotchIdx, &vecDefectCandidateList);
-#else	
+#else
+
+#if MINI_LED
 	nOffset = m_nChamferOffset_um / m_pGlassData->GetSideData(emDim)->m_dPixelSizeX;
 	nOffset += pNotchParam->m_nNotch_Inspect_Defect_Offset;
 	m_nChamferOffset_um = 0;
+#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);
@@ -4337,9 +4349,8 @@
 	IplImage* pProcessCanny = cvCreateImage(cvSize(rcIns.Width(), rcIns.Height()), 8, 1);
 	IplImage* pProcessBin = cvCreateImage(cvSize(rcIns.Width(), rcIns.Height()), 8, 1);
 	IplImage* pMaskImage = cvCreateImage(cvSize(rcIns.Width(), rcIns.Height()), 8, 1);
-	IplImage* pMaskImage1 = cvCreateImage(cvSize(rcIns.Width(), rcIns.Height()), 8, 1);
-	IplImage* pdstImage = cvCreateImage(cvSize(rcIns.Width(), rcIns.Height()), 8, 1);//绘画轮廓
-	IplImage* ptempImage1 = cvCreateImage(cvSize(rcIns.Width(), rcIns.Height()), 8, 1);//凸包
+	IplImage* pDilatedMaskImage = cvCreateImage(cvSize(rcIns.Width(), rcIns.Height()), 8, 1);
+
 	for(int i=0; i<rcIns.Height(); i++)
 	{
 		memcpy(pProcImage->imageData+(i*pProcImage->widthStep), scr->imageData+(rtROI.left + (i*scr->widthStep)), rcIns.Width());
@@ -4359,8 +4370,8 @@
 	strTemp.Format(_T("Notch\\Notch_%d_Defect_EdgeOrBin"), nNotchIdx);
 	SaveDebugImage(emDim, stFrame, pMaskImage, strTemp);
 	
-	BOOL		bFind;
-	int			nFindX;
+	BOOL bFind;
+	int	 nFindX;
 	for(int y=0;y<pMaskImage->height;y++)
 	{
 		bFind = FALSE;
@@ -4379,19 +4390,15 @@
 		{
 			memset(&pMaskImage->imageData[y*pMaskImage->widthStep], 255, nFindX);
 			memset(&pMaskImage->imageData[y*pMaskImage->widthStep+nFindX], 0, pMaskImage->width - nFindX);
-
-			//for (int x = 0; x < nFindX; x++)
-			//	pMaskImage->imageData[y * pMaskImage->widthStep + x] = 255;
-			//for (int x = nFindX; x < pMaskImage->width; x++)
-			//	pMaskImage->imageData[y * pMaskImage->widthStep + x] = 0;
 		}
 	}
 
 	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,15 +4415,30 @@
 
 	strTemp.Format(_T("Notch\\Notch_%d_Defect_ProcBinImage"), nNotchIdx);
 	SaveDebugImage(emDim, stFrame, pProcBinImage, strTemp);
-	#if	MINI_LED == TRUE	
-	cvDilate(pMaskImage, pMaskImage1, 0, pNotchParam->m_nNotch_Inspect_Defect_dilate);
-	strTemp.Format(_T("Notch\\Notch_%d_Defect_pMaskImage2"), nNotchIdx);
-	SaveDebugImage(emDim, stFrame, pMaskImage1, strTemp);
 
-		cvAnd(pMaskImage1, pProcBinImage, pMaskImage1);
-		strTemp.Format(_T("Notch\\Notch_%d_Defect_pMaskImage3"), nNotchIdx);
-		SaveDebugImage(emDim, stFrame, pMaskImage1, strTemp);
-    #endif	
+#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_Mask_And_Bin"), nNotchIdx);
+	SaveDebugImage(emDim, stFrame, pDilatedMaskImage, strTemp);
+#endif	
+
 	// 3. Blob
 	int iScan = stFrame.nScanIdx;
 	int iFrame = stFrame.nFrameIdx;
@@ -4432,14 +4454,15 @@
 	DefectLocation defectLoc = DefectLoc_Notch_Crack;
 
 	std::vector<CDefect_Info> vecDefectCandidateList;
-	if (MINI_LED == TRUE)
-		DefectProcess(emDim, iScan, iFrame, pProcImage, pMaskImage1, rtProcessArea, nMinSize, nMinSize_X, nMinSize_Y, nDilation, bStartNearFilterUse, nStartNearFilterRange, defectLoc, &vecDefectCandidateList);
-	else
+#if	MINI_LED == 0	
+		DefectProcess(emDim, iScan, iFrame, pProcImage, pDilatedMaskImage, rtProcessArea, nMinSize, nMinSize_X, nMinSize_Y, nDilation, bStartNearFilterUse, nStartNearFilterRange, defectLoc, &vecDefectCandidateList);
+#else
 		DefectProcess(emDim, iScan, iFrame, pProcImage, pProcBinImage, rtProcessArea, nMinSize, nMinSize_X, nMinSize_Y, nDilation, bStartNearFilterUse, nStartNearFilterRange, defectLoc, &vecDefectCandidateList);
-
+#endif	
 	
 	m_pDefectControl->ExtractDefect_Notch(emDim, m_iCamera, stFrame.nScanIdx, nNotchIdx, &vecDefectCandidateList);
 
+	cvReleaseImage(&pDilatedMaskImage);
 	cvReleaseImage(&pMaskImage);
 	cvReleaseImage(&pProcImage);
 	cvReleaseImage(&pProcBinImage);

--
Gitblit v1.9.3