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)
      return;
   CString strTemp;
   int sigma1 = m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nSmooth_Filter;
   int sigma2 = 7;
   if(sigma1 % 2 == 0)
      sigma1 = sigma1 - 1;
   if(1 < sigma1)
      cvSmooth(scr,img_Bin,CV_MEDIAN,sigma1);
//    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);
   double th1 = (double) m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nGrind_Threshold;
   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)
   if (nNotchIdx < 0 || nNotchIdx >= MAX_SIDE_NOTCH_COUNT) {
      return;
   }
   if(nNotchIdx < 0 || MAX_SIDE_NOTCH_COUNT <= nNotchIdx)
      return;
   // 中值滤波器大小,用于去噪,必须为奇数(若偶数会自动减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;
   // 磨边区域阈值,控制反二值化阈值,主要用于提取 Notch 边缘特征
   const int nGrindThreshold = m_pRecipe->m_SideParam[(int)emDim].m_NotchPrm[nNotchIdx].m_nGrind_Threshold;
   CString strTemp;
   cvThreshold(scr,img_Edge,sigma1,255,CV_THRESH_BINARY_INV);
   // 玻璃区域阈值,控制正二值化阈值,用于提取玻璃主体区域
   const int nGlassThreshold = m_pRecipe->m_SideParam[(int)emDim].m_NotchPrm[nNotchIdx].m_nGlass_Threshold;
   double th1 = (double) m_pRecipe->m_SideParam[(int) emDim].m_NotchPrm[nNotchIdx].m_nGrind_Threshold;
   double th2 = 40.0;
   int sigma=5;
   // 确保奇数滤波器大小
   int nFilterSize = (nSmoothFilter % 2 == 0) ? (nSmoothFilter - 1) : nSmoothFilter;
   cvCanny(img_Edge, img_Edge,th1,th2,sigma);
   if (nFilterSize > 1) {
      // 中值滤波降噪
      cvSmooth(scr, img_Bin, CV_MEDIAN, nFilterSize);
   }
   strTemp.Format(_T("Notch\\Notch_%d_Edge.jpg"), nNotchIdx);
   SaveDebugImage(emDim, stFrame, img_Edge, strTemp);
   // 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);
   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)
@@ -4156,6 +4110,21 @@
      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;
      }
      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;
      }
      // 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;
@@ -4169,9 +4138,9 @@
      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;
      }
   }
@@ -4243,7 +4212,6 @@
         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;
@@ -4308,7 +4276,6 @@
   
   CString strTemp;
      
   int nSide = (int) emDim;
   int nThreshold = pNotchParam->m_nNotch_Inspect_Defect_Threshold;
   int nOffset = pNotchParam->m_nNotch_Inspect_Defect_Offset;
@@ -4354,7 +4321,6 @@
   }
   m_pDefectControl->ExtractDefect_Notch(emDim, m_iCamera, stFrame.nScanIdx, nNotchIdx, &vecDefectCandidateList);
#else
   nOffset = m_nChamferOffset_um / m_pGlassData->GetSideData(emDim)->m_dPixelSizeX;
   nOffset += pNotchParam->m_nNotch_Inspect_Defect_Offset;
   m_nChamferOffset_um = 0;
@@ -4518,27 +4484,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 +4508,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++)
      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 +4612,6 @@
         {
            sprintf(pSideData->m_strNotchCircle_Radius_Comment[nNotchIdx][nCircleIdx], "Rad. [OK] %.1f um / %.1f um", dRadiuse_um, dResult_um);
         }
      }
   }
}
@@ -5565,8 +5529,6 @@
   return TRUE;
}
BOOL CInspectCamera::Measure_TopCorner_RCut(int iThread,DimensionDir emDim, stFrameIndex stFrame)
{
   if(m_pGlassData == NULL || m_pRecipe == NULL)
@@ -5938,13 +5900,11 @@
BOOL CInspectCamera::OnThreadEnd(int iThread, CInspectThread *pInspectThread)
{
   return TRUE;
}
BOOL CInspectCamera::OnThreadEndAll()
{
   return TRUE;
}