From 8c7705d8e69c358dcbe77354fcc02b76156e8164 Mon Sep 17 00:00:00 2001
From: LWQ <1806950222@qq.com>
Date: 星期一, 14 七月 2025 11:40:58 +0800
Subject: [PATCH] 释放图像内存

---
 EdgeInspector_App/Process/InspectCamera.cpp |  326 ++++++++++++++++++++++++-----------------------------
 1 files changed, 147 insertions(+), 179 deletions(-)

diff --git a/EdgeInspector_App/Process/InspectCamera.cpp b/EdgeInspector_App/Process/InspectCamera.cpp
index 7c6fa50..a091371 100644
--- a/EdgeInspector_App/Process/InspectCamera.cpp
+++ b/EdgeInspector_App/Process/InspectCamera.cpp
@@ -3720,13 +3720,6 @@
 
 BOOL CInspectCamera::Notch_Process(int iThread,DimensionDir emDim, stFrameIndex stFrame)
 {
-	//CTime tCurrentTime = CTime::GetCurrentTime();
-	//CTime tLimitTime = CTime(2023,3,31,11,59,59);
-	//CTimeSpan tSpanTime = tLimitTime - tCurrentTime;
-
-	//if(tSpanTime.GetTotalSeconds() < 0)
-	//	return FALSE;
-
 	if(m_pGlassData == NULL)
 		return FALSE;
 
@@ -3782,24 +3775,20 @@
 		pEdgeImage = cvCloneImage(pOriginImage);
 		pBinImage = cvCloneImage(pOriginImage);
 
-		g_pLog->DisplayMessage(_T("1. Notch_Process %s : %d, idx %d"),g_SideName[(int) emDim], stFrame.nFrameIdx,i);
-
 		// 1. Image PreProcess..
+		g_pLog->DisplayMessage(_T("1. Notch_Process %s : %d, idx %d"), g_SideName[(int)emDim], stFrame.nFrameIdx, i);
 		Notch_Process_PreProcess(emDim, stFrame, i, pOriginImage, pEdgeImage, pBinImage, rtRotateArea);
-		// Notch_Process_PreProcess_2(emDim, stFrame, i, pOriginImage, pEdgeImage, pBinImage, rtRotateArea);
-
-		g_pLog->DisplayMessage(_T("2. Notch_Process %s : %d, idx %d"),g_SideName[(int) emDim], stFrame.nFrameIdx,i);
 
 		// 2. Notch Dimension/Chamfer Find..
+		g_pLog->DisplayMessage(_T("2. Notch_Process %s : %d, idx %d"), g_SideName[(int)emDim], stFrame.nFrameIdx, i);
 		Notch_Process_Measure(emDim, stFrame, i, pOriginImage, pEdgeImage, pBinImage, rtRotateArea);
 
-		g_pLog->DisplayMessage(_T("3. Notch_Process %s : %d, idx %d"),g_SideName[(int) emDim], stFrame.nFrameIdx,i);
-
 		// 3. Notch Dimension/Chamfer Calculate Result..
+		g_pLog->DisplayMessage(_T("3. Notch_Process %s : %d, idx %d"), g_SideName[(int)emDim], stFrame.nFrameIdx, i);
 		bool bResult = Notch_Process_Calculate(emDim, stFrame, i, pOriginImage, pEdgeImage, pBinImage, rtRotateArea);
 
-		g_pLog->DisplayMessage(_T("4. Notch_Process %s : %d, idx %d"),g_SideName[(int) emDim], stFrame.nFrameIdx,i);
 		// 4. Notch Dimension/Chamfer Judge
+		g_pLog->DisplayMessage(_T("4. Notch_Process %s : %d, idx %d"), g_SideName[(int)emDim], stFrame.nFrameIdx, i);
 		Notch_Process_Judge(emDim, stFrame, i, pOriginImage, pEdgeImage, pBinImage, rtRotateArea);
 
 		if (!bResult)
@@ -3822,21 +3811,19 @@
 
 			pSideData->m_bNotchInspection_Complete = TRUE;
 
-			
-
 			return FALSE;
 		}
 
-		g_pLog->DisplayMessage(_T("5. Notch_Process %s : %d, idx %d"),g_SideName[(int) emDim], stFrame.nFrameIdx,i);
 		// 5. Notch Defect Inspect (Chip/Crack)
+		g_pLog->DisplayMessage(_T("5. Notch_Process %s : %d, idx %d"), g_SideName[(int)emDim], stFrame.nFrameIdx, i);
 		Notch_Process_Defect(emDim, stFrame, i, pOriginImage, pEdgeImage, pBinImage, rtRotateArea);
 
-		g_pLog->DisplayMessage(_T("6. Notch_Process %s : %d, idx %d"),g_SideName[(int) emDim], stFrame.nFrameIdx,i);
 		// 6. Notch Find Circle
+		g_pLog->DisplayMessage(_T("6. Notch_Process %s : %d, idx %d"), g_SideName[(int)emDim], stFrame.nFrameIdx, i);
 		Notch_Process_FindCircle(emDim, stFrame, i);
 
+		// 7. Notch Process End
 		g_pLog->DisplayMessage(_T("7. Notch_Process %s : %d, idx %d"),g_SideName[(int) emDim], stFrame.nFrameIdx,i);
-
 		cvReleaseImageHeader(&pOriginImage);
 		cvReleaseImage(&pEdgeImage);
 		cvReleaseImage(&pBinImage);
@@ -3844,79 +3831,47 @@
 
 	pSideData->m_bNotchInspection_Complete = TRUE;
 
-	g_pLog->DisplayMessage(_T("7. Notch_Process End %s : %d"),g_SideName[(int) emDim], stFrame.nFrameIdx);
-
 	return TRUE;
 }
 
 void CInspectCamera::Notch_Process_PreProcess(DimensionDir emDim, stFrameIndex stFrame, int nNotchIdx, IplImage* scr, IplImage* img_Edge, IplImage* img_Bin, CRect rtROI)
 {
-	if(m_pRecipe == NULL)
+	if (m_pRecipe == nullptr) {
 		return;
+	}
 
-	if(nNotchIdx < 0 || MAX_SIDE_NOTCH_COUNT <= nNotchIdx)
+	if (nNotchIdx < 0 || nNotchIdx >= MAX_SIDE_NOTCH_COUNT) {
 		return;
+	}
 
-	CString strTemp;
+	// 中值滤波器大小,用于去噪,必须为奇数(若偶数会自动减1)
+	const int nSmoothFilter = m_pRecipe->m_SideParam[(int)emDim].m_NotchPrm[nNotchIdx].m_nSmooth_Filter;
 
-	int sigma1 = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nSmooth_Filter;
-	int sigma2 = 7;
+	// 磨边区域阈值,控制反二值化阈值,主要用于提取 Notch 边缘特征
+	const int nGrindThreshold = m_pRecipe->m_SideParam[(int)emDim].m_NotchPrm[nNotchIdx].m_nGrind_Threshold;
 
-	if(sigma1 % 2 == 0)
-		sigma1 = sigma1 - 1;
+	// 玻璃区域阈值,控制正二值化阈值,用于提取玻璃主体区域
+	const int nGlassThreshold = m_pRecipe->m_SideParam[(int)emDim].m_NotchPrm[nNotchIdx].m_nGlass_Threshold;
 
-	if(1 < sigma1)
-		cvSmooth(scr,img_Bin,CV_MEDIAN,sigma1);
+	// 确保奇数滤波器大小
+	int nFilterSize = (nSmoothFilter % 2 == 0) ? (nSmoothFilter - 1) : nSmoothFilter;
 
-// 	double th1 = (double) m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nGrind_Threshold * 10;
-// 	double th2 = 40.0;
-// 	int sigma=5;
-// 
-// 	cvCanny(img_Bin,img_Edge,th1,th2,sigma);
+	if (nFilterSize > 1) {
+		// 中值滤波降噪
+		cvSmooth(scr, img_Bin, CV_MEDIAN, nFilterSize);
+	}
 
-	double th1 = (double) m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nGrind_Threshold;
+	// Edge图:反二值化阈值处理
+	cvThreshold(scr, img_Edge, nGrindThreshold, 255, CV_THRESH_BINARY_INV);
+	CString strEdgeFileName;
+	strEdgeFileName.Format(_T("Notch\\Notch_%d_Edge.jpg"), nNotchIdx);
+	SaveDebugImage(emDim, stFrame, img_Edge, strEdgeFileName);
 
-	cvThreshold(scr,img_Edge,th1,255,CV_THRESH_BINARY_INV);
-	                                               
-	strTemp.Format(_T("Notch\\Notch_%d_Edge.jpg"), nNotchIdx);    
-	SaveDebugImage(emDim, stFrame, img_Edge, strTemp);
-
-	th1 = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nGlass_Threshold;
-
-	cvThreshold(scr,img_Bin,th1,255,CV_THRESH_BINARY);
-
-	strTemp.Format(_T("Notch\\Notch_%d_Bin.jpg"), nNotchIdx);
-	SaveDebugImage(emDim, stFrame, img_Bin, strTemp);
-}
-
-void CInspectCamera::Notch_Process_PreProcess_2(DimensionDir emDim, stFrameIndex stFrame, int nNotchIdx, IplImage* scr, IplImage* img_Edge, IplImage* img_Bin, CRect rtROI)
-{
-	if(m_pRecipe == NULL)
-		return;
-
-	if(nNotchIdx < 0 || MAX_SIDE_NOTCH_COUNT <= nNotchIdx)
-		return;
-
-	int sigma1 = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nSmooth_Filter;
-
-	CString strTemp;
-	cvThreshold(scr,img_Edge,sigma1,255,CV_THRESH_BINARY_INV);
-
-	double th1 = (double) m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nGrind_Threshold;
-	double th2 = 40.0;
-	int sigma=5;
-
-	cvCanny(img_Edge, img_Edge,th1,th2,sigma);
-
-	strTemp.Format(_T("Notch\\Notch_%d_Edge.jpg"), nNotchIdx);
-	SaveDebugImage(emDim, stFrame, img_Edge, strTemp);
-
-	th1 = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nGlass_Threshold;
-
-	cvThreshold(scr,img_Bin,th1,255,CV_THRESH_BINARY);
-
-	strTemp.Format(_T("Notch\\Notch_%d_Bin.jpg"), nNotchIdx);
-	SaveDebugImage(emDim, stFrame, img_Bin, strTemp);
+	// Bin图:正常二值化
+	cvThreshold(scr, img_Bin, nGlassThreshold, 255, CV_THRESH_BINARY);
+	CString strBinFileName;
+	strBinFileName.Format(_T("Notch\\Notch_%d_Bin.jpg"), nNotchIdx);
+	SaveDebugImage(emDim, stFrame, img_Bin, strBinFileName);
 }
 
 void CInspectCamera::Notch_Process_Measure(DimensionDir emDim, stFrameIndex stFrame, int nNotchIdx, IplImage* scr,IplImage* img_Edge,IplImage* img_Bin, CRect rtROI)
@@ -4088,7 +4043,6 @@
 						break;
 			}
 
-
 			if(bEdgeCheck == FALSE && (bEdgeFind == TRUE || bBinEdgeFind == TRUE))
 			{
 				if(bFindEdge == FALSE && bEdgeFind == TRUE)
@@ -4154,24 +4108,37 @@
 	{
 		CPoint ptRef_pxl = m_pGlassData->GetSideData(emDim)->m_ptNotch_Reference[nNotchIdx][nMeasureIdx];
 		CPoint ptDim_pxl = m_pGlassData->GetSideData(emDim)->m_ptNotch_Dimension_Edge[nNotchIdx][nMeasureIdx];
-		CPoint ptChamfer_pxl = m_pGlassData->GetSideData(emDim)->m_ptNotch_Chamfer_Edge[nNotchIdx][nMeasureIdx];
+
+		if (ptRef_pxl.x < 0 || ptRef_pxl.y < 0) {
+			g_pLog->DisplayMessage(_T("[ERROR] Invalid Ref Point at Notch_Process_Calculate, NotchIdx: %d, MeasureIdx: %d, Point: (%d, %d)"), nNotchIdx, nMeasureIdx, ptRef_pxl.x, ptRef_pxl.y);
+			continue;
+		}
+		
+		if (ptDim_pxl.x < 0 || ptDim_pxl.y < 0) {
+			g_pLog->DisplayMessage(_T("[ERROR] Invalid Dim Point at Notch_Process_Calculate, NotchIdx: %d, MeasureIdx: %d, Point: (%d, %d)"), nNotchIdx, nMeasureIdx, ptDim_pxl.x, ptDim_pxl.y);
+			continue;
+		}
 
 		// Dimension
 		double dX_dim_um = (double)(ptRef_pxl.x - ptDim_pxl.x) * m_dPixelSizeX;
 		double dY_dim_um = (double)(ptRef_pxl.y - ptDim_pxl.y) * m_dPixelSizeY;
 		double dDimension = sqrtf((dX_dim_um * dX_dim_um) + (dY_dim_um * dY_dim_um));
-
 		m_pGlassData->GetSideData(emDim)->m_dNotch_Dimension_Edge_Result_um[nNotchIdx][nMeasureIdx] = dDimension;
+
+		CPoint ptChamfer_pxl = m_pGlassData->GetSideData(emDim)->m_ptNotch_Chamfer_Edge[nNotchIdx][nMeasureIdx];
+		if (ptChamfer_pxl.x < 0 || ptChamfer_pxl.y < 0) {
+			g_pLog->DisplayMessage(_T("[ERROR] Invalid Chamfer Point at Notch_Process_Calculate, NotchIdx: %d, MeasureIdx: %d, Point: (%d, %d)"), nNotchIdx, nMeasureIdx, ptChamfer_pxl.x, ptChamfer_pxl.y);
+			continue;
+		}
 
 		double dX_Chamfer = (double)(ptChamfer_pxl.x - ptDim_pxl.x) * m_dPixelSizeX;
 		double dY_Chamfer = (double)(ptChamfer_pxl.y - ptDim_pxl.y) * m_dPixelSizeY;
 		double dChamfer = sqrtf((dX_Chamfer * dX_Chamfer) + (dY_Chamfer * dY_Chamfer));
-
 		m_pGlassData->GetSideData(emDim)->m_dNotch_Chamfer_Edge_Result_um[nNotchIdx][nMeasureIdx] = dChamfer;
 
-		if (dDimension > 2000 && dChamfer > 2000)
-		{
-			return FALSE;
+		if (dDimension > 2000.0 && dChamfer > 2000.0) {
+			g_pLog->DisplayMessage(_T("[ERROR] Abnormal Dimension & Chamfer too large at Notch_Process_Calculate, NotchIdx: %d, MeasureIdx: %d, Dimension: %.2f, Chamfer: %.2f"), nNotchIdx, nMeasureIdx, dDimension, dChamfer);
+			continue;
 		}
 	}
 
@@ -4180,38 +4147,46 @@
 
 void CInspectCamera::Notch_Process_Judge(DimensionDir emDim, stFrameIndex stFrame, int nNotchIdx, IplImage* scr,IplImage* img_Edge,IplImage* img_Bin, CRect rtROI)
 {
-	if(m_pRecipe == NULL || m_pGlassData == NULL)
+	if (m_pRecipe == NULL || m_pGlassData == NULL) {
+		g_pLog->DisplayMessage(_T("[ERROR] Recipe or Glass Data is NULL in Notch_Process_Judge"));
 		return;
+	}
+
+	CSide_Data* pSideData = m_pGlassData->GetSideData(emDim);
+	if (pSideData == NULL) {
+		g_pLog->DisplayMessage(_T("[ERROR] Side Data is NULL in Notch_Process_Judge, Dimension: %d"), (int)emDim);
+		return;
+	}
 
 	for(int nMeasureIdx=0; nMeasureIdx < MAX_SIDE_NOTCH_MEASURE_COUNT; nMeasureIdx++)
 	{
 		// Dimension
 		if(m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_bNotch_Dimension_Use[nMeasureIdx] == TRUE)
 		{
-			double dDimension_um = m_pGlassData->GetSideData(emDim)->m_dNotch_Dimension_Edge_Result_um[nNotchIdx][nMeasureIdx];
+			double dDimension_um = pSideData->m_dNotch_Dimension_Edge_Result_um[nNotchIdx][nMeasureIdx];
 			double dDimension_std_um = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_dNotch_Dimension_STD_mm[nMeasureIdx] * 1000.0;
 			double dDimension_min_um = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_dNotch_Dimension_Diff_MIN_mm[nMeasureIdx] * 1000.0;
 			double dDimension_max_um = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_dNotch_Dimension_Diff_MAX_mm[nMeasureIdx] * 1000.0;
 			double dDimension_Diff_um = dDimension_std_um - dDimension_um;
 
-			m_pGlassData->GetSideData(emDim)->m_nNotch_Dimension_Edge_Judge[nNotchIdx][nMeasureIdx] = 1;
-			m_pGlassData->GetSideData(emDim)->m_dNotch_Dimension_Edge_Result_Diff_um[nNotchIdx][nMeasureIdx] = dDimension_Diff_um;
-			m_pGlassData->GetSideData(emDim)->m_dNotch_Dimension_Edge_Judge_Std_um[nNotchIdx][nMeasureIdx] = dDimension_std_um;
-			m_pGlassData->GetSideData(emDim)->m_dNotch_Dimension_Edge_Judge_Min_um[nNotchIdx][nMeasureIdx] = dDimension_min_um;
-			m_pGlassData->GetSideData(emDim)->m_dNotch_Dimension_Edge_Judge_Max_um[nNotchIdx][nMeasureIdx] = dDimension_max_um;
+			pSideData->m_nNotch_Dimension_Edge_Judge[nNotchIdx][nMeasureIdx] = 1;
+			pSideData->m_dNotch_Dimension_Edge_Result_Diff_um[nNotchIdx][nMeasureIdx] = dDimension_Diff_um;
+			pSideData->m_dNotch_Dimension_Edge_Judge_Std_um[nNotchIdx][nMeasureIdx] = dDimension_std_um;
+			pSideData->m_dNotch_Dimension_Edge_Judge_Min_um[nNotchIdx][nMeasureIdx] = dDimension_min_um;
+			pSideData->m_dNotch_Dimension_Edge_Judge_Max_um[nNotchIdx][nMeasureIdx] = dDimension_max_um;
 
 			if(dDimension_um > dDimension_std_um + dDimension_max_um)
-				m_pGlassData->GetSideData(emDim)->m_nNotch_Dimension_Edge_Judge[nNotchIdx][nMeasureIdx] = 2;
+				pSideData->m_nNotch_Dimension_Edge_Judge[nNotchIdx][nMeasureIdx] = 2;
 			else if(dDimension_um < dDimension_std_um - dDimension_min_um)
-				m_pGlassData->GetSideData(emDim)->m_nNotch_Dimension_Edge_Judge[nNotchIdx][nMeasureIdx] = 3;
+				pSideData->m_nNotch_Dimension_Edge_Judge[nNotchIdx][nMeasureIdx] = 3;
 
-			if(m_pGlassData->GetSideData(emDim)->m_nNotch_Dimension_Edge_Judge[nNotchIdx][nMeasureIdx] != 1)
+			if(pSideData->m_nNotch_Dimension_Edge_Judge[nNotchIdx][nMeasureIdx] != 1)
 			{
 				// Make 搬窃..
-				CRect rtDefectArea = CRect(m_pGlassData->GetSideData(emDim)->m_ptNotch_Dimension_Edge[nNotchIdx][nMeasureIdx].x, 
-												m_pGlassData->GetSideData(emDim)->m_ptNotch_Dimension_Edge[nNotchIdx][nMeasureIdx].y, 
-													m_pGlassData->GetSideData(emDim)->m_ptNotch_Dimension_Edge[nNotchIdx][nMeasureIdx].x, 
-														m_pGlassData->GetSideData(emDim)->m_ptNotch_Dimension_Edge[nNotchIdx][nMeasureIdx].y);
+				CRect rtDefectArea = CRect(pSideData->m_ptNotch_Dimension_Edge[nNotchIdx][nMeasureIdx].x,
+											pSideData->m_ptNotch_Dimension_Edge[nNotchIdx][nMeasureIdx].y,
+											pSideData->m_ptNotch_Dimension_Edge[nNotchIdx][nMeasureIdx].x,
+											pSideData->m_ptNotch_Dimension_Edge[nNotchIdx][nMeasureIdx].y);
 
 				rtDefectArea.InflateRect(10, 10);
 
@@ -4219,7 +4194,7 @@
 				defectInfo.m_iFrameIdx			= stFrame.nFrameIdx;
 				defectInfo.m_nCamID				= m_iCamera;
 				defectInfo.m_nScanIdx			= stFrame.nScanIdx;
-				defectInfo.m_nGlassStartLine	= m_pGlassData->GetSideData(emDim)->m_nGlassStartLine;
+				defectInfo.m_nGlassStartLine	= pSideData->m_nGlassStartLine;
 				defectInfo.m_nSideIdx			= (int) emDim;
 				defectInfo.m_ptDefectPos_pxl	= rtDefectArea.CenterPoint();
 				defectInfo.m_rtDefectPos_pxl	= rtDefectArea;
@@ -4232,36 +4207,38 @@
 		// Chamfer
 		if(m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_bNotch_Chamfer_Use[nMeasureIdx] == TRUE)
 		{
-			double dChamfer_um = m_pGlassData->GetSideData(emDim)->m_dNotch_Chamfer_Edge_Result_um[nNotchIdx][nMeasureIdx];
+			double dChamfer_um = pSideData->m_dNotch_Chamfer_Edge_Result_um[nNotchIdx][nMeasureIdx];
 			double dChamfer_std_um = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_dNotch_Chamfer_STD_mm[nMeasureIdx] * 1000.0;
 			double dChamfer_min_um = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_dNotch_Chamfer_Diff_MIN_mm[nMeasureIdx] * 1000.0;
 			double dChamfer_max_um = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_dNotch_Chamfer_Diff_MAX_mm[nMeasureIdx] * 1000.0;
 			double dChamfer_Diff_um = dChamfer_std_um - dChamfer_um;
 
-			m_pGlassData->GetSideData(emDim)->m_nNotch_Chamfer_Edge_Judge[nNotchIdx][nMeasureIdx] = 1;
+			pSideData->m_nNotch_Chamfer_Edge_Judge[nNotchIdx][nMeasureIdx] = 1;
+
+#if MINI_LED
 			//选取测量研磨尺寸最大值
-			if (m_nChamferOffset_um < dChamfer_um)
-			{
-					m_nChamferOffset_um = dChamfer_um;
-				
+			if (m_nChamferOffset_um < dChamfer_um) {
+				m_nChamferOffset_um = dChamfer_um;
 			}
-			m_pGlassData->GetSideData(emDim)->m_dNotch_Chamfer_Edge_Result_Diff_um[nNotchIdx][nMeasureIdx] = dChamfer_std_um - dChamfer_um;
-			m_pGlassData->GetSideData(emDim)->m_dNotch_Chamfer_Edge_Judge_Std_um[nNotchIdx][nMeasureIdx] = dChamfer_std_um;
-			m_pGlassData->GetSideData(emDim)->m_dNotch_Chamfer_Edge_Judge_Min_um[nNotchIdx][nMeasureIdx] = dChamfer_min_um;
-			m_pGlassData->GetSideData(emDim)->m_dNotch_Chamfer_Edge_Judge_Max_um[nNotchIdx][nMeasureIdx] = dChamfer_max_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;
+			pSideData->m_dNotch_Chamfer_Edge_Judge_Max_um[nNotchIdx][nMeasureIdx] = dChamfer_max_um;
 
 			if(dChamfer_um > dChamfer_std_um + dChamfer_max_um)
-				m_pGlassData->GetSideData(emDim)->m_nNotch_Chamfer_Edge_Judge[nNotchIdx][nMeasureIdx] = 2;
+				pSideData->m_nNotch_Chamfer_Edge_Judge[nNotchIdx][nMeasureIdx] = 2;
 			else if(dChamfer_um < dChamfer_std_um - dChamfer_min_um)
-				m_pGlassData->GetSideData(emDim)->m_nNotch_Chamfer_Edge_Judge[nNotchIdx][nMeasureIdx] = 3;
+				pSideData->m_nNotch_Chamfer_Edge_Judge[nNotchIdx][nMeasureIdx] = 3;
 
-			if(m_pGlassData->GetSideData(emDim)->m_nNotch_Chamfer_Edge_Judge[nNotchIdx][nMeasureIdx] != 1)
+			if(pSideData->m_nNotch_Chamfer_Edge_Judge[nNotchIdx][nMeasureIdx] != 1)
 			{
 				// Make 搬窃..
-				CRect rtDefectArea = CRect(m_pGlassData->GetSideData(emDim)->m_ptNotch_Chamfer_Edge[nNotchIdx][nMeasureIdx].x, 
-					m_pGlassData->GetSideData(emDim)->m_ptNotch_Chamfer_Edge[nNotchIdx][nMeasureIdx].y, 
-					m_pGlassData->GetSideData(emDim)->m_ptNotch_Chamfer_Edge[nNotchIdx][nMeasureIdx].x, 
-					m_pGlassData->GetSideData(emDim)->m_ptNotch_Chamfer_Edge[nNotchIdx][nMeasureIdx].y);
+				CRect rtDefectArea = CRect(pSideData->m_ptNotch_Chamfer_Edge[nNotchIdx][nMeasureIdx].x,
+											pSideData->m_ptNotch_Chamfer_Edge[nNotchIdx][nMeasureIdx].y,
+											pSideData->m_ptNotch_Chamfer_Edge[nNotchIdx][nMeasureIdx].x,
+											pSideData->m_ptNotch_Chamfer_Edge[nNotchIdx][nMeasureIdx].y);
 
 				rtDefectArea.InflateRect(10, 10);
 
@@ -4269,7 +4246,7 @@
 				defectInfo.m_iFrameIdx			= stFrame.nFrameIdx;
 				defectInfo.m_nCamID				= m_iCamera;
 				defectInfo.m_nScanIdx			= stFrame.nScanIdx;
-				defectInfo.m_nGlassStartLine	= m_pGlassData->GetSideData(emDim)->m_nGlassStartLine;
+				defectInfo.m_nGlassStartLine	= pSideData->m_nGlassStartLine;
 				defectInfo.m_nSideIdx			= (int) emDim;
 				defectInfo.m_ptDefectPos_pxl	= rtDefectArea.CenterPoint();
 				defectInfo.m_rtDefectPos_pxl	= rtDefectArea;
@@ -4281,15 +4258,14 @@
 
 		// Make Comment
 		char strJudge[4][16] = {"None", "OK", "OV", "UN"};
-
-		sprintf(m_pGlassData->GetSideData(emDim)->m_strNotchMeasure_Comment[nNotchIdx][nMeasureIdx], "NOTCH[%d]-POINT[%d]-DIM[%.1f/STD:%.1f][%s], CHAMFER[%.1f/STD:%.1f][%s]"
+		sprintf(pSideData->m_strNotchMeasure_Comment[nNotchIdx][nMeasureIdx], "NOTCH[%d]-POINT[%d]-DIM[%.1f/STD:%.1f][%s], CHAMFER[%.1f/STD:%.1f][%s]"
 			, nNotchIdx+1, nMeasureIdx+1
-			, m_pGlassData->GetSideData(emDim)->m_dNotch_Dimension_Edge_Result_um[nNotchIdx][nMeasureIdx]
-			, m_pGlassData->GetSideData(emDim)->m_dNotch_Dimension_Edge_Judge_Std_um[nNotchIdx][nMeasureIdx]
-			, strJudge[m_pGlassData->GetSideData(emDim)->m_nNotch_Dimension_Edge_Judge[nNotchIdx][nMeasureIdx]]
-			, m_pGlassData->GetSideData(emDim)->m_dNotch_Chamfer_Edge_Result_um[nNotchIdx][nMeasureIdx]
-			, m_pGlassData->GetSideData(emDim)->m_dNotch_Chamfer_Edge_Judge_Std_um[nNotchIdx][nMeasureIdx]
-			, strJudge[m_pGlassData->GetSideData(emDim)->m_nNotch_Chamfer_Edge_Judge[nNotchIdx][nMeasureIdx]]);
+			, pSideData->m_dNotch_Dimension_Edge_Result_um[nNotchIdx][nMeasureIdx]
+			, pSideData->m_dNotch_Dimension_Edge_Judge_Std_um[nNotchIdx][nMeasureIdx]
+			, strJudge[pSideData->m_nNotch_Dimension_Edge_Judge[nNotchIdx][nMeasureIdx]]
+			, pSideData->m_dNotch_Chamfer_Edge_Result_um[nNotchIdx][nMeasureIdx]
+			, pSideData->m_dNotch_Chamfer_Edge_Judge_Std_um[nNotchIdx][nMeasureIdx]
+			, strJudge[pSideData->m_nNotch_Chamfer_Edge_Judge[nNotchIdx][nMeasureIdx]]);
 	}
 }
 
@@ -4307,7 +4283,6 @@
 		return;
 	
 	CString strTemp;
-		
 		
 	int nSide = (int) emDim;
 	int nThreshold = pNotchParam->m_nNotch_Inspect_Defect_Threshold;
@@ -4354,10 +4329,12 @@
 	}
 	m_pDefectControl->ExtractDefect_Notch(emDim, m_iCamera, stFrame.nScanIdx, nNotchIdx, &vecDefectCandidateList);
 #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
 
 	CRect rcIns = rtROI;
 	rcIns.OffsetRect(-rcIns.left,-rcIns.top);
@@ -4366,9 +4343,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());
@@ -4388,8 +4364,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;
@@ -4408,11 +4384,6 @@
 		{
 			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;
 		}
 	}
 
@@ -4437,15 +4408,17 @@
 
 	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	
+	cvDilate(pMaskImage, pDilatedMaskImage, 0, pNotchParam->m_nNotch_Inspect_Defect_dilate);
+	strTemp.Format(_T("Notch\\Notch_%d_Defect_pMaskImage2"), nNotchIdx);
+	SaveDebugImage(emDim, stFrame, pDilatedMaskImage, strTemp);
+
+	cvAnd(pDilatedMaskImage, pProcBinImage, pDilatedMaskImage);
+	strTemp.Format(_T("Notch\\Notch_%d_Defect_pMaskImage3"), nNotchIdx);
+	SaveDebugImage(emDim, stFrame, pDilatedMaskImage, strTemp);
+#endif	
+
 	// 3. Blob
 	int iScan = stFrame.nScanIdx;
 	int iFrame = stFrame.nFrameIdx;
@@ -4461,14 +4434,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);
@@ -4518,27 +4492,21 @@
 		CRect rtSet_Area = pNotchParam->m_rtNotch_Circle_Area_pxl[nCircleIdx];
 
 		CRect rtRotateArea;
-
-		if(GetAlignRotate(emDim, ptSet_TopMark, ptSet_BotMark, rtSet_Area, rtRotateArea) == FALSE)
+		if (GetAlignRotate(emDim, ptSet_TopMark, ptSet_BotMark, rtSet_Area, rtRotateArea) == FALSE) {
+			g_pLog->DisplayMessage(_T("Notch Circle %d Rotate Area Error"), nCircleIdx);
 			continue;
+		}
 
 		CRect rtROI = rtRotateArea;
+		if (rtROI.Width() <= 0 || rtROI.Height() <= 0) {
+			g_pLog->DisplayMessage(_T("Invalid ROI size: %d x %d"), rtROI.Width(), rtROI.Height());
+			continue;
+		}
 		pSideData->m_rtNotchCircleArea_pxl[nNotchIdx][nCircleIdx] = rtROI;
 
 		VectorDouble vectorX;
 		VectorDouble vectorY;
 		VectorDouble vectorR;
-
-		/*
-		for(int nMeasureIdx=0; nMeasureIdx<MAX_SIDE_NOTCH_MEASURE_COUNT; nMeasureIdx++)
-		{
-			if(pSideData->m_nNotch_Dimension_Edge_Judge[nMeasureIdx] ==0)
-				continue;
-
-			vectorX.push_back((double) pSideData->m_ptNotch_Dimension_Edge[nNotchIdx][nMeasureIdx].x);
-			vectorY.push_back((double) pSideData->m_ptNotch_Dimension_Edge[nNotchIdx][nMeasureIdx].y);
-		}
-		*/
 
 		// 2. Get Edge Line
 		int nThreshold = m_pRecipe->m_SideParam[(int) emDim].m_nChamferLineThreshold;
@@ -4548,22 +4516,27 @@
 		IplImage* pEdgeImage = cvCreateImage(cvSize(rtROI.Width(), rtROI.Height()), 8, 1);
 		IplImage* pCannyEdgeImage = cvCreateImage(cvSize(rtROI.Width(), rtROI.Height()), 8, 1);
 
-		for(int i=0; i<rtROI.Height(); i++)
-			memcpy(&pEdgeImage->imageData[(i*pEdgeImage->widthStep)], (pFrameBuffer + ((rtROI.top+i)*m_nFrameWidth) + rtROI.left), rtROI.Width());
+		for (int i = 0; i < rtROI.Height(); i++) {
+			memcpy(&pEdgeImage->imageData[(i * pEdgeImage->widthStep)], (pFrameBuffer + ((rtROI.top + i) * m_nFrameWidth) + rtROI.left), rtROI.Width());
+		}
 
-		int sigma1 = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nSmooth_Filter;
+		int nMedianFilterSize = m_pRecipe->m_SideParam[(int)emDim].m_NotchPrm[nNotchIdx].m_nSmooth_Filter;
+		nMedianFilterSize = std::max(3, std::min(nMedianFilterSize, 31));
+		if (nMedianFilterSize % 2 == 0) {
+			nMedianFilterSize--;
+		}
+		cvSmooth(pEdgeImage, pEdgeImage, CV_MEDIAN, nMedianFilterSize);
 
-		if(sigma1 % 2 == 0)
-			sigma1 = sigma1 - 1;
+		double th1 = 40.0;
+		double th2 = std::max(1.0, std::min((double)m_pRecipe->m_SideParam[(int)emDim].m_NotchPrm[nNotchIdx].m_nGrind_Threshold * 10, 255.0));
 
-		if(1 < sigma1)
-			cvSmooth(pEdgeImage,pEdgeImage,CV_MEDIAN,sigma1);
+		if (th1 > th2) {
+			std::swap(th1, th2);
+		}
 
-		double th1 = (double) m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nGrind_Threshold * 10;
-		double th2 = 40.0;
-		int sigma=5;
+		g_pLog->DisplayMessage(_T("Smooth Filter: %d, Canny th1: %.1f, th2: %.1f"), nMedianFilterSize, th1, th2);
 
-		cvCanny(pEdgeImage,pCannyEdgeImage,th1,th2,sigma);
+		cvCanny(pEdgeImage, pCannyEdgeImage, th1, th2, nMedianFilterSize);
 
 		CString strTemp;
 		strTemp.Format(_T("Notch\\Notch_%d_Circle_%d_Ori.jpg"), nNotchIdx, nCircleIdx);
@@ -4647,7 +4620,6 @@
 			{
 				sprintf(pSideData->m_strNotchCircle_Radius_Comment[nNotchIdx][nCircleIdx], "Rad. [OK] %.1f um / %.1f um", dRadiuse_um, dResult_um);
 			}
-	
 		}
 	}
 }
@@ -5565,8 +5537,6 @@
 	return TRUE;
 }
 
-
-
 BOOL CInspectCamera::Measure_TopCorner_RCut(int iThread,DimensionDir emDim, stFrameIndex stFrame)
 {
 	if(m_pGlassData == NULL || m_pRecipe == NULL)
@@ -5938,13 +5908,11 @@
 
 BOOL CInspectCamera::OnThreadEnd(int iThread, CInspectThread *pInspectThread)
 {
-
 	return TRUE;
 }
 
 BOOL CInspectCamera::OnThreadEndAll()
 {
-
 	return TRUE;
 }
 

--
Gitblit v1.9.3