#include "StdAfx.h" #include "PostProcess.h" #include "ResultData.h" #include "FreeImage.h" #include "ResultFile.h" #include "Glass_Data.h" #include "GlassRecipe.h" #include "Transfer_Data.h" #include "InspectCamera.h" #define JPEG_RATE JPEG_QUALITYNORMAL|JPEG_OPTIMIZE CPostProcess::CPostProcess(void) { InitPostProcessing(); m_pResultData = NULL; } CPostProcess::~CPostProcess(void) { } void CPostProcess::SetInspectCamera(int iCam,CInspectCamera *pIns) { m_pInspectCamera[iCam] = pIns; } void CPostProcess::SetPrameter(CGlass_Data *pGlsData,CHardwareSettings *pHW,CGlassRecipe *pRecipe,CInspectOption *pInsOpt,CProfileOption *pProfileOpt,CTransfer_Data *pTransData,CResultData *pResultData) { m_pGlassData = pGlsData; m_pHardware = pHW; m_pRecipe = pRecipe; m_pInspectOpt = pInsOpt; m_pProfileOpt = pProfileOpt; m_pTransData = pTransData; m_pResultData = pResultData; } BOOL CPostProcess::InitPostProcessing() { m_pGlassData = NULL; m_pHardware = NULL; m_pRecipe = NULL; for(int i=0;im_strRecipeName.Format(_T("%s"), m_pRecipe->GetRecipeName()); m_pResultData->m_strScanStartTime.Format(_T("%s"),m_pGlassData->GetLoadingTime()); m_pResultData->m_strScanEndTime.Format(_T("%s"),m_pGlassData->GetScanEndTime()); } void CPostProcess::AddHistoryData(HISTORY_RESULT Data) { m_HistoryData = Data; } BOOL CPostProcess::RunPostProcessing() { g_pLog->DisplayMessage(_T("Start RunPostProcessing")); if(m_pGlassData == NULL) { g_pLog->DisplayMessage(_T("Glass Data is NULL")); return FALSE; } CreateDataDirectory(); m_pResultData->Reset(); m_pResultData->SetInspectOpt(m_pInspectOpt,m_pProfileOpt); SetResultData(); MergeDefect(); int nDefectCount = GetInspectionDefect(m_pGlassData->GetMapDefect()); g_pLog->DisplayMessage(_T("Total Defect Count : Tot %d, Defect %d"),m_pGlassData->GetTotalDefectCount(),m_pGlassData->GetJudgeDefectCount()); m_pResultData->SetDefectIns(m_pGlassData->GetMapDefect(),nDefectCount); return TRUE; } void CPostProcess::RunPostProcessing_SaveDefectInfo() { int nDefectCount = m_pGlassData->GetJudgeDefectCount(); SaveProfileData(); SaveChamferData(TRUE); SaveChamferData(FALSE); SaveDefectData(nDefectCount); } void CPostProcess::SaveDefectData(int nDefect) { int iDefect = 0; CInspectDefect *pDefect; MapDefectIt it; MapDefect *pMapDefect = m_pGlassData->GetMapDefect(); CString glass_id; glass_id.Format(_T("%s"), g_pBase->m_strHPanelID); if (glass_id.IsEmpty() == TRUE) { glass_id.Format(_T("MANUAL_ID")); } CString defect_data,strValue; CString strTime = g_pBase->m_strLoadingTime; for(it=pMapDefect->begin();it!=pMapDefect->end();it++,iDefect++) { pDefect = static_cast(it->second); if(pDefect == NULL) continue; if(pDefect->m_bJudgeDefect == FALSE) continue; switch(pDefect->m_sDefectLoc) { case 0: strValue.Format(_T("Crack")); break; case 1: strValue.Format(_T("Broken")); break; case 2: strValue.Format(_T("Chip")); break; case 3: strValue.Format(_T("Chamfer")); break; case 4: strValue.Format(_T("Burr")); break; case 5: strValue.Format(_T("Thin")); break; case 6: strValue.Format(_T("Area")); break; case 7: strValue.Format(_T("NCut")); break; case 8: strValue.Format(_T("CCD Hole")); break; case 9: strValue.Format(_T("Profile")); break; case 10: strValue.Format(_T("KDist")); break; case 11: strValue.Format(_T("RCUTChip")); break; default: strValue.Format(_T("Unknown")); break; } CString strEqip,strUnit,strInsGrind,strRoughPadLayer,strRoughPadCount,strRoughNonPadLayer,strRoughNonPadCount; CString strPrePadLayer,strPrePadCount,strPreNonPadLayer,strPreNonPadCount; CTransfer_Data *pTrans = m_pTransData; if(pTrans != NULL) { strEqip.Format(_T("%d"),pTrans->m_nEqpNo); strUnit.Format(_T("%d"),pTrans->m_nUnitNo); strInsGrind.Format(_T("%d"),pTrans->m_nisGrind); strRoughPadLayer.Format(_T("%d"),pTrans->m_nRoughPadLayer); strRoughPadCount.Format(_T("%d"),pTrans->m_nRoughPadCount); strRoughNonPadLayer.Format(_T("%d"),pTrans->m_nRoughNonPadLayer); strRoughNonPadCount.Format(_T("%d"),pTrans->m_nRoughNonPadCount); strPrePadLayer.Format(_T("%d"),pTrans->m_nPrePadLayer); strPrePadCount.Format(_T("%d"),pTrans->m_nPrePadCount); strPreNonPadLayer.Format(_T("%d"),pTrans->m_nPreNonPadLayer); strPreNonPadCount.Format(_T("%d"),pTrans->m_nPreNonPadCount); } else { strEqip.Format(_T("%d"),0); strUnit.Format(_T("%d"),0); strInsGrind.Format(_T("%d"),0); strRoughPadLayer.Format(_T("%d"),0); strRoughPadCount.Format(_T("%d"),0); strRoughNonPadLayer.Format(_T("%d"),0); strRoughNonPadCount.Format(_T("%d"),0); strPrePadLayer.Format(_T("%d"),0); strPrePadCount.Format(_T("%d"),0); strPreNonPadLayer.Format(_T("%d"),0); strPreNonPadCount.Format(_T("%d"),0); } defect_data.AppendFormat(_T("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%d,%d,%d,%d,%s,"), strTime, glass_id, strEqip, strUnit, strRoughPadLayer, strRoughPadCount, strRoughNonPadLayer, strRoughNonPadCount, strPrePadLayer, strPrePadCount, strPreNonPadLayer, strPreNonPadCount, PANEL_SIDE[pDefect->m_nSideLoc], strValue, pDefect->m_nUMOriginY, pDefect->m_nUMOriginX, pDefect->m_nUMSizeY, pDefect->m_nUMSizeX, pDefect->m_strDefectPath); defect_data.Append(_T("\r\n")); } CString Defect_file_path; Defect_file_path.Format(_T("%s\\%s_DefectData.csv"), m_strResultFolder, g_pBase->m_strLoadingDay); CheckDirectory(Defect_file_path); CFile file_module; CFileException file_exception; file_module.Open(Defect_file_path, CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate|CFile::shareDenyWrite|CFile::shareDenyRead, &file_exception); if(file_exception.m_cause == CFileException::none) { if(file_module.SeekToEnd() == 0L) { CString defect_title; defect_title.Format(_T("TIME,ID,EQP NO,UNIT NO,ROUGH PAD LAYER 1,ROUGH PAD COUNT 1,ROUGH NON PAD LAYER 1,ROUGH NON PAD COUNT 1,ROUGH PAD LAYER 2,ROUGH PAD COUNT 2,ROUGH NON PAD LAYER 2,ROUGH NON PAD COUNT 2,SIDE,CAT,POS_X,POS_Y,WIDTH,HEIGHT,IMAGE_PATH\r\n")); file_module.Write(defect_title, defect_title.GetLength()*sizeof(TCHAR)); } file_module.Write(defect_data, defect_data.GetLength()*sizeof(TCHAR)); file_module.Close(); } } void CPostProcess::SaveProfileData() { CString glass_id; glass_id.Format(_T("%s"), g_pBase->m_strHPanelID); if (glass_id.IsEmpty() == TRUE) { glass_id.Format(_T("MANUAL_ID")); } CString profile_data; CString strEqip,strUnit,strInsGrind,strRoughPadLayer,strRoughPadCount,strRoughNonPadLayer,strRoughNonPadCount; CString strPrePadLayer,strPrePadCount,strPreNonPadLayer,strPreNonPadCount; CTransfer_Data *pTrans = m_pTransData; if(pTrans != NULL) { strEqip.Format(_T("%d"),pTrans->m_nEqpNo); strUnit.Format(_T("%d"),pTrans->m_nUnitNo); strInsGrind.Format(_T("%d"),pTrans->m_nisGrind); strRoughPadLayer.Format(_T("%d"),pTrans->m_nRoughPadLayer); strRoughPadCount.Format(_T("%d"),pTrans->m_nRoughPadCount); strRoughNonPadLayer.Format(_T("%d"),pTrans->m_nRoughNonPadLayer); strRoughNonPadCount.Format(_T("%d"),pTrans->m_nRoughNonPadCount); strPrePadLayer.Format(_T("%d"),pTrans->m_nPrePadLayer); strPrePadCount.Format(_T("%d"),pTrans->m_nPrePadCount); strPreNonPadLayer.Format(_T("%d"),pTrans->m_nPreNonPadLayer); strPreNonPadCount.Format(_T("%d"),pTrans->m_nPreNonPadCount); } else { strEqip.Format(_T("%d"),0); strUnit.Format(_T("%d"),0); strInsGrind.Format(_T("%d"),0); strRoughPadLayer.Format(_T("%d"),0); strRoughPadCount.Format(_T("%d"),0); strRoughNonPadLayer.Format(_T("%d"),0); strRoughNonPadCount.Format(_T("%d"),0); strPrePadLayer.Format(_T("%d"),0); strPrePadCount.Format(_T("%d"),0); strPreNonPadLayer.Format(_T("%d"),0); strPreNonPadCount.Format(_T("%d"),0); } CString strRet(_T("OK")); if (m_HistoryData.bResult == FALSE) { strRet.Format(_T("NG")); } profile_data.Format(_T("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,"), g_pBase->m_strLoadingTime, glass_id, strRet, strEqip, strUnit, strRoughPadLayer, strRoughPadCount, strRoughNonPadLayer, strRoughNonPadCount, strPrePadLayer, strPrePadCount, strPreNonPadLayer, strPreNonPadCount); PROFILECHAMFER_RESULT_DATA *pResult = m_pGlassData->GetProfileInspectData(); for (BYTE loop_count=0; loop_countProfileData[loop_count].nProfileDiff); } profile_data.Append(_T("\n")); CString profile_file_path; profile_file_path.Format(_T("%s\\%s_ProfileData.csv"), m_strResultFolder, g_pBase->m_strLoadingDay); CheckDirectory(profile_file_path); CFile file_module; CFileException file_exception; file_module.Open(profile_file_path, CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate|CFile::shareDenyWrite|CFile::shareDenyRead, &file_exception); if(file_exception.m_cause == CFileException::none) { if(file_module.SeekToEnd() == 0L) { CString profile_title; profile_title.Format(_T("TIME,ID,NG/OK,EQP NO,UNIT NO,ROUGH PAD LAYER 1,ROUGH PAD COUNT 1,ROUGH NON PAD LAYER 1,ROUGH NON PAD COUNT 1,ROUGH PAD LAYER 2,ROUGH PAD COUNT 2,ROUGH NON PAD LAYER 2,ROUGH NON PAD COUNT 2,")); for(BYTE header=1; header<(MAX_PROFILE_SETTING_COUNT+1); header++) { profile_title.AppendFormat(_T("%d,"),header); } profile_title.Append(_T("\r\n")); file_module.Write(profile_title, profile_title.GetLength()*sizeof(TCHAR)); } file_module.Write(profile_data, profile_data.GetLength()*sizeof(TCHAR)); file_module.Close(); } } void CPostProcess::SaveChamferData(BOOL bUp) { CString glass_id; glass_id.Format(_T("%s"), g_pBase->m_strHPanelID); if (glass_id.IsEmpty() == TRUE) { glass_id.Format(_T("MANUAL_ID")); } CString chamfer_data; CString strEqip,strUnit,strInsGrind,strRoughPadLayer,strRoughPadCount,strRoughNonPadLayer,strRoughNonPadCount; CString strPrePadLayer,strPrePadCount,strPreNonPadLayer,strPreNonPadCount; CTransfer_Data *pTrans = m_pTransData; if(pTrans != NULL) { strEqip.Format(_T("%d"),pTrans->m_nEqpNo); strUnit.Format(_T("%d"),pTrans->m_nUnitNo); strInsGrind.Format(_T("%d"),pTrans->m_nisGrind); strRoughPadLayer.Format(_T("%d"),pTrans->m_nRoughPadLayer); strRoughPadCount.Format(_T("%d"),pTrans->m_nRoughPadCount); strRoughNonPadLayer.Format(_T("%d"),pTrans->m_nRoughNonPadLayer); strRoughNonPadCount.Format(_T("%d"),pTrans->m_nRoughNonPadCount); strPrePadLayer.Format(_T("%d"),pTrans->m_nPrePadLayer); strPrePadCount.Format(_T("%d"),pTrans->m_nPrePadCount); strPreNonPadLayer.Format(_T("%d"),pTrans->m_nPreNonPadLayer); strPreNonPadCount.Format(_T("%d"),pTrans->m_nPreNonPadCount); } else { strEqip.Format(_T("%d"),0); strUnit.Format(_T("%d"),0); strInsGrind.Format(_T("%d"),0); strRoughPadLayer.Format(_T("%d"),0); strRoughPadCount.Format(_T("%d"),0); strRoughNonPadLayer.Format(_T("%d"),0); strRoughNonPadCount.Format(_T("%d"),0); strPrePadLayer.Format(_T("%d"),0); strPrePadCount.Format(_T("%d"),0); strPreNonPadLayer.Format(_T("%d"),0); strPreNonPadCount.Format(_T("%d"),0); } chamfer_data.Format(_T("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,"), g_pBase->m_strLoadingTime, glass_id,strEqip, strUnit, strRoughPadLayer, strRoughPadCount, strRoughNonPadLayer, strRoughNonPadCount, strPrePadLayer, strPrePadCount, strPreNonPadLayer, strPreNonPadCount); PROFILECHAMFER_RESULT_DATA *pResult = m_pGlassData->GetProfileInspectData(); for (BYTE loop_count=0; loop_countResultChamfer[loop_count][bUp].nChamferCalThick); } chamfer_data.Append(_T("\n")); CString profile_file_path; if(bUp ==TRUE) profile_file_path.Format(_T("%s\\%s_ChamferUpData.csv"),m_strResultFolder, g_pBase->m_strLoadingDay); else profile_file_path.Format(_T("%s\\%s_ChamferDownData.csv"),m_strResultFolder, g_pBase->m_strLoadingDay); CheckDirectory(profile_file_path); CFile file_module; CFileException file_exception; file_module.Open(profile_file_path, CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate|CFile::shareDenyWrite|CFile::shareDenyRead, &file_exception); if(file_exception.m_cause == CFileException::none) { if(file_module.SeekToEnd() == 0L) { CString chamfer_title; chamfer_title.Format(_T("TIME,ID,EQP NO,UNIT NO,ROUGH PAD LAYER 1,ROUGH PAD COUNT 1,ROUGH NON PAD LAYER 1,ROUGH NON PAD COUNT 1,ROUGH PAD LAYER 2,ROUGH PAD COUNT 2,ROUGH NON PAD LAYER 2,ROUGH NON PAD COUNT 2,")); for(BYTE header=1; header<(MAX_PROFILE_SETTING_COUNT+1); header++) { chamfer_title.AppendFormat(_T("%d,"),header); } chamfer_title.Append(_T("\r\n")); file_module.Write(chamfer_title, chamfer_title.GetLength()*sizeof(TCHAR)); } file_module.Write(chamfer_data, chamfer_data.GetLength()*sizeof(TCHAR)); file_module.Close(); } } void CPostProcess::JudgementData() { if(m_pGlassData == NULL) return; PROFILECHAMFER_RESULT_DATA *pResult = m_pGlassData->GetProfileInspectData(); PROFILE_RESULT *pProfileRes; int index; for(index=0;indexProfileData[index]; pProfileRes->bProfileJudge = TRUE; if(pProfileRes->bUseJudge == TRUE) { if(pProfileRes->nProfileDiff < -1*pProfileRes->nJudgeMinus || pProfileRes->nProfileDiff > pProfileRes->nJudgePlus) { pProfileRes->bProfileJudge = FALSE; } } for(int i=0;i<2;i++) { CHAMFER_RESULT *pChamferRes = &pResult->ResultChamfer[index][i]; pChamferRes->bChamferJudge = TRUE; if(pChamferRes->bUseJudge == TRUE) { if(pChamferRes->nChamferThick < pChamferRes->nJudgeReference - pChamferRes->nJudgeMinusChamfer || pChamferRes->nChamferThick > pChamferRes->nJudgeReference+pChamferRes->nJudgePlusChamfer) { pChamferRes->bChamferJudge = FALSE; //InsertManualDefect(pInspect,m_pHardware->GetCameraSettings(pProfileRes->iCam,pProfileRes->iScan),pProfileRes->iScan,pProfileRes->ptGlassStart.x,pProfileRes->ptGlassStart.y,DefectLoc_Chamfer); } } } } } BOOL CPostProcess::SaveDefectImage(LPBYTE pImage,CSize szImage,CRect rectDefect,CString strFile,DimensionDir emDir) { FIBITMAP *bitmap = NULL; BYTE *pBitBuffer; bitmap = FreeImage_Allocate(szImage.cx, szImage.cy,8); pBitBuffer = FreeImage_GetBits(bitmap); if(pBitBuffer == NULL) { FreeImage_Unload(bitmap); return FALSE; } CopyMemory(pBitBuffer,pImage,szImage.cx*szImage.cy); FIBITMAP *pBit24 = FreeImage_ConvertTo24Bits(bitmap); if(pBit24 == NULL) return FALSE; char cFilePath[255]; #ifdef _UNICODE int nSize = WideCharToMultiByte(CP_ACP, 0, strFile.GetBuffer(), -1, NULL, 0, NULL,NULL); WideCharToMultiByte(CP_ACP, 0, strFile.GetBuffer(), -1, cFilePath, nSize, NULL, NULL); #else sprintf_s(cFilePath,sizeof(char)*255,_T("%s"),(LPSTR)(LPCTSTR)strFile); #endif FreeImage_FlipVertical(pBit24); FreeImage_Save(FIF_JPEG, pBit24, cFilePath,JPEG_RATE); FreeImage_Unload(bitmap); FreeImage_Unload(pBit24); return TRUE; } BOOL CPostProcess::SaveDefectImage2(LPBYTE pImage,CSize szImage,CRect rectDefect,CString strFile,DimensionDir emDir) { IplImage *pImageTmp = cvCreateImage(cvSize(szImage.cx, szImage.cy), 8,1); cvZero(pImageTmp); // add point to the image for (int i = 0; iimageData[i*pImageTmp->widthStep+j] = pImage[i*szImage.cx+j]; } } IplImage *pColorImg = cvCreateImage(cvSize(szImage.cx, szImage.cy), 8, 3); cvZero(pColorImg); cvCvtColor(pImageTmp, pColorImg, CV_GRAY2BGR); cvRectangle(pColorImg,CvPoint(rectDefect.left,rectDefect.top), CvPoint(rectDefect.left+rectDefect.Width(),rectDefect.top+rectDefect.Height()), cvScalar(0,0,255), 1); USES_CONVERSION; char str_filename[1024]; sprintf_s(str_filename, "%s", W2A(strFile)); cvSaveImage(str_filename, pColorImg); cvReleaseImage(&pColorImg); cvReleaseImage(&pImageTmp); return TRUE; } void CPostProcess::GetJudgePrm(CDefect *pDefect,int &nJudgeW,int &nJudgeH,BOOL &bAnd,EM_CHIPPING_COMP &eComp,BOOL &bJudgeUse) { CINSPECT_JUDGEMENT_PARM *pJudge = m_pRecipe->GetJudgePrm(); nJudgeW = nJudgeH = 0; bAnd = bJudgeUse = TRUE; switch(pDefect->m_DefectLoc) { case DefectLoc_Chip: nJudgeW = pJudge->defect[pDefect->m_nSideLoc][0].Chip_Side.szDefect.cx; nJudgeH = pJudge->defect[pDefect->m_nSideLoc][0].Chip_Side.szDefect.cy; bAnd = pJudge->defect[pDefect->m_nSideLoc][0].Chip_Side.bConditionAND; bJudgeUse = pJudge->defect[pDefect->m_nSideLoc][0].Chip_Side.bError; break; case DefectLoc_Crack: nJudgeW = pJudge->defect[pDefect->m_nSideLoc][0].Crack.szDefect.cx; nJudgeH = pJudge->defect[pDefect->m_nSideLoc][0].Crack.szDefect.cy; bAnd = pJudge->defect[pDefect->m_nSideLoc][0].Crack.bConditionAND; bJudgeUse = pJudge->defect[pDefect->m_nSideLoc][0].Crack.bError; break; case DefectLoc_Broken: nJudgeW = pJudge->defect[pDefect->m_nSideLoc][0].Broken.szDefect.cx; nJudgeH = pJudge->defect[pDefect->m_nSideLoc][0].Broken.szDefect.cy; bAnd = pJudge->defect[pDefect->m_nSideLoc][0].Broken.bConditionAND; bJudgeUse = pJudge->defect[pDefect->m_nSideLoc][0].Broken.bError; break; case DefectLoc_Burr: nJudgeW = pJudge->defect[pDefect->m_nSideLoc][0].Burr_Side.szDefect.cx; nJudgeH = pJudge->defect[pDefect->m_nSideLoc][0].Burr_Side.szDefect.cy; bAnd = pJudge->defect[pDefect->m_nSideLoc][0].Burr_Side.bConditionAND; bJudgeUse = pJudge->defect[pDefect->m_nSideLoc][0].Burr_Side.bError; break; } if(bAnd == TRUE) eComp = CHIP_AND; else eComp = CHIP_OR; } int CPostProcess::GetInspectionDefect(MapDefect *mapDefectData) { if(m_pRecipe == NULL || m_pHardware == NULL) return 0; int nDefectCount = 0; int iCam,iDefect; CDefect *pDefect; CHardwareSettings *pHard = m_pHardware; CInspectDefect *pInsDefect = NULL; int nResultCount = 0; CString strFile; CCameraSettings *pCamera = NULL; int nJudgeW,nJudgeH; BOOL bAnd,bJudgeUse; EM_CHIPPING_COMP eComp; double dScanRes = 0.,dConvRes = 0.; BOOL bJudgeDefect; int nStageNo = g_pBase->m_nStageNo; if(nStageNo < 0 || nStageNo > 1) nStageNo = 0; for(iCam=0;iCamGetCameraCount();iCam++) { nDefectCount = m_pInspectCamera[iCam]->GetDefectCount(); g_pLog->DisplayMessage(_T("[%d]Camera Defect Count : %d"), iCam,nDefectCount); for(iDefect=0;iDefectGetDefect(iDefect); if(pDefect == NULL || pDefect->m_bRemoved == TRUE) continue; if(m_pGlassData->GetTotalDefectCount() >= MAX_DEFECT_COUNT) return nResultCount; GetJudgePrm(pDefect,nJudgeW,nJudgeH,bAnd,eComp,bJudgeUse); if(bJudgeUse == FALSE) continue; pCamera = m_pHardware->GetCameraSettings(pDefect->m_nCamID,pDefect->m_nScanIdx); if(pCamera == NULL) continue; dScanRes = pCamera->m_dScanResolution[nStageNo]; dConvRes = pCamera->m_dConvResolution[nStageNo]; if (dScanRes <= 0.0 || dConvRes <= 0) continue; if(pDefect->m_nSplineLoc == Spline_None) { bJudgeDefect = FALSE; if(eComp == CHIP_AND) { if((double)pDefect->m_Defect.s_DefectRect.Width()*dConvRes >= nJudgeW && (double)pDefect->m_Defect.s_DefectRect.Height()*dScanRes >= nJudgeH) bJudgeDefect = TRUE; } else if(eComp == CHIP_OR) { if((double)pDefect->m_Defect.s_DefectRect.Width()*dConvRes >= nJudgeW || (double)pDefect->m_Defect.s_DefectRect.Height()*dScanRes >= nJudgeH) bJudgeDefect = TRUE; } else continue; } else { bJudgeDefect = TRUE; } pInsDefect = new CInspectDefect; pCamera = pHard->GetCameraSettings(iCam,pDefect->m_nScanIdx); if(pCamera != NULL) pInsDefect->m_nDir = (int)pCamera->m_enCamDir; INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(pCamera->m_eDimension); pInsDefect->m_nDefectIdx = pDefect->m_nIndex; pInsDefect->m_nCameraID = iCam; pInsDefect->m_nScanIdx = pDefect->m_nScanIdx; pInsDefect->m_nPixelGlassStart = pEdgeInfo->nGlassStartLine; pInsDefect->m_sDefectType = static_cast(pDefect->m_Defect.s_DefectType); pInsDefect->m_nPixelSize = pDefect->m_Defect.s_nDefectArea; pInsDefect->SetPixelConv(pDefect->m_Defect.s_nDefectX); pInsDefect->SetPixelScan(pDefect->m_Defect.s_nDefectY); pInsDefect->m_nPixelConv = pDefect->m_Defect.s_DefectRect.left; pInsDefect->m_nPixelScan = pDefect->m_Defect.s_DefectRect.top; pInsDefect->m_sPixelWidth = pDefect->m_Defect.s_DefectRect.Width(); pInsDefect->m_sPixelHeight = pDefect->m_Defect.s_DefectRect.Height(); pInsDefect->m_nDefectRScale = pDefect->m_Defect.s_nDefectRScale; pInsDefect->m_nLevelSrcMin = pDefect->m_Defect.s_sLevelSrcMin; pInsDefect->m_nLevelSrcMax = pDefect->m_Defect.s_sLevelSrcMax; pInsDefect->m_nLevelSrcAvg = pDefect->m_Defect.s_sLevelSrcAvg; pInsDefect->m_nLevelRefMin = pDefect->m_Defect.s_sLevelRefMin; pInsDefect->m_nLevelRefMax = pDefect->m_Defect.s_sLevelRefMax; pInsDefect->m_nLevelRefAvg = pDefect->m_Defect.s_sLevelRefAvg; pInsDefect->m_nLevelDiffMin = pDefect->m_Defect.s_sLevelDiffMin; pInsDefect->m_nLevelDiffMax = pDefect->m_Defect.s_sLevelDiffMax; pInsDefect->m_nLevelDiffAvg = pDefect->m_Defect.s_sLevelDiffAvg; pInsDefect->m_sDefectLoc = static_cast(pDefect->m_DefectLoc); pInsDefect->m_sSplineLoc = static_cast(pDefect->m_nSplineLoc); pInsDefect->m_sThreshold = pDefect->m_Defect.s_sThreshold; pInsDefect->m_sDefectPeak = pDefect->m_Defect.s_sDefectPeak; pInsDefect->m_nSideLoc = (DimensionDir)pDefect->m_nSideLoc; pInsDefect->m_dwTickCount = GetTickCount(); pInsDefect->m_bJudgeDefect = bJudgeDefect; pInsDefect->m_nProfileIdx = pDefect->m_nProfileIndex; if(DefectProcess(pInsDefect,m_pInspectCamera[iCam]) == FALSE) { delete pInsDefect, pInsDefect = NULL; continue; } if(m_pGlassData->SetDefect(pInsDefect) == FALSE) { delete pInsDefect, pInsDefect = NULL; continue; } if(pInsDefect->m_bJudgeDefect == TRUE) { CString str = _T("Unknown"); switch(pDefect->m_DefectLoc) { case DefectLoc_Crack: str=_T("Crack"); break; case DefectLoc_Broken: str=_T("Broken"); break; case DefectLoc_Chip: { if(pInsDefect->m_sSplineLoc == Spline_None) str=_T("Chip"); else str=_T("R_Chip"); break; } case DefectLoc_Chamfer: str=_T("Chamfer"); break; case DefectLoc_Burr: str=_T("Burr"); break; case DefectLoc_Thin: str=_T("Thin"); break; case DefectLoc_Air: str=_T("Lifting"); break; case DefectLoc_Notch: str=_T("NCut"); break; case DefectLoc_Hole: str=_T("Hole"); break; case DefectLoc_Profile: str=_T("Profile"); break; case DefectLoc_KDist: str = _T("KDist"); break; case DefectLoc_RCUTChip: str = _T("RCUTChip"); break; } strFile.Format(_T("%s\\%03d_%s_%s_%s.jpg"),m_strDefectFolder,pInsDefect->m_nGlassDefectIdx,g_pBase->m_strHPanelID,PANEL_SIDE[(DimensionDir)pDefect->m_nSideLoc],str); CRect rectDefect; rectDefect.left = DEFECTIMAGE_WIDTH/2-pInsDefect->m_sPixelWidth/2; rectDefect.right = DEFECTIMAGE_WIDTH/2+pInsDefect->m_sPixelWidth/2; rectDefect.top = DEFECTIMAGE_HEIGHT/2-pInsDefect->m_sPixelHeight/2; rectDefect.bottom = DEFECTIMAGE_HEIGHT/2+pInsDefect->m_sPixelHeight/2; CheckDirectory(strFile); SaveDefectImage2(pDefect->m_Image,CSize(DEFECTIMAGE_WIDTH,DEFECTIMAGE_HEIGHT),rectDefect,strFile,(DimensionDir)pDefect->m_nSideLoc); pInsDefect->m_strDefectPath = strFile; } nResultCount++; } } return m_pGlassData->GetJudgeDefectCount(); } BOOL CPostProcess::DefectProcess(CInspectDefect* pDefect,CInspectCamera *pInspect) { CGlassRecipe* pRecipe = m_pRecipe; if(pRecipe == NULL) return FALSE; CCameraSettings *pCamera = m_pHardware->GetCameraSettings(pDefect->m_nCameraID,pDefect->m_nScanIdx); if(pCamera == NULL) return FALSE; double dScanRes = 0.,dConvRes = 0.; int nMarginLeft; int nStageNo = g_pBase->m_nStageNo; if(nStageNo < 0 || nStageNo > 1) nStageNo = 0; dScanRes = pCamera->m_dScanResolution[nStageNo]; dConvRes = pCamera->m_dConvResolution[nStageNo]; nMarginLeft = pCamera->m_nMarginLeft; if (dScanRes <= 0.0 || dConvRes <= 0) return FALSE; pDefect->m_nUMOriginX = (int)((double)(pDefect->GetPixelScan() - pDefect->m_nPixelGlassStart) * dScanRes); pDefect->m_nUMOriginY = (int)(((double)(pDefect->GetPixelConv()) * dConvRes)); pDefect->m_nUMSizeX = (int)((double)pDefect->m_sPixelHeight * dScanRes); pDefect->m_nUMSizeY = (int)((double)pDefect->m_sPixelWidth * dConvRes); pDefect->m_nUMSize =(int)((double)pDefect->m_nPixelSize * dScanRes * dConvRes); if(pDefect->m_nUMOriginX <= -1 || pDefect->m_nUMOriginY <= -1) return FALSE; return TRUE; } void CPostProcess::CalcGlassSize() { int iCam,iScan; CHardwareSettings *pHard = m_pHardware; CCameraSettings *pCamera; double dGlasssizeY[2],dGlasssizeX[2]; double dGlassSizeSumY[2],dGlassSizeCntY[2]; double dDist; CAMERA_SCANDIR ScanDirection; double dScanRes; double dConvRes; int nStageNo = g_pBase->m_nStageNo; if(nStageNo < 0 || nStageNo > 1) nStageNo = 0; for(iCam=0;iCamGetCameraCount();iCam++) { for(iScan=0;iScanGetScanCount();iScan++) { pCamera = pHard->GetCameraSettings(iCam,iScan); if(pCamera == NULL) continue; ZeroMemory(dGlasssizeY,sizeof(double)*2); ZeroMemory(dGlasssizeX,sizeof(double)*2); INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(pCamera->m_eDimension); dGlasssizeX[0] = pEdgeInfo->nGlassEdgeXPos[0]; dGlasssizeX[1] = pEdgeInfo->nGlassEdgeXPos[1]; dGlasssizeY[0] = pEdgeInfo->nGlassStartLine; dGlasssizeY[1] = pEdgeInfo->nGlassEndLine; dScanRes = pCamera->m_dScanResolution[nStageNo]; dConvRes = pCamera->m_dConvResolution[nStageNo]; dDist = sqrt(pow((dGlasssizeX[0]*dConvRes-dGlasssizeX[1]*dConvRes),2) + pow((dGlasssizeY[0]*dScanRes-dGlasssizeY[1]*dScanRes),2)); if(dDist > 0) { ScanDirection = pCamera->m_eScanDir; dGlassSizeCntY[(int)ScanDirection]++; dGlassSizeSumY[(int)ScanDirection] += dDist; } } } // Glass Size for(int i=0;i<2;i++) { if(dGlassSizeCntY[i] > 0) m_pResultData->m_dGlassSize[i] = dGlassSizeSumY[i]/dGlassSizeCntY[i]; } } void CPostProcess::MergeDefect() { int iCam,iDefect,iDefectDest; int nDefectCount; CDefect *pDefect,*pDefectDest; int nMergeDistX,nMergeDistY; int nRemoveCount = 0; CRect rect,rectOrg,rectTgt; CHardwareSettings *pHard = m_pHardware; CGlassRecipe *pGlsRcp = m_pRecipe; if(pGlsRcp == NULL) return; nMergeDistX = nMergeDistY = 5; for(iCam=0;iCamGetCameraCount();iCam++) { nDefectCount = m_pInspectCamera[iCam]->GetDefectCount(); for(iDefect=0;iDefectGetDefect(iDefect); if(pDefect == NULL) continue; if(pDefect->m_bRemoved == TRUE) continue; if(pDefect->m_DefectLoc == DefectLoc_Chamfer || pDefect->m_DefectLoc == DefectLoc_Profile) continue; if (pDefect->m_DefectLoc == DefectLoc_Hole || pDefect->m_DefectLoc == DefectLoc_KDist || pDefect->m_DefectLoc == DefectLoc_RCUTChip) continue; rectOrg = pDefect->m_Defect.s_DefectRect; rectOrg.InflateRect(nMergeDistX,nMergeDistY); for(iDefectDest=iDefect+1;iDefectDestGetDefect(iDefectDest); if(pDefectDest == NULL) continue; if(pDefectDest->m_nScanIdx != pDefect->m_nScanIdx) continue; if(pDefectDest->m_bRemoved == TRUE) continue; if(pDefectDest->m_nCamDir != pDefect->m_nCamDir) continue; if(pDefectDest->m_DefectLoc == DefectLoc_Chamfer || pDefectDest->m_DefectLoc == DefectLoc_Profile) continue; rectTgt = pDefectDest->m_Defect.s_DefectRect; if(rect.IntersectRect(rectOrg,rectTgt) == TRUE) { pDefect->m_Defect.MergeBlob(pDefectDest->m_Defect); if(pDefect->m_DefectLoc == DefectLoc_Chip) { if(pDefectDest->m_DefectLoc == DefectLoc_Crack || pDefectDest->m_DefectLoc == DefectLoc_Broken) pDefect->m_DefectLoc = pDefectDest->m_DefectLoc; } else if(pDefect->m_DefectLoc == DefectLoc_Crack) { if(pDefectDest->m_DefectLoc == DefectLoc_Broken) pDefect->m_DefectLoc = pDefectDest->m_DefectLoc; } pDefectDest->m_bRemoved = TRUE; nRemoveCount++; } } } } g_pLog->DisplayMessage(_T("Defect Merge Count : %d"), nRemoveCount); } void CPostProcess::JudgeThinCrackDefect() { int iCam,iDefect; int nDefectCount; int nJudgeCount = 0; CDefect *pDefect; CHardwareSettings *pHard = m_pHardware; CGlassRecipe *pGlsRcp = m_pRecipe; if(pGlsRcp == NULL) return; for(iCam=0;iCamGetCameraCount();iCam++) { nDefectCount = m_pInspectCamera[iCam]->GetDefectCount(); for(iDefect=0;iDefectGetDefect(iDefect); if(pDefect == NULL) continue; if(pDefect->m_bRemoved == TRUE) continue; if(pDefect->m_bThinCrackDefect == FALSE) continue; if(Judge_CornerChip_Item_ThinCrack(pDefect,iCam) == FALSE) { pDefect->m_bRemoved = TRUE; } else { nJudgeCount++; pDefect->m_DefectLoc = DefectLoc_Thin; } } } g_pLog->DisplayMessage(_T("Thin Crack Count : %d"), nJudgeCount); } BOOL CPostProcess::Judge_CornerChip_Item_ThinCrack(CDefect* pDefect,int nCamIdx) { CHardwareSettings *pHard = m_pHardware; CCameraSettings *pCamera; pCamera = pHard->GetCameraSettings(nCamIdx,pDefect->m_nScanIdx); if(pCamera == NULL) return FALSE; int nStageNo = g_pBase->m_nStageNo; if(nStageNo < 0 || nStageNo > 1) nStageNo = 0; double dScanRes = pCamera->m_dScanResolution[nStageNo]; double dConvRes = pCamera->m_dConvResolution[nStageNo]; EM_CHIPPING_COMP emComp = CHIP_OR; //Length and Depth CINSPECT_JUDGEMENT_PARM *pJudgePrm = m_pRecipe->GetJudgePrm(); if(pJudgePrm->bUseLengthDepthComp) { //and if(emComp == CHIP_AND) { if((double)pDefect->m_Defect.s_DefectRect.Width()*dConvRes >= pJudgePrm->nCornerJugeDepth && (double)pDefect->m_Defect.s_DefectRect.Height()*dScanRes >= pJudgePrm->nCornerJugeLength) return TRUE; } else if(emComp == CHIP_OR) { if((double)pDefect->m_Defect.s_DefectRect.Width()*dConvRes >= pJudgePrm->nCornerJugeDepth || (double)pDefect->m_Defect.s_DefectRect.Height()*dScanRes >= pJudgePrm->nCornerJugeLength) return TRUE; } } return FALSE; } BOOL CPostProcess::CreateDataDirectory() { m_strResultFolder.Format(_T("%sDATA_RESULT"),PATH_INSPECTION_DATA); CheckDirectory(m_strResultFolder); m_strDefectFolder.Format(_T("%sIMG_DEFECT\\%s\\%s\\%s"),PATH_INSPECTION_DATA,g_pBase->m_strLoadingDay,g_pBase->m_strHPanelID,g_pBase->m_strLoadingTime); CheckDirectory(m_strDefectFolder); m_strSplineFolder.Format(_T("%sSPLINE\\%s\\%s\\%s"),PATH_INSPECTION_DATA,g_pBase->m_strLoadingDay,g_pBase->m_strHPanelID,g_pBase->m_strLoadingTime); CheckDirectory(m_strSplineFolder); m_strFullImgeFolder.Format(_T("%sIMG_FULL\\%s\\%s\\%s"),PATH_INSPECTION_DATA,g_pBase->m_strLoadingDay,g_pBase->m_strHPanelID,g_pBase->m_strLoadingTime); CheckDirectory(m_strFullImgeFolder); m_strImageMarkFolder.Format(_T("%sIMG_MARK\\%s\\%s\\%s"),PATH_INSPECTION_DATA,g_pBase->m_strLoadingDay,g_pBase->m_strHPanelID,g_pBase->m_strLoadingTime); CheckDirectory(m_strImageMarkFolder); return TRUE; } BOOL CPostProcess::CheckDirectory(const TCHAR szPathName[]) { CFileFind finder; CString strTemp; CString strDir = szPathName; int nPos; if (finder.FindFile(szPathName) == FALSE) { nPos = strDir.Find(_T("\\")); nPos = strDir.Find(_T("\\"), nPos+1); while(nPos > 0) { strTemp = strDir.Mid(0,nPos); if(0 < strTemp.GetLength()) { if(CString("\\") == strTemp.GetAt(strTemp.GetLength()-1)) { nPos = strDir.Find(_T("\\"), nPos+1); continue; } } if (finder.FindFile(strTemp) == FALSE) { if(::CreateDirectory(strTemp, NULL) == FALSE) { strTemp.Format(_T("[%s]Folder Create Fail. "), szPathName); //LogMessage(strTemp, 2); return FALSE; } } nPos = strDir.Find(_T("\\"), nPos+1); } } return TRUE; } CString CPostProcess::GetResultPath(EM_RESULT_PATH ePath) { CString str; switch(ePath) { case EM_RESULT_FILE: str=m_strResultFolder; break; case EM_RESULT_DEFECT: str=m_strDefectFolder; break; case EM_RESULT_SPLINE: str=m_strSplineFolder; break; case EM_RESULT_FULL: str=m_strFullImgeFolder; break; case EM_RESULT_MARK: str=m_strImageMarkFolder; break; } return str; }