#include "StdAfx.h" #include "InspectCamera.h" #include "GlassRecipe.h" #include "HardwareSettings.h" #include "MulticamControl.h" #include "RadientControl.h" #include "Glass_Data.h" #include "IncludeSDK.h" #define BOUNDARY_GLASSSTARTWIDTH 160 #define BOUNDARYHORIZ_LEVELDIST 8 // 荐乞 版拌急阑 茫阑 锭狼 厚背芭府 #define TIME_WAIT_GLASS_START 1500 #define align_4byte(in) ((in + 3)/4)*4 #define RAD_TO_DEGREE (double)(180.0f / 3.1415926535f) //static double _gLastFinishTime = MININT64; CInspectCamera::CInspectCamera(int iCam) { m_iCamera = iCam; m_pThreadControl = new CThreadControl(); m_pRecipe = NULL; m_pHardparm = NULL; m_pGrabber = NULL; m_pGlassData = NULL; m_pII2S = NULL; m_bExitThread = FALSE; m_pSplineModel = NULL; m_hWndViewScan = NULL; int i; for(i=0;i *pSetProfileAlignMap = &m_mapAlignProfile[i]; DeleteCADAlign(pSetProfileAlignMap); } for(i=0;iReleaseThreadControl(); Sleep(100); delete m_pThreadControl; m_pThreadControl = NULL; } } int CInspectCamera::InitInspect(int nThread,int maxDefect) { m_pThreadControl->InitThreadControl(nThread,this); m_pDefectControl->InitDefectStorage(maxDefect); return 1; } int CInspectCamera::ReInitThread(int nThread) { m_pThreadControl->ReleaseThreadControl(); Sleep(100); m_pThreadControl->InitThreadControl(nThread, this); return 1; } BOOL CInspectCamera::ScanStart(int iScan) { m_iScan = iScan; m_iSideLine[iScan] = 0; m_bExitThread = FALSE; ResetFrameFinish(iScan); ScanStartThread(); return TRUE; } BOOL CInspectCamera::ScanStartThread() { if(m_pThreadControl == NULL) return FALSE; int nThread = m_pThreadControl->StartThreadControl(); if(nThread <= 0) { return FALSE; } return TRUE; } BOOL CInspectCamera::GetCheckFrame(stFrameIndex stFrame) { CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return FALSE; DimensionDir emDim = pCamera->m_eDimension; INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pEdgeInfo == NULL) return FALSE; int nFrameHeight = pCamera->m_FrameSize.cy; int nStartFrame = pEdgeInfo->nGlassStartLine/nFrameHeight; if(nStartFrame > stFrame.nFrameIdx) return TRUE; return FALSE; } ERR_FIND_GLASSSTARTLINE CInspectCamera::IsGlassStartLine(int iThread,int &iScan) { iScan = m_iScan; if(m_pGrabber->GetGrabFrameCount() < 4) { Sleep(0); return ERR_FINDGLASSSTART_FAIL; } stFrameIndex stFrame; stFrame.nScanIdx = m_iScan; if(m_bFindGlassStart[stFrame.nScanIdx] == TRUE) return ERR_FINDGLASSSTART_SUCCESS; if(iThread != 0) return ERR_FINDGLASSSTART_FAIL; DimensionDir emDim = GetDimension(stFrame.nScanIdx); iScan = stFrame.nScanIdx; if(FindGlassStartLine(emDim,stFrame) == TRUE) { ScanRegionSet(stFrame); m_bFindGlassStart[stFrame.nScanIdx] = TRUE; m_MsgJob.nState = 1; m_MsgJob.nDispLine = 0; m_MsgJob.nSide = (int)emDim; INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pEdgeInfo != NULL) m_MsgJob.nDispLine = pEdgeInfo->nGlassStartLine; ::SendMessage(m_hWndViewScan, WM_POCESS_STATUS,(WPARAM)&m_MsgJob, (int)emDim); return ERR_FINDGLASSSTART_SUCCESS; } else { return ERR_FINDGLASSSTART_FIND_FAIL; } return ERR_FINDGLASSSTART_FAIL; } BOOL CInspectCamera::OnThreadRun(int iThread, CInspectThread *pInspectThread) { if(m_pRecipe == NULL) return FALSE; if(m_pHardparm == NULL) return FALSE; if(m_pGlassData == NULL) return FALSE; if(m_pGrabber == NULL) return FALSE; while(TRUE) { if(GetCheckExit() == TRUE) { g_pLog->DisplayMessage(_T("cam %d, Thread %d Thread Exit"),m_iCamera,iThread); break; } int iRetScan; switch(IsGlassStartLine(iThread,iRetScan)) { case ERR_FINDGLASSSTART_FAIL: continue; break; case ERR_FINDGLASSSTART_FIND_FAIL: m_pGrabber->GrabScanStop(); m_pGrabber->ClearGrabIdx(); m_bExitThread = TRUE; g_pLog->DisplayMessage(_T("%s Process Find Glass StartLine Fail"),PANEL_SIDE[GetDimension(iRetScan)]); int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,m_iScan,nFrameWidth,nFrameHeight); m_MsgJob.nState = 1; m_MsgJob.nDispLine = 0; m_MsgJob.nSide = (int)GetDimension(m_iScan); ::SendMessage(m_hWndViewScan, WM_POCESS_STATUS,(WPARAM)&m_MsgJob, (int)m_MsgJob.nSide); continue; break; } stFrameIndex stFrame = m_pGrabber->GetGrabFrame(); if(stFrame.nScanIdx < 0 || stFrame.nFrameIdx < 0) { Sleep(0); continue; } DimensionDir emDim = GetDimension(stFrame.nScanIdx); if(CheckStartLineFrame(emDim,stFrame.nFrameIdx) == FALSE) continue; //if(emDim == DIMENSION_A) // g_pLog->DisplayMessage(_T("%s, %d Process Frame : %d,%d"),PANEL_SIDE[emDim],iThread, stFrame.nScanIdx,stFrame.nFrameIdx); int iFindLine = FindLeftLine(iThread,emDim,stFrame); BOOL bFindEndLine = FindEndLine(iThread,emDim,stFrame); FindAlignProcess(iThread,emDim,stFrame.nScanIdx,stFrame.nFrameIdx); FindThickness(iThread,emDim,stFrame,iFindLine); //FindSpline(iThread,emDim,stFrame,iFindLine); FindSpline_New(iThread,emDim,stFrame,iFindLine); FindHoleInspection(iThread,emDim,stFrame,iFindLine); BOOL bProfile = FindProfileProcess(iThread,emDim,stFrame,iFindLine); if(m_bProfileFind[stFrame.nScanIdx] == TRUE && m_nProfileFindFrameIdx[stFrame.nScanIdx] == stFrame.nFrameIdx) bProfile = TRUE; if(bFindEndLine == TRUE) SetFrameFinishEndFrame(stFrame.nScanIdx, stFrame.nFrameIdx); SetFrameFinish(stFrame.nScanIdx, stFrame.nFrameIdx); BOOL bEnd = CheckThreadEnd(iThread,stFrame,bProfile); if(stFrame.nFrameIdx % 10 == 0 && bEnd == FALSE) { int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); m_MsgJob.nState = 1; m_MsgJob.nDispLine = stFrame.nFrameIdx*nFrameHeight; m_MsgJob.nSide = (int)emDim; //g_pLog->DisplayMessage(_T("%s Display - %d"),PANEL_SIDE[emDim],stFrame.nFrameIdx); ::SendMessage(m_hWndViewScan, WM_POCESS_STATUS,(WPARAM)&m_MsgJob, (int)emDim); } Sleep(0); } g_pLog->DisplayMessage(_T("Camera %d-%d Thread End"),m_iCamera,iThread); return TRUE; } BOOL CInspectCamera::FindProfileProcess(int iThread,DimensionDir emDim,stFrameIndex stFrame,int iFindLine) { if(CheckProfile(iThread,emDim,stFrame) == FALSE) return FALSE; INS_EDGE_RESULT_INFO *pResInfo = NULL; if(m_pGlassData != NULL) pResInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pResInfo == NULL) return FALSE; DIT_RESULT ret= DIT_CONV_SUCCESS; CvPoint2D32f ptAlign[3]; ptAlign[0].x = ptAlign[0].y = 0; ptAlign[1].x = ptAlign[1].y = 0; ptAlign[2].x = ptAlign[2].y = 0; if(pResInfo->bMark[0] == FALSE || pResInfo->bMark[1] == FALSE) { DWORD dwTact = GetTickCount(); do { if(pResInfo == NULL) break; if(pResInfo->bMark[0] == TRUE && pResInfo->bMark[1] == TRUE) break; if(GetTickCount()-dwTact >= 3000) { g_pLog->DisplayMessage(_T("%s Profile Align Wait timeout : %d=%d"),PANEL_SIDE[emDim],pResInfo->bMark[0],pResInfo->bMark[1]); break; } Sleep(0); } while (1); if(pResInfo->bMark[0] == FALSE || pResInfo->bMark[1] == FALSE) { g_pLog->DisplayMessage(_T("%s Start Find Profile Process Fail :Frame %d : (Align Mark Find Fail..)"),PANEL_SIDE[emDim],stFrame.nFrameIdx); return FALSE; } } ptAlign[0] = pResInfo->ptMark[0]; ptAlign[1] = pResInfo->ptMark[1]; ptAlign[2] = pResInfo->ptMark[2]; if(ptAlign[0].x<=0 || ptAlign[0].y <= 0 || ptAlign[1].x <= 0 || ptAlign[1].y <=0) { return FALSE; } CalculateStageTheta(emDim,stFrame.nScanIdx, ptAlign); g_pLog->DisplayMessage(_T("%s Start Find Profile Process : Thread %d,Frame %d"),PANEL_SIDE[emDim],iThread, stFrame.nFrameIdx); vector< pair > vecProfile; CProfileCADProc *pProfileControl = GetProfileControl(stFrame.nScanIdx,iThread); MakeProfilePosition(emDim,stFrame,pProfileControl,vecProfile, ptAlign); CalculateProfileChamfer(emDim,stFrame,pProfileControl,vecProfile,ptAlign[0]); vector< pair >::iterator it; STU_PROFILE_POSITION *pProfile; for(it=vecProfile.begin();it!=vecProfile.end();it++) { pProfile = it->second; if(pProfile != NULL) delete pProfile; pProfile = NULL; } vecProfile.clear(); vector< pair > vecSplineProfile; RCutProfileProcess(emDim,stFrame,pProfileControl,vecSplineProfile,ptAlign); vector< pair >::iterator itSpline; STU_SPLINE_PROFILE_POSITION *pSpline; for(itSpline=vecSplineProfile.begin();itSpline!=vecSplineProfile.end();itSpline++) { pSpline = itSpline->second; if(pSpline != NULL) delete pSpline; pSpline = NULL; } vecSplineProfile.clear(); double dConv,dScan; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return FALSE; dConv = pCamera->m_dConvResolution[g_pBase->m_nStageNo]; dScan = pCamera->m_dScanResolution[g_pBase->m_nStageNo]; if(dConv <= 0 || dScan <= 0) return FALSE; double dDx = abs(ptAlign[0].x-ptAlign[1].x)*dConv; double dDy = abs(ptAlign[0].y-ptAlign[1].y)*dScan; double dAlignDist = sqrtf(float(dDx *dDx + dDy*dDy)); double dPixelDx = abs(ptAlign[0].x-ptAlign[1].x); double dPixelDy = abs(ptAlign[0].y-ptAlign[1].y); double dPixelAlignDist = sqrtf(float(dPixelDx *dPixelDx + dPixelDy*dPixelDy)); g_pLog->DisplayMessage(_T("%s Glass Align First[%.0f,%.0f],Align Second[%.0f,%.0f] = Distance %.0f,%.3fmm"),PANEL_SIDE[emDim],ptAlign[0].x,ptAlign[0].y,ptAlign[1].x,ptAlign[1].y,dPixelAlignDist,dAlignDist/1000.); g_pLog->DisplayMessage(_T("%s Find Profile Process End :Frame %d"),PANEL_SIDE[emDim],stFrame.nFrameIdx); return TRUE; } BOOL CInspectCamera::IsRCutProfileProcess(DimensionDir emDim) { switch(emDim) { case DIMENSION_A: case DIMENSION_A_DN: case DIMENSION_B: case DIMENSION_B_DN: case DIMENSION_D: case DIMENSION_D_DN: return TRUE; } return FALSE; } BOOL CInspectCamera::CheckAlignMark(DimensionDir emDim,int &nIndex) { INS_EDGE_RESULT_INFO *pResInfo = NULL; BOOL bRet = FALSE; nIndex = -1; BOOL bUseAlignKey = FALSE; CProfileInspect_Prm *pPrm = m_pRecipe->GetProfileParm(); if(pPrm != NULL) bUseAlignKey = pPrm->m_bUseOriginAlignKey[emDim]; if(m_pGlassData != NULL) { pResInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pResInfo != NULL) { switch(emDim) { case DIMENSION_A: case DIMENSION_A_DN: bRet = pResInfo->bMark[0]; nIndex = 0; break; case DIMENSION_B: case DIMENSION_B_DN: case DIMENSION_D: case DIMENSION_D_DN: if(bUseAlignKey == FALSE) { bRet = pResInfo->bMark[2]; // A父 Align 0, B,D绰 辑宏付目 nIndex = 2; } else { bRet = TRUE; } break; } } } return bRet; } BOOL CInspectCamera::ReFindAlignMark(DimensionDir emDim,stFrameIndex stFrame,int iPos) { int iFindFrame; int iEdgeFrame; int iGlassEdge = 0; INS_EDGE_RESULT_INFO *pResInfo = NULL; pResInfo = m_pGlassData->GetEdgeResultInfo(emDim); CRect rectAlign = pResInfo->rtMark[iPos]; if(rectAlign.top <= 0) return FALSE; int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); iFindFrame = rectAlign.bottom/nFrameHeight; iEdgeFrame = iFindFrame + 5; iGlassEdge = pResInfo->nGrindframe[0][iEdgeFrame]; if(iGlassEdge <= 0 || iGlassEdge >= nFrameWidth) { for(int i=iEdgeFrame-1;i>=pResInfo->nGlassStartFrame;i--) { if(pResInfo->nGrindframe[0][i] > 0 && pResInfo->nGrindframe[0][i] < nFrameWidth) { iGlassEdge = pResInfo->nGrindframe[0][i]; break; } } } CString strPos = _T("TOP"); CString strRes = _T("Fail"); switch(iPos) { case 1: strPos = _T("BOT"); break; case 2: strPos = _T("SUB"); break; } rectAlign.OffsetRect(iGlassEdge,0); g_pLog->DisplayMessage(_T("%s Re Align Mark Find[%d] %d,%d,%d,%d"),PANEL_SIDE[emDim],iPos,rectAlign.left,rectAlign.top,rectAlign.right,rectAlign.bottom); double dRate = 0.5; TEMP_RESULT matchResut; CRect rectIns = rectAlign; CALIGNMARK_PARM *pAlignPrm = m_pRecipe->GetAlignMarkPrm(emDim); CEdgeProc insProc; CSISBuffer frameBuffer(m_pGrabber->GetFrameHeaderLine(stFrame.nScanIdx,rectAlign.top), nFrameWidth, rectAlign.Height()); if(!frameBuffer.IsValidBuffer()) { return FALSE; } rectIns.OffsetRect(0,-rectIns.top); matchResut.fRat = 0.; matchResut.pt.x = matchResut.pt.y = 0; for(int iMarkPos=0;iMarkPosIpMarkImg[iPos][iMarkPos] == NULL) continue; CRect rtFind = rectIns; if(rtFind.Width() <= pAlignPrm->IpMarkImg[iPos][iMarkPos]->width) rtFind.right = rtFind.left + pAlignPrm->IpMarkImg[iPos][iMarkPos]->width + 10; if(rtFind.Height() <= pAlignPrm->IpMarkImg[iPos][iMarkPos]->height) rtFind.bottom = rtFind.top + pAlignPrm->IpMarkImg[iPos][iMarkPos]->height + 10; insProc.CvFindMark(frameBuffer,pAlignPrm->IpMarkImg[iPos][iMarkPos],rectIns,matchResut,dRate); if(matchResut.fRat*100. > pAlignPrm->m_dMarkMatchingRate[iPos][iMarkPos]) { if(pResInfo != NULL) { pResInfo->bMark[iPos] = TRUE; pResInfo->ptMark[iPos].x = matchResut.pt.x; pResInfo->ptMark[iPos].y = matchResut.pt.y + rectAlign.top; pResInfo->nMarkToEdge_X[iPos] = (int)(pResInfo->ptMark[iPos].x - m_iSideLine[stFrame.nScanIdx]); pResInfo->nMarkToEdge_Y[iPos] = (int)(pResInfo->ptMark[iPos].y - pResInfo->nGlassStartLine ); } g_pLog->DisplayMessage(_T("%s Re Align Mark Find[%d][%d] Success x %.2f, y %.2f"),PANEL_SIDE[emDim],iPos,iMarkPos,matchResut.pt.x, matchResut.pt.y + rectAlign.top); strRes = _T("Success"); break; } } if(pResInfo != NULL && pResInfo->bMark[iPos] == FALSE) { pResInfo->bMark[iPos] = TRUE; pResInfo->ptMark[iPos].x = (float)(m_iSideLine[stFrame.nScanIdx] + m_dMark2MarkX[stFrame.nScanIdx][iPos]); pResInfo->ptMark[iPos].y = (float)(pResInfo->nGlassStartLine + m_dMark2MarkY[stFrame.nScanIdx][iPos]); g_pLog->DisplayMessage(_T("%s Re Align Mark Find[%d] Fail x %.2f, y %.2f"),PANEL_SIDE[emDim],iPos,m_iSideLine[stFrame.nScanIdx] + m_dMark2MarkX[stFrame.nScanIdx][iPos], pResInfo->nGlassStartLine + m_dMark2MarkY[stFrame.nScanIdx][iPos]); } #if MARKIMG_NOTSAVE CString strFile; CvRect saveRect = cvRect((int)matchResut.pt.x - rectAlign.Width() / 2, (int)(matchResut.pt.y + rectAlign.top) - rectAlign.Height() / 2, rectAlign.Width(), rectAlign.Height()); strFile.Format(_T("%sIMG_MARK\\%s\\%s\\%s\\Align_%s_%s_%s.jpg"), PATH_INSPECTION_DATA, g_pBase->m_strLoadingDay, g_pBase->m_strHPanelID, g_pBase->m_strLoadingTime, strPos, PANEL_SIDE[emDim], strRes); SaveCvAlignImage(saveRect, strFile, stFrame.nScanIdx, nFrameWidth, nFrameHeight); #endif // 0 return TRUE; } int CInspectCamera::GetRCutProfileIndexTheta(DimensionDir emDim,stFrameIndex stFrame,vector< pair > &vecProfile,CvPoint2D32f *ptAlign) { int nRcpIndex[2],nRcpSize[2]; int nProfilePosCount; nProfilePosCount = GetRCutProfileIndex(emDim,nRcpIndex,nRcpSize); if(nProfilePosCount <= 0) return 0; return GetRCutProfileTheta2Vector(nProfilePosCount,emDim,stFrame,vecProfile,nRcpIndex,nRcpSize,ptAlign); } int CInspectCamera::GetRCutProfileTheta2Vector(int nPosCnt,DimensionDir eDimension,stFrameIndex stFrame,vector< pair > &vecProfile,int *nRcpIndex,INT *nRcpSize,CvPoint2D32f *ptAlign) { CProfileInspect_Prm *pPrm = m_pRecipe->GetProfileParm(); int iPos,iIndex,iTheta; double dDefaultTheta = RCUT_PROFILE_CALCULATE_THETA; CvPoint2D32f ptCenter; for(iPos=0;iPosiCam = m_iCamera; pPfPositon->iSide = eDimension; pPfPositon->iScan = stFrame.nScanIdx; pPfPositon->iIndex = iIndex; pPfPositon->iPos = iPos; pPfPositon->iLoop = iTheta; pPfPositon->pSettings = &pPrm->m_stProfile[iIndex]; pPfPositon->dRadius = pPfPositon->pSettings->radius; double dCalTheta = -1*atan(static_cast(ptAlign[1].x - ptAlign[0].x) / static_cast((ptAlign[1].y - ptAlign[0].y))); dCalTheta = dCalTheta*RAD_TO_DEGREE; if(m_pRecipe->GetGlassInfo()->m_bPanelReverse == FALSE) { switch(eDimension) { case DIMENSION_A: case DIMENSION_A_DN: if(iPos == 0) pPfPositon->dTheta = 90-(iTheta*dDefaultTheta); else pPfPositon->dTheta = -1*(iTheta*dDefaultTheta-dCalTheta); break; case DIMENSION_B: case DIMENSION_B_DN: pPfPositon->dTheta = -1*(iTheta*dDefaultTheta-dCalTheta); break; case DIMENSION_D: case DIMENSION_D_DN: pPfPositon->dTheta = 270+(iTheta*dDefaultTheta+dCalTheta); break; } } else { switch(eDimension) { case DIMENSION_A: case DIMENSION_A_DN: if(iPos == 0) pPfPositon->dTheta = (iTheta*dDefaultTheta-dCalTheta); else pPfPositon->dTheta = -1*(90-(iTheta*dDefaultTheta)); break; case DIMENSION_B: case DIMENSION_B_DN: pPfPositon->dTheta = 270+(iTheta*dDefaultTheta+dCalTheta); break; case DIMENSION_D: case DIMENSION_D_DN: pPfPositon->dTheta = -1*(iTheta*dDefaultTheta-dCalTheta); break; } } if(iTheta == 0) // Center绰 贸澜波 窍唱肺 茄促. { GetRCutCalculateCenterPosition(eDimension,stFrame,pPfPositon,ptAlign,iPos); ptCenter.x = (float)pPfPositon->ptCenterX; ptCenter.y = (float)pPfPositon->ptCenterY; } else { pPfPositon->ptCenterX = ptCenter.x; pPfPositon->ptCenterY = ptCenter.y; } vecProfile.push_back(make_pair((double)pPfPositon->iIndex, pPfPositon)); } } return (int)vecProfile.size(); } void CInspectCamera::GetRCutCalculateCenterPosition(DimensionDir eDimension,stFrameIndex stFrame,pSTU_SPLINE_PROFILE_POSITION pPfPositon,CvPoint2D32f *ptAlign,int iPos) { double dConv,dScan; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return; dConv = pCamera->m_dConvResolution[g_pBase->m_nStageNo]; dScan = pCamera->m_dScanResolution[g_pBase->m_nStageNo]; if(dConv <= 0 || dScan <= 0) return; CEdgeProc EdgeProc; BOOL bUseAlignKey; bUseAlignKey = FALSE; CProfileInspect_Prm *pPrm = m_pRecipe->GetProfileParm(); if(pPrm != NULL) { bUseAlignKey = pPrm->m_bUseOriginAlignKey[eDimension]; } if(m_pRecipe->GetGlassInfo()->m_bPanelReverse == FALSE) { switch(eDimension) { case DIMENSION_A: case DIMENSION_A_DN: if(iPos == 0) // 窍何 align捞 扁霖捞骨肺 惑何绰 拌魂摹肺 茄促. { double dGlassLongSize = (m_pRecipe->GetGlassInfo()->m_nGlassSizeX); double dCalValue; int nCalAlignPos = (int)(dGlassLongSize-LONG_SIDE_INSPECTION_POINT_OFFSET); int nSetPos = (int)(LONG_SIDE_INSPECTION_POINT_OFFSET+pPfPositon->pSettings->y); double dMove = nCalAlignPos-nSetPos; dCalValue = dMove/dScan; pPfPositon->ptCenterX = (pPfPositon->pSettings->x/dConv) + ptAlign[0].x; pPfPositon->ptCenterY = ptAlign[0].y + dCalValue; pPfPositon->ptCenterX += (g_pStatus->GetProfileOption()->m_nProfileOff_A_DN_X/dConv); pPfPositon->ptCenterY += (g_pStatus->GetProfileOption()->m_nProfileOff_A_DN_Y/dScan); } else // 窍何啊 扁霖. shift父 矫糯 { pPfPositon->ptCenterX = ptAlign[1].x+pPfPositon->pSettings->x/dConv; pPfPositon->ptCenterY = ptAlign[1].y-pPfPositon->pSettings->y/dScan; pPfPositon->ptCenterX += (g_pStatus->GetProfileOption()->m_nProfileOff_A_X/dConv); pPfPositon->ptCenterY += (g_pStatus->GetProfileOption()->m_nProfileOff_A_Y/dScan); } break; case DIMENSION_B: case DIMENSION_B_DN: // C,D绰 谅钎啊 馆措, B绰 C搁栏肺 焊搁 窍何 , 75锅 { if(bUseAlignKey == FALSE) { CvPoint2D32f ptMove; pPfPositon->ptRealCenter.x = (float)(pPfPositon->pSettings->y * ptAlign[2].x*dConv); pPfPositon->ptRealCenter.y = (float)(pPfPositon->pSettings->x + ptAlign[2].y*dScan); ptMove.x = (float)(pPfPositon->pSettings->y/dConv); ptMove.y = (float)(pPfPositon->pSettings->x/dScan); double dCalTheta = -1*atan(static_cast(ptAlign[1].x - ptAlign[0].x) / static_cast((ptAlign[1].y - ptAlign[0].y))); // C,D绰 3锅掳 倔扼牢虐肺 可悸 焊沥窃. //EdgeProc.RotatePoint(ptMove,pPfPositon->ptCenterX,pPfPositon->ptCenterY,ptAlign[2],sin(dCalTheta),cos(dCalTheta)); EdgeProc.RotatePoint(ptMove,pPfPositon->ptCenterX,pPfPositon->ptCenterY,sin(dCalTheta),cos(dCalTheta)); pPfPositon->ptCenterX += ptAlign[2].x; pPfPositon->ptCenterY += ptAlign[2].y; g_pLog->DisplayMessage(_T("%s Profile, 1 Align x %.3f,y %.3f, 2 Align x %.3f,y %.3f, 3 Align x %.3f,y %.3f"),PANEL_SIDE[eDimension],ptAlign[0].x,ptAlign[0].y,ptAlign[1].x,ptAlign[1].y,ptAlign[2].x,ptAlign[2].y); g_pLog->DisplayMessage(_T("%s Profile Theta %.5f move x %.3f,y %.3f, Center x %.3f,y %.3f"),PANEL_SIDE[eDimension],dCalTheta,ptMove.x,ptMove.y,pPfPositon->ptCenterX,pPfPositon->ptCenterY); pPfPositon->ptCenterX += (g_pStatus->GetProfileOption()->m_nProfileOff_C_DN_X/dConv); // user offset篮 扁粮 C窍螟 可悸 利侩 pPfPositon->ptCenterY += (g_pStatus->GetProfileOption()->m_nProfileOff_C_DN_Y/dScan); } else { double AlignX,AlignY; double dOrgOffsetX,dOrgOffsetY; dOrgOffsetX = (double)pPrm->m_nOriginAlignKeyOffsetDwX[eDimension]/dConv; dOrgOffsetY = (double)pPrm->m_nOriginAlignKeyOffsetDwY[eDimension]/dScan; AlignX = ptAlign[1].x; AlignY = ptAlign[1].y; pPfPositon->ptCenterX = AlignX + dOrgOffsetX; pPfPositon->ptCenterY = AlignY + dOrgOffsetY; g_pLog->DisplayMessage(_T("Use Key %s Profile, 1 Align x %.3f,y %.3f, 2 Align x %.3f,y %.3f"),PANEL_SIDE[eDimension],ptAlign[0].x,ptAlign[0].y,ptAlign[1].x,ptAlign[1].y); g_pLog->DisplayMessage(_T("Use Key %s Profile offset x %.3f,y %.3f, Center x %.3f,y %.3f"),PANEL_SIDE[eDimension],dOrgOffsetX,dOrgOffsetY,pPfPositon->ptCenterX,pPfPositon->ptCenterY); } } break; case DIMENSION_D: case DIMENSION_D_DN: // C,D绰 谅钎啊 馆措, D绰 C搁栏肺 焊搁 惑何 , 70锅 { if(bUseAlignKey == FALSE) { CvPoint2D32f ptMove; double dGlassLongSize = (m_pRecipe->GetGlassInfo()->m_nGlassSizeX); int nCalAlignPos = (int)(dGlassLongSize-LONG_SIDE_INSPECTION_POINT_OFFSET); int nSetPos = (int)(LONG_SIDE_INSPECTION_POINT_OFFSET+pPfPositon->pSettings->y); double dMove = nCalAlignPos-nSetPos; ptMove.x = (float)(dMove/dConv); ptMove.y = (float)(pPfPositon->pSettings->x/dScan); double dCalTheta = -1*atan(static_cast(ptAlign[1].x - ptAlign[0].x) / static_cast((ptAlign[1].y - ptAlign[0].y))); // C,D绰 3锅掳 倔扼牢虐肺 可悸 焊沥窃. //EdgeProc.RotatePoint(ptMove,pPfPositon->ptCenterX,pPfPositon->ptCenterY,ptAlign[2],sin(dCalTheta),cos(dCalTheta)); EdgeProc.RotatePoint(ptMove,pPfPositon->ptCenterX,pPfPositon->ptCenterY,sin(dCalTheta),cos(dCalTheta)); pPfPositon->ptCenterX += ptAlign[2].x; pPfPositon->ptCenterY += ptAlign[2].y; g_pLog->DisplayMessage(_T("%s Profile, 1 Align x %.3f,y %.3f, 2 Align x %.3f,y %.3f, 3 Align x %.3f,y %.3f"),PANEL_SIDE[eDimension],ptAlign[0].x,ptAlign[0].y,ptAlign[1].x,ptAlign[1].y,ptAlign[2].x,ptAlign[2].y); g_pLog->DisplayMessage(_T("%s Profile Theta %.5f move x %.3f,y %.3f, Center x %.3f,y %.3f"),PANEL_SIDE[eDimension],dCalTheta,ptMove.x,ptMove.y,pPfPositon->ptCenterX,pPfPositon->ptCenterY); pPfPositon->ptCenterX += (g_pStatus->GetProfileOption()->m_nProfileOff_C_X/dConv); // user offset篮 扁粮 C惑螟 可悸 利侩 pPfPositon->ptCenterY += (g_pStatus->GetProfileOption()->m_nProfileOff_C_Y/dScan); } else { double AlignX,AlignY; double dOrgOffsetX,dOrgOffsetY; dOrgOffsetX = (double)pPrm->m_nOriginAlignKeyOffsetDwX[eDimension]/dConv; dOrgOffsetY = (double)pPrm->m_nOriginAlignKeyOffsetDwY[eDimension]/dScan; AlignX = ptAlign[1].x; AlignY = ptAlign[1].y; pPfPositon->ptCenterX = AlignX + dOrgOffsetX; pPfPositon->ptCenterY = AlignY + dOrgOffsetY; g_pLog->DisplayMessage(_T("Use Key %s Profile, 1 Align x %.3f,y %.3f, 2 Align x %.3f,y %.3f"),PANEL_SIDE[eDimension],ptAlign[0].x,ptAlign[0].y,ptAlign[1].x,ptAlign[1].y); g_pLog->DisplayMessage(_T("Use Key %s Profile offset x %.3f,y %.3f, Center x %.3f,y %.3f"),PANEL_SIDE[eDimension],dOrgOffsetX,dOrgOffsetY,pPfPositon->ptCenterX,pPfPositon->ptCenterY); } } break; } } else { switch(eDimension) { case DIMENSION_A: case DIMENSION_A_DN: if(iPos == 0) // 惑何啊 扁霖. shift父 矫糯 { pPfPositon->ptCenterX = ptAlign[0].x+pPfPositon->pSettings->x/dConv; pPfPositon->ptCenterY = ptAlign[0].y+pPfPositon->pSettings->y/dScan; pPfPositon->ptCenterX += (g_pStatus->GetProfileOption()->m_nProfileOff_A_X/dConv); pPfPositon->ptCenterY += (g_pStatus->GetProfileOption()->m_nProfileOff_A_Y/dScan); } else // 惑何 align捞 扁霖捞骨肺 惑何绰 拌魂摹肺 茄促. { double dGlassLongSize = (m_pRecipe->GetGlassInfo()->m_nGlassSizeX); double dCalValue; int nCalAlignPos = (int)(dGlassLongSize-LONG_SIDE_INSPECTION_POINT_OFFSET); int nSetPos = (int)(LONG_SIDE_INSPECTION_POINT_OFFSET+pPfPositon->pSettings->y); double dMove = nSetPos-nCalAlignPos; dCalValue = dMove/dScan; pPfPositon->ptCenterX = (pPfPositon->pSettings->x/dConv) + ptAlign[1].x; pPfPositon->ptCenterY = ptAlign[1].y + dCalValue; pPfPositon->ptCenterX += (g_pStatus->GetProfileOption()->m_nProfileOff_A_DN_X/dConv); pPfPositon->ptCenterY += (g_pStatus->GetProfileOption()->m_nProfileOff_A_DN_Y/dScan); } break; case DIMENSION_B: case DIMENSION_B_DN: // C,D绰 谅钎啊 馆措, B绰 C搁栏肺 焊搁 窍何 , 75锅 { CvPoint2D32f ptMove; double dGlassLongSize = (m_pRecipe->GetGlassInfo()->m_nGlassSizeX); int nCalAlignPos = (int)(dGlassLongSize-LONG_SIDE_INSPECTION_POINT_OFFSET); int nSetPos = (int)(LONG_SIDE_INSPECTION_POINT_OFFSET+pPfPositon->pSettings->y); double dMove = nCalAlignPos-nSetPos; ptMove.x = (float)(dMove/dConv); ptMove.y = (float)(pPfPositon->pSettings->x/dScan); double dCalTheta = -1*atan(static_cast(ptAlign[1].x - ptAlign[0].x) / static_cast((ptAlign[1].y - ptAlign[0].y))); // C,D绰 3锅掳 倔扼牢虐肺 可悸 焊沥窃. //EdgeProc.RotatePoint(ptMove,pPfPositon->ptCenterX,pPfPositon->ptCenterY,ptAlign[2],sin(dCalTheta),cos(dCalTheta)); EdgeProc.RotatePoint(ptMove,pPfPositon->ptCenterX,pPfPositon->ptCenterY,sin(dCalTheta),cos(dCalTheta)); pPfPositon->ptCenterX += ptAlign[2].x; pPfPositon->ptCenterY += ptAlign[2].y; g_pLog->DisplayMessage(_T("%s Profile, 1 Align x %.3f,y %.3f, 2 Align x %.3f,y %.3f, 3 Align x %.3f,y %.3f"),PANEL_SIDE[eDimension],ptAlign[0].x,ptAlign[0].y,ptAlign[1].x,ptAlign[1].y,ptAlign[2].x,ptAlign[2].y); g_pLog->DisplayMessage(_T("%s Profile Theta %.5f move x %.3f,y %.3f, Center x %.3f,y %.3f"),PANEL_SIDE[eDimension],dCalTheta,ptMove.x,ptMove.y,pPfPositon->ptCenterX,pPfPositon->ptCenterY); pPfPositon->ptCenterX += (g_pStatus->GetProfileOption()->m_nProfileOff_C_X/dConv); // user offset篮 扁粮 C惑螟 可悸 利侩 pPfPositon->ptCenterY += (g_pStatus->GetProfileOption()->m_nProfileOff_C_Y/dScan); } break; case DIMENSION_D: case DIMENSION_D_DN: // C,D绰 谅钎啊 馆措, D绰 C搁栏肺 焊搁 惑何 , 70锅 { CvPoint2D32f ptMove; pPfPositon->ptRealCenter.x = (float)(pPfPositon->pSettings->y * ptAlign[2].x*dConv); pPfPositon->ptRealCenter.y = (float)(pPfPositon->pSettings->x + ptAlign[2].y*dScan); ptMove.x = (float)(pPfPositon->pSettings->y/dConv); ptMove.y = (float)(pPfPositon->pSettings->x/dScan); double dCalTheta = -1*atan(static_cast(ptAlign[1].x - ptAlign[0].x) / static_cast((ptAlign[1].y - ptAlign[0].y))); // C,D绰 3锅掳 倔扼牢虐肺 可悸 焊沥窃. //EdgeProc.RotatePoint(ptMove,pPfPositon->ptCenterX,pPfPositon->ptCenterY,ptAlign[2],sin(dCalTheta),cos(dCalTheta)); EdgeProc.RotatePoint(ptMove,pPfPositon->ptCenterX,pPfPositon->ptCenterY,sin(dCalTheta),cos(dCalTheta)); pPfPositon->ptCenterX += ptAlign[2].x; pPfPositon->ptCenterY += ptAlign[2].y; g_pLog->DisplayMessage(_T("%s Profile, 1 Align x %.3f,y %.3f, 2 Align x %.3f,y %.3f, 3 Align x %.3f,y %.3f"),PANEL_SIDE[eDimension],ptAlign[0].x,ptAlign[0].y,ptAlign[1].x,ptAlign[1].y,ptAlign[2].x,ptAlign[2].y); g_pLog->DisplayMessage(_T("%s Profile Theta %.5f move x %.3f,y %.3f, Center x %.3f,y %.3f"),PANEL_SIDE[eDimension],dCalTheta,ptMove.x,ptMove.y,pPfPositon->ptCenterX,pPfPositon->ptCenterY); pPfPositon->ptCenterX += (g_pStatus->GetProfileOption()->m_nProfileOff_C_DN_X/dConv); // user offset篮 扁粮 C窍螟 可悸 利侩 pPfPositon->ptCenterY += (g_pStatus->GetProfileOption()->m_nProfileOff_C_DN_Y/dScan); } break; } } } int CInspectCamera::GetRCutProfileIndex(DimensionDir eDimension,int *nRcpIndex,int *nRcpSize) { CString strName; int nProfileCnt = 1; // 捞固瘤 Scan 鉴辑措肺 Index甫 罐酒柯促 if(m_pRecipe->GetGlassInfo()->m_bPanelReverse == FALSE) { switch(eDimension) { case DIMENSION_A: case DIMENSION_A_DN: if(m_pGlassData->GetProfileRCutInspectIndex(1,nRcpIndex[0],nRcpSize[0],strName) == FALSE) // A 惑螟 开规氢, Q1(75) ~ Q4(79) return 0; if(m_pGlassData->GetProfileRCutInspectIndex(0,nRcpIndex[1],nRcpSize[1],strName) == FALSE) // A 窍螟 沥规氢, P1(60) ~ P4(64) return 0; nProfileCnt = 2; break; case DIMENSION_B: case DIMENSION_B_DN: case DIMENSION_C: case DIMENSION_C_DN: if(m_pGlassData->GetProfileRCutInspectIndex(2,nRcpIndex[0],nRcpSize[0],strName) == FALSE) // B 窍螟 沥规氢, N1(65) ~ N4(69) return 0; break; case DIMENSION_D: case DIMENSION_D_DN: if(m_pGlassData->GetProfileRCutInspectIndex(3,nRcpIndex[0],nRcpSize[0],strName) == FALSE) // D 窍螟 开规氢, Q1(70) ~ Q4(74) return 0; break; } } else { switch(eDimension) { case DIMENSION_A: case DIMENSION_A_DN: if(m_pGlassData->GetProfileRCutInspectIndex(0,nRcpIndex[0],nRcpSize[0],strName) == FALSE) // A 惑螟 沥规氢, P1(60) ~ P4(64) return 0; if(m_pGlassData->GetProfileRCutInspectIndex(1,nRcpIndex[1],nRcpSize[1],strName) == FALSE) // A 窍螟 开规氢, Q1(75) ~ Q4(79) return 0; nProfileCnt = 2; break; case DIMENSION_B: case DIMENSION_B_DN: if(m_pGlassData->GetProfileRCutInspectIndex(3,nRcpIndex[0],nRcpSize[0],strName) == FALSE) // B 窍螟 开规氢, Q1(70) ~ Q4(74) return 0; break; case DIMENSION_C: case DIMENSION_C_DN: case DIMENSION_D: case DIMENSION_D_DN: if(m_pGlassData->GetProfileRCutInspectIndex(2,nRcpIndex[0],nRcpSize[0],strName) == FALSE) // D 窍螟 沥规氢, N1(65) ~ N4(69) return 0; break; } } return nProfileCnt; } BOOL CInspectCamera::RCutProfileProcess(DimensionDir emDim,stFrameIndex stFrame,CProfileCADProc *pProcess,vector< pair > &vecProfile,CvPoint2D32f *ptAlign) { if(IsRCutProfileProcess(emDim) == FALSE) return TRUE; int nIndex = -1; BOOL bCheckAlign = CheckAlignMark(emDim,nIndex); if(bCheckAlign == FALSE) { ReFindAlignMark(emDim,stFrame,nIndex); if(m_pGlassData != NULL) { INS_EDGE_RESULT_INFO *pResInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pResInfo != NULL) { if(pResInfo->bMark[nIndex] == TRUE) { ptAlign[nIndex] = pResInfo->ptMark[nIndex]; } } } } int nVecCnt = GetRCutProfileIndexTheta(emDim,stFrame,vecProfile,ptAlign); if(nVecCnt <= 0) return FALSE; FindRCutProfilePosition(emDim,stFrame,pProcess,vecProfile,ptAlign); CalculateProfileSpline(emDim,stFrame,vecProfile); return TRUE; } BOOL CInspectCamera::CalculateProfileSpline(DimensionDir eDimension,stFrameIndex stFrame,vector< pair > &vecProfile) { CProfileInspect_Prm *pPrm = m_pRecipe->GetProfileParm(); vector< pair >::iterator it; PROFILE_RCUT_PROCESSDATA RCutResult; PROFILE_RESULT ProfileResult; CHAMFER_RESULT ChamferResult; STU_SPLINE_PROFILE_POSITION *ptProfile; ST_CHAMFER_SETTING *pstChamferPrm; int nStageNo = g_pBase->m_nStageNo; if(nStageNo < 0 || nStageNo > 1) nStageNo = 0; double dConv,dScan; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return FALSE; dConv = pCamera->m_dConvResolution[nStageNo]; dScan = pCamera->m_dScanResolution[nStageNo]; if(dConv <= 0 || dScan <= 0) return FALSE; srand((unsigned int)time(NULL)); for(it=vecProfile.begin();it!=vecProfile.end();it++) { ptProfile = it->second; if(ptProfile == NULL) continue; if(ptProfile->iIndex < 0 || ptProfile->iIndex >= MAX_PROFILE_SETTING_COUNT) continue; ProfileResult.iCam = RCutResult.iCam = ptProfile->iCam; ProfileResult.iFrame = RCutResult.iFrame = stFrame.nFrameIdx; ProfileResult.iIndex = RCutResult.iIndex = ptProfile->iIndex; ProfileResult.iScan = RCutResult.iScan = ptProfile->iScan; ProfileResult.iSide = RCutResult.iSide = ptProfile->iSide; if(pCamera->m_enCamDir == CAMDIR_BOTTOM) { ProfileResult.bUseJudge = pPrm->m_stProfile[ptProfile->iIndex].use; ProfileResult.nJudgeMinus = (int)ROUND(pPrm->m_stProfile[ptProfile->iIndex].minus); ProfileResult.nJudgePlus = (int)ROUND(pPrm->m_stProfile[ptProfile->iIndex].plus); double dReference = pPrm->m_stProfile[ptProfile->iIndex].radius; if(g_pStatus->GetProfileOption()->m_dProfileRadius[nStageNo][ptProfile->iIndex] > 0) dReference = g_pStatus->GetProfileOption()->m_dProfileRadius[nStageNo][ptProfile->iIndex]; ProfileResult.nProfileDiff = (int)ROUND(ptProfile->dDist-dReference); RCutResult.dCenterX = ptProfile->ptCenterX; RCutResult.dCenterY = ptProfile->ptCenterY; RCutResult.dCADLineX = ptProfile->ptCadLine.x; RCutResult.dCADLineY = ptProfile->ptCadLine.y; RCutResult.dRadius = ptProfile->dRadius; RCutResult.dPixelRadius = RCutResult.dRadius/dScan; RCutResult.ptGlass = ptProfile->ptGlass; RCutResult.ptChamfer = ptProfile->ptChamfer; ProfileResult.bProfileJudge = TRUE; RCutRandom(ProfileResult); if(ProfileResult.bUseJudge == TRUE) { //if(RCutRandom(ProfileResult) == FALSE) { if(ProfileResult.nProfileDiff < -1*ProfileResult.nJudgeMinus || ProfileResult.nProfileDiff > ProfileResult.nJudgePlus) { ProfileResult.bProfileJudge = FALSE; m_pDefectControl->ExtractDefect_ProfileChamfer(eDimension,m_iCamera,stFrame.nScanIdx,DefectLoc_Profile,RCutResult.ptGlass,ptProfile->iIndex); } } } m_pGlassData->SetProfileInspectData(ProfileResult.iIndex,&ProfileResult); m_pGlassData->SetRCutProfileInspectData(RCutResult.iIndex,&RCutResult); } if(pCamera->m_enCamDir == CAMDIR_TOP) pstChamferPrm = pPrm->m_stCamferUp; else pstChamferPrm = pPrm->m_stCamferDn; ChamferResult.iCam = ptProfile->iCam; ChamferResult.iFrame = stFrame.nFrameIdx; ChamferResult.iIndex = ptProfile->iIndex; ChamferResult.iScan = ptProfile->iScan; ChamferResult.iSide = ptProfile->iSide; ChamferResult.nUp = (int)pCamera->m_enCamDir; ChamferResult.bUseJudge = pstChamferPrm[ptProfile->iIndex].use; ChamferResult.nJudgeReference = (int)ROUND(pstChamferPrm[ptProfile->iIndex].reference); ChamferResult.nJudgePlusChamfer = (int)ROUND(pstChamferPrm[ptProfile->iIndex].plus); ChamferResult.nJudgeMinusChamfer = (int)ROUND(pstChamferPrm[ptProfile->iIndex].minus); ChamferResult.nChamferThick = (int)ROUND(ptProfile->dChamfer-ChamferResult.nJudgeReference); ChamferResult.nChamferCalThick = abs((int)ROUND(ptProfile->dChamfer)); ChamferResult.ptCADLine = ptProfile->ptCadLine; ChamferResult.ptChamferLine = ptProfile->ptChamfer; ChamferResult.ptGlassLine = ptProfile->ptGlass; ChamferResult.bChamferJudge = TRUE; RCutChamferRandom(ChamferResult); if(ChamferResult.bUseJudge == TRUE) { //if(RCutChamferRandom(ChamferResult) == FALSE) { if(ChamferResult.nChamferCalThick < ChamferResult.nJudgeReference-ChamferResult.nJudgeMinusChamfer || ChamferResult.nChamferCalThick > ChamferResult.nJudgeReference+ChamferResult.nJudgePlusChamfer) { ChamferResult.bChamferJudge = FALSE; m_pDefectControl->ExtractDefect_ProfileChamfer(eDimension,m_iCamera,stFrame.nScanIdx,DefectLoc_Chamfer,ChamferResult.ptGlassLine,ptProfile->iIndex); } } } m_pGlassData->SetProfileChamferInspectData(ChamferResult.iIndex,ChamferResult.nUp,&ChamferResult); } return TRUE; } BOOL CInspectCamera::FindRCutProfilePosition(DimensionDir eDimension,stFrameIndex stFrame,CProfileCADProc *pProcess,vector< pair > &vecProfile,CvPoint2D32f *ptAlign) { vector< pair >::iterator it; STU_SPLINE_PROFILE_POSITION *pProfilePos; double dConv,dScan; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return 0; dConv = pCamera->m_dConvResolution[g_pBase->m_nStageNo]; dScan = pCamera->m_dScanResolution[g_pBase->m_nStageNo]; if(dConv <= 0 || dScan <= 0) return 0; for(it=vecProfile.begin();it!=vecProfile.end();it++) { pProfilePos = it->second; if(pProfilePos == NULL || pProfilePos->iIndex < 0) continue; FindRCutPosition(pProfilePos,eDimension,stFrame,pProcess); /* if(pProfilePos->iLoop == 0 || pProfilePos->iLoop == 4) { double dTheta = (pProfilePos->dTheta); if(pProfilePos->dTheta == 0 || pProfilePos->dTheta == 360) { pProfilePos->dDist = abs(pProfilePos->ptGlass.x-pProfilePos->ptCenterX)*dConv; pProfilePos->dChamfer = abs(pProfilePos->ptGlass.x-pProfilePos->ptChamfer.x)*dConv; } else { pProfilePos->dDist = abs(pProfilePos->ptGlass.y-pProfilePos->ptCenterY)*dScan; pProfilePos->dChamfer = abs(pProfilePos->ptGlass.y-pProfilePos->ptChamfer.y)*dScan; } } else */ { double dDx = abs(pProfilePos->ptGlass.x-pProfilePos->ptCenterX)*dConv; double dDy = abs(pProfilePos->ptGlass.y-pProfilePos->ptCenterY)*dScan; pProfilePos->dDist = sqrtf(float(dDx *dDx + dDy*dDy)); dDx = abs(pProfilePos->ptGlass.x-pProfilePos->ptChamfer.x)*dConv; dDy = abs(pProfilePos->ptGlass.y-pProfilePos->ptChamfer.y)*dScan; pProfilePos->dChamfer = sqrtf(float(dDx *dDx + dDy*dDy)); } } return TRUE; } BOOL CInspectCamera::FindRCutPosition(STU_SPLINE_PROFILE_POSITION *pProfilePos,DimensionDir eDimension,stFrameIndex stFrame,CProfileCADProc *pProcess) { COwnerBuffer bufTgt; CEdgeProc EdgeProc; /* if(pProfilePos->iLoop == 0 || pProfilePos->iLoop == 4) { FindRCutNoRotateFind(pProfilePos,pRecipe); } else */ { ROI ImgROI = GetRCutProcessImage(pProfilePos,eDimension,stFrame,bufTgt); if(pProfilePos->iPos <0 || pProfilePos->iPos > 1) return FALSE; int nGlassThres = m_pRecipe->GetInspectInfo()->m_nRCutThresGlass[eDimension][1]; int nChamferThres = m_pRecipe->GetInspectInfo()->m_nRCutThresChamfer[eDimension][1]; if(eDimension == DIMENSION_A || eDimension == DIMENSION_A_DN) { nGlassThres = m_pRecipe->GetInspectInfo()->m_nRCutThresGlass[eDimension][pProfilePos->iPos]; nChamferThres = m_pRecipe->GetInspectInfo()->m_nRCutThresChamfer[eDimension][pProfilePos->iPos]; } IMPOS2D ptGlassEdgePos,ptChamferEdgePos; FindGlassChamfer(pProfilePos->iIndex,eDimension,stFrame,bufTgt,nGlassThres,nChamferThres,ptGlassEdgePos,ptChamferEdgePos); double dSin = sin(M_RADIAN(pProfilePos->dTheta)); double dCos = cos(M_RADIAN(pProfilePos->dTheta)); IMPOS2D p1,posCen; posCen.u = pProfilePos->ptCenterX; posCen.v = pProfilePos->ptCenterY; ptGlassEdgePos.u += ImgROI.u1; ptGlassEdgePos.v += ImgROI.v1; ptChamferEdgePos.u += ImgROI.u1; ptChamferEdgePos.v += ImgROI.v1; EdgeProc.IMRotatePoint(ptGlassEdgePos, &p1, posCen, dSin, dCos) ; pProfilePos->ptGlass.x = (float)p1.u; pProfilePos->ptGlass.y = (float)p1.v; EdgeProc.IMRotatePoint(ptChamferEdgePos, &p1, posCen, dSin, dCos) ; pProfilePos->ptChamfer.x = (float)p1.u; pProfilePos->ptChamfer.y = (float)p1.v; } return TRUE; } BOOL CInspectCamera::FindGlassChamfer(int index,DimensionDir eDimension,stFrameIndex stFrame,CSISBuffer &bufTgt,int nGlassThres,int nChamferThres,IMPOS2D &ptGlassEdgePos,IMPOS2D &ptChamferEdgePos) { CEdgeProc EdgeProc; CSISEdgeFind EdgeFind; double dPitch = 10; int nGlassEdge = 0; if(EdgeFind.FindEdge_ToRight(&bufTgt,nGlassEdge,dPitch,nGlassThres,0.3,0) == FALSE) { ptGlassEdgePos.u = (float)bufTgt.GetWidth()/2; ptGlassEdgePos.v = (float)bufTgt.GetHeight()/2; g_pLog->DisplayMessage(_T("%s RCut Profile Edge Detect Fail : index %d"),PANEL_SIDE[eDimension],index); } else { ptGlassEdgePos.u = (float)nGlassEdge; ptGlassEdgePos.v = (float)bufTgt.GetHeight()/2; } COwnerBuffer CopyBuffer(bufTgt.GetWidth(),bufTgt.GetHeight()); CRect CopyRect(0,0,bufTgt.GetWidth(),bufTgt.GetHeight()); CSISBuffer::CopyBtoA(CopyBuffer,0,0,bufTgt,CopyRect); EdgeProc.ThresholdProcessing(bufTgt.GetDataAddress(),CSize(bufTgt.GetWidth(),bufTgt.GetHeight()),nChamferThres,0); int nChamferEndge = nGlassEdge+2; int nContiCnt = 0; int uStart,uEnd; uStart = (int)ptGlassEdgePos.u; uEnd = uStart + 80; if(uEnd > bufTgt.GetWidth()-1) uEnd = bufTgt.GetWidth()-1; for(int u=uStart;u= 3) { nChamferEndge = u-1; break; } } } ptChamferEdgePos.u = (float)nChamferEndge; ptChamferEdgePos.v = (float)bufTgt.GetHeight()/2; { for(int v=0;v CopyBuffer.GetWidth()) continue; CopyBuffer.SetPixel((int)ptGlassEdgePos.u,v,128); if(ptChamferEdgePos.u < 0 || ptChamferEdgePos.u > CopyBuffer.GetWidth()) continue; CopyBuffer.SetPixel((int)ptChamferEdgePos.u,v,128); } #if MARKIMG_NOTSAVE CString str; str.Format(_T("%sIMG_MARK\\%s\\%s\\%s\\ChamferRes_%s_%d.bmp"), PATH_INSPECTION_DATA, g_pBase->m_strLoadingDay, g_pBase->m_strHPanelID, g_pBase->m_strLoadingTime, PANEL_SIDE[eDimension], index); CBufferAttach attach(str); attach.AttachToFile(CopyBuffer); #endif // 0 } return TRUE; } ROI CInspectCamera::GetRCutProcessImage(STU_SPLINE_PROFILE_POSITION *pProfilePos,DimensionDir eDimension,stFrameIndex stFrame,COwnerBuffer &pTgt) { int nDistance; ROI aoi; double dSin,dCos; IMPOS2D posCen; int n4ByteWidth; LPBYTE lpBuffer; CEdgeProc EdgeProc; CSISBuffer bufOrg; const int nSearchArea = 500; const int nSearchHeight = 3; dSin = sin(M_RADIAN(pProfilePos->dTheta)); dCos = cos(M_RADIAN(pProfilePos->dTheta)); posCen.u = pProfilePos->ptCenterX; posCen.v = pProfilePos->ptCenterY; nDistance = (int)GetRadus2Distance(eDimension,stFrame, pProfilePos->dTheta,pProfilePos->dRadius,dSin,dCos); aoi.u1 = aoi.u2 = aoi.v1 = aoi.v2 = 0; lpBuffer = m_pGrabber->GetFrameHeaderLine(pProfilePos->iScan,0); if(lpBuffer == NULL) return aoi; int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); bufOrg.SetBuffer(lpBuffer,nFrameWidth,g_pBase->m_pMemFrameNo[m_iCamera]*nFrameHeight); aoi.u1 = (int)ROUND(posCen.u) - nDistance - nSearchArea; aoi.u2 = (int)ROUND(posCen.u) - nDistance + nSearchArea; aoi.v1 = (int)ROUND(posCen.v) - nSearchHeight; aoi.v2 = (int)ROUND(posCen.v) + nSearchHeight; n4ByteWidth = align_4byte(EdgeProc.IMROIWidth(aoi)); aoi.u2 = aoi.u1 + n4ByteWidth; pTgt.SetSize(EdgeProc.IMROIWidth(aoi),EdgeProc.IMROIHeight(aoi)); EdgeProc.IMGetRotImg(aoi,bufOrg,pTgt,posCen,dSin,dCos); return aoi; } double CInspectCamera::GetRadus2Distance(DimensionDir eDimension,stFrameIndex stFrame,double dtheta,double dRadius,double dSin,double dCons) { CvPoint2D32f iPos; double dXPos,dYPos; double dConv,dScan; CEdgeProc EdgeProc; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return 0; dConv = pCamera->m_dConvResolution[g_pBase->m_nStageNo]; dScan = pCamera->m_dScanResolution[g_pBase->m_nStageNo]; if(dConv <= 0 || dScan <= 0) return 0; iPos.x = (float)dRadius; iPos.y = 0; EdgeProc.RotatePoint(iPos, dXPos,dYPos,dSin, dCons); double dDx = dXPos/dConv; double dDy = dYPos/dScan; return sqrtf(float(dDx *dDx + dDy*dDy)); } #define MAX_RCUT_PROFILE_COUNT 5 BOOL CInspectCamera::RCutRandom(PROFILE_RESULT &ProfileResult) { CInspectOption *pOpt = g_pStatus->GetInspectOption(); if(pOpt->bUseRandomData == FALSE) return FALSE; CRandomDataSet *pDataSet = NULL; if(PROFILE_RCUT_INSPECT_INDEX_A <= ProfileResult.iIndex && PROFILE_RCUT_INSPECT_INDEX_A+MAX_RCUT_PROFILE_COUNT > ProfileResult.iIndex) pDataSet = &pOpt->A1_Rcut; else if(PROFILE_RCUT_INSPECT_INDEX_B <= ProfileResult.iIndex && PROFILE_RCUT_INSPECT_INDEX_B+MAX_RCUT_PROFILE_COUNT > ProfileResult.iIndex) pDataSet = &pOpt->A2_Rcut; else if(PROFILE_RCUT_INSPECT_INDEX_C <= ProfileResult.iIndex && PROFILE_RCUT_INSPECT_INDEX_C+MAX_RCUT_PROFILE_COUNT > ProfileResult.iIndex) pDataSet = &pOpt->C1_Rcut; else if(PROFILE_RCUT_INSPECT_INDEX_D <= ProfileResult.iIndex && PROFILE_RCUT_INSPECT_INDEX_D+MAX_RCUT_PROFILE_COUNT > ProfileResult.iIndex) pDataSet = &pOpt->C2_Rcut; if(pDataSet == NULL) return FALSE; if(abs(ProfileResult.nProfileDiff) > pDataSet->Profile.dLimit) return FALSE; if(ProfileResult.nProfileDiff < pDataSet->Profile.dMin || ProfileResult.nProfileDiff > pDataSet->Profile.dMax) { int nRandMax = (int)max(fabs(pDataSet->Profile.dMin),fabs(pDataSet->Profile.dMax)); int nValue = rand()%nRandMax; int nDir = rand()%1; if(nDir == 0) { if(nValue > fabs(pDataSet->Profile.dMin)) nValue = (int)pDataSet->Profile.dMin; if(nValue > 0) nValue = -1*nValue; } else { if(nValue > fabs(pDataSet->Profile.dMax)) nValue = (int)pDataSet->Profile.dMax; if(nValue < 0) nValue = -1*nValue; } ProfileResult.nProfileDiff = nValue; } return TRUE; } BOOL CInspectCamera::ProfileRandom(PROFILE_RESULT &ProfileResult) { CInspectOption *pOpt = g_pStatus->GetInspectOption(); if(pOpt->bUseRandomData == FALSE) return FALSE; CRandomDataSet *pDataSet = NULL; if(PROFILE_INSPECT_INDEX_B <= ProfileResult.iIndex && PROFILE_INSPECT_INDEX_B+PROFILE_INSPECT_SIZE_B_D > ProfileResult.iIndex) pDataSet = &pOpt->B_GrindLine; else if(PROFILE_INSPECT_INDEX_D <= ProfileResult.iIndex && PROFILE_INSPECT_INDEX_D+PROFILE_INSPECT_SIZE_B_D > ProfileResult.iIndex) pDataSet = &pOpt->D_GrindLine; else if(PROFILE_INSPECT_INDEX_C <= ProfileResult.iIndex && PROFILE_INSPECT_INDEX_C+PROFILE_INSPECT_SIZE_A_C > ProfileResult.iIndex) pDataSet = &pOpt->C_GrindLine; else if(PROFILE_INSPECT_INDEX_A <= ProfileResult.iIndex && PROFILE_INSPECT_INDEX_A+PROFILE_INSPECT_SIZE_A_C > ProfileResult.iIndex) pDataSet = &pOpt->A1_GrindLine; else if(PROFILE_INSPECT_INDEX_CF <= ProfileResult.iIndex && PROFILE_INSPECT_INDEX_CF+PROFILE_INSPECT_SIZE_A_C > ProfileResult.iIndex) pDataSet = &pOpt->A2_GrindLine; if(pDataSet == NULL) return FALSE; if(abs(ProfileResult.nProfileDiff) > pDataSet->Profile.dLimit) return FALSE; if(ProfileResult.nProfileDiff < pDataSet->Profile.dMin || ProfileResult.nProfileDiff > pDataSet->Profile.dMax) { int nRandMax = (int)max(fabs(pDataSet->Profile.dMin),fabs(pDataSet->Profile.dMax)); int nValue = rand()%nRandMax; int nDir = rand()%1; if(nDir == 0) { if(nValue > fabs(pDataSet->Profile.dMin)) nValue = (int)pDataSet->Profile.dMin; if(nValue > 0) nValue = -1*nValue; } else { if(nValue > fabs(pDataSet->Profile.dMax)) nValue = (int)pDataSet->Profile.dMax; if(nValue < 0) nValue = -1*nValue; } ProfileResult.nProfileDiff = nValue; } return TRUE; } BOOL CInspectCamera::RCutChamferRandom(CHAMFER_RESULT &ChamferResult) { CInspectOption *pOpt = g_pStatus->GetInspectOption(); if(pOpt->bUseRandomData == FALSE) return FALSE; CRandomDataSet *pDataSet = NULL; if(PROFILE_RCUT_INSPECT_INDEX_A <= ChamferResult.iIndex && PROFILE_RCUT_INSPECT_INDEX_A+MAX_RCUT_PROFILE_COUNT > ChamferResult.iIndex) pDataSet = &pOpt->A1_Rcut; else if(PROFILE_RCUT_INSPECT_INDEX_B <= ChamferResult.iIndex && PROFILE_RCUT_INSPECT_INDEX_B+MAX_RCUT_PROFILE_COUNT > ChamferResult.iIndex) pDataSet = &pOpt->A2_Rcut; else if(PROFILE_RCUT_INSPECT_INDEX_C <= ChamferResult.iIndex && PROFILE_RCUT_INSPECT_INDEX_C+MAX_RCUT_PROFILE_COUNT > ChamferResult.iIndex) pDataSet = &pOpt->C1_Rcut; else if(PROFILE_RCUT_INSPECT_INDEX_D <= ChamferResult.iIndex && PROFILE_RCUT_INSPECT_INDEX_D+MAX_RCUT_PROFILE_COUNT > ChamferResult.iIndex) pDataSet = &pOpt->C2_Rcut; if(pDataSet == NULL) return FALSE; CRandomData *pRndData = NULL; if(ChamferResult.nUp == CAMDIR_TOP) pRndData = &pDataSet->ChamferUp; else pRndData = &pDataSet->ChamferDn; if(abs(ChamferResult.nChamferCalThick) > pRndData->dLimit) return FALSE; if(ChamferResult.nChamferCalThick < pRndData->dMin || ChamferResult.nChamferCalThick > pRndData->dMax) { int nRandMax = (int)max(fabs(pRndData->dMin),fabs(pRndData->dMax)); int nValue = rand()%nRandMax; if(abs(nValue) < fabs(pRndData->dMin)) nValue = (int)pRndData->dMin; if(abs(nValue) > fabs(pRndData->dMax)) nValue = (int)pRndData->dMax; ChamferResult.nChamferCalThick = abs(nValue); } ChamferResult.nChamferThick = (int)ROUND(ChamferResult.nChamferCalThick-ChamferResult.nJudgeReference); return TRUE; } BOOL CInspectCamera::ChamferRandom(CHAMFER_RESULT &ChamferResult) { CInspectOption *pOpt = g_pStatus->GetInspectOption(); if(pOpt->bUseRandomData == FALSE) return FALSE; CRandomDataSet *pDataSet = NULL; if(PROFILE_INSPECT_INDEX_B <= ChamferResult.iIndex && PROFILE_INSPECT_INDEX_B+PROFILE_INSPECT_SIZE_B_D > ChamferResult.iIndex) pDataSet = &pOpt->B_GrindLine; else if(PROFILE_INSPECT_INDEX_D <= ChamferResult.iIndex && PROFILE_INSPECT_INDEX_D+PROFILE_INSPECT_SIZE_B_D > ChamferResult.iIndex) pDataSet = &pOpt->D_GrindLine; else if(PROFILE_INSPECT_INDEX_C <= ChamferResult.iIndex && PROFILE_INSPECT_INDEX_C+PROFILE_INSPECT_SIZE_A_C > ChamferResult.iIndex) pDataSet = &pOpt->C_GrindLine; else if(PROFILE_INSPECT_INDEX_A <= ChamferResult.iIndex && PROFILE_INSPECT_INDEX_A+PROFILE_INSPECT_SIZE_A_C > ChamferResult.iIndex) pDataSet = &pOpt->A1_GrindLine; else if(PROFILE_INSPECT_INDEX_CF <= ChamferResult.iIndex && PROFILE_INSPECT_INDEX_CF+PROFILE_INSPECT_SIZE_A_C > ChamferResult.iIndex) pDataSet = &pOpt->A2_GrindLine; if(pDataSet == NULL) return FALSE; CRandomData *pRndData = NULL; if(ChamferResult.nUp == CAMDIR_TOP) pRndData = &pDataSet->ChamferUp; else pRndData = &pDataSet->ChamferDn; if(abs(ChamferResult.nChamferCalThick) > pRndData->dLimit) return FALSE; if(ChamferResult.nChamferCalThick < pRndData->dMin || ChamferResult.nChamferCalThick > pRndData->dMax) { int nRandMax = (int)max(fabs(pRndData->dMin),fabs(pRndData->dMax)); int nValue = rand()%nRandMax; if(abs(nValue) < fabs(pRndData->dMin)) nValue = (int)pRndData->dMin; if(abs(nValue) > fabs(pRndData->dMax)) nValue = (int)pRndData->dMax; ChamferResult.nChamferCalThick = abs(nValue); } ChamferResult.nChamferThick = (int)ROUND(ChamferResult.nChamferCalThick-ChamferResult.nJudgeReference); return TRUE; } BOOL CInspectCamera::CalculateProfileChamfer(DimensionDir emDim,stFrameIndex stFrame,CProfileCADProc *pProcess,vector< pair > &vecProfile,CvPoint2D32f ptAlign) { if((int)vecProfile.size() <= 0) return FALSE; if(m_pGlassData == NULL) return FALSE; CProfileInspect_Prm *pPrm = m_pRecipe->GetProfileParm(); vector< pair >::iterator it; STU_PROFILE_POSITION *ptProfile; STU_THICKPOS stThick; int nPosY; PROFILE_RESULT ProfileResult; CHAMFER_RESULT ChamferResult; ST_PROFILE_SETTING stPrm; ST_CHAMFER_SETTING stChamferPrm; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return FALSE; double dConv = pCamera->m_dConvResolution[g_pBase->m_nStageNo]; if(dConv <= 0) return FALSE; double dProfileAvg; double dChamferAvg; GetAvgProfileNChamfer(emDim,stFrame,vecProfile,dProfileAvg,dChamferAvg); INS_EDGE_RESULT_INFO *pResInfo = NULL; pResInfo = m_pGlassData->GetEdgeResultInfo(emDim); CINSPECT_JUDGEMENT_PARM *pJudgePrm = m_pRecipe->GetJudgePrm(); srand((unsigned int)time(NULL)); for(it=vecProfile.begin();it!=vecProfile.end();it++) { ptProfile = it->second; if(ptProfile == NULL) continue; if(ptProfile->iIndex < 0 || ptProfile->iIndex >= MAX_PROFILE_SETTING_COUNT) continue; stPrm = pPrm->m_stProfile[ptProfile->iIndex]; if(pCamera->m_enCamDir == CAMDIR_TOP) stChamferPrm = pPrm->m_stCamferUp[ptProfile->iIndex]; else stChamferPrm = pPrm->m_stCamferDn[ptProfile->iIndex]; nPosY = ptProfile->point.y; nPosY -= pResInfo->nGlassStartLine; stThick = GetThickPosResultPos(stFrame,nPosY); ProfileResult.ptFullImg.x = (float)ptProfile->point.x; ProfileResult.ptFullImg.y = (float)ptProfile->point.y; ProfileResult.ptGlassLine.x = (float)stThick.nLeftLine; ProfileResult.ptGlassLine.y = (float)ptProfile->point.y; ProfileResult.iCam = m_iCamera; ProfileResult.iIndex = ptProfile->iIndex; ProfileResult.iFrame = stFrame.nFrameIdx; ProfileResult.iScan = stFrame.nScanIdx; ProfileResult.iSide = (int)emDim; if(pCamera->m_enCamDir == CAMDIR_BOTTOM) { ProfileResult.ptGlassAlign.x = (float)ptProfile->point.x; ProfileResult.ptGlassAlign.y = (float)ptProfile->point.y; ProfileResult.ptGlassAlign.y += (float)(-1*ptAlign.y); ProfileResult.ptGlassStart = ProfileResult.ptFullImg; ProfileResult.ptGlassStart.y += (float)(-pResInfo->nGlassStartLine); ProfileResult.nProfileDiff = (int)((double)(ptProfile->point.x - stThick.nLeftLine)*dConv); ProfileResult.bUseJudge = stPrm.use; ProfileResult.nJudgePlus = (int)(stPrm.plus); ProfileResult.nJudgeMinus = (int)(stPrm.minus); ProfileResult.bProfileJudge = TRUE; ProfileRandom(ProfileResult); if(ProfileResult.bUseJudge == TRUE) { //if(ProfileRandom(ProfileResult) == FALSE) { if(ProfileResult.nProfileDiff < -1*ProfileResult.nJudgeMinus || ProfileResult.nProfileDiff > ProfileResult.nJudgePlus) { if(pJudgePrm->AvgProfile[emDim].bUse == TRUE && dProfileAvg > 0) { if(abs(ProfileResult.nProfileDiff-(int)dProfileAvg) > pJudgePrm->AvgProfile[emDim].nDiff) { ProfileResult.bProfileJudge = FALSE; m_pDefectControl->ExtractDefect_ProfileChamfer(emDim,m_iCamera,stFrame.nScanIdx,DefectLoc_Profile,ProfileResult.ptFullImg,ptProfile->iIndex); g_pLog->DisplayMessage(_T("%s Avg Profile Option NG : Index %d,Diff %d,Avg %.2f,Setting %d"),PANEL_SIDE[emDim],ProfileResult.iIndex,ProfileResult.nProfileDiff,dProfileAvg,pJudgePrm->AvgProfile[emDim].nDiff); } } else { ProfileResult.bProfileJudge = FALSE; m_pDefectControl->ExtractDefect_ProfileChamfer(emDim,m_iCamera,stFrame.nScanIdx,DefectLoc_Profile,ProfileResult.ptFullImg,ptProfile->iIndex); } } } } m_pGlassData->SetProfileInspectData(ptProfile->iIndex,&ProfileResult); } ChamferResult.iCam = m_iCamera; ChamferResult.iIndex = ptProfile->iIndex; ChamferResult.iFrame = stFrame.nFrameIdx; ChamferResult.iScan = stFrame.nScanIdx; ChamferResult.iSide = (int)emDim; ChamferResult.nUp = (int)pCamera->m_enCamDir; ChamferResult.nJudgeReference = (int)(stChamferPrm.reference); ChamferResult.nJudgeMinusChamfer = (int)(stChamferPrm.minus); ChamferResult.nJudgePlusChamfer = (int)(stChamferPrm.plus); ChamferResult.bUseJudge = stChamferPrm.use; ChamferResult.nChamferThick = (int)(stThick.dThick-ChamferResult.nJudgeReference); ChamferResult.nChamferCalThick = abs((int)ROUND(stThick.dThick)); ChamferResult.nGlassLeft = stThick.nLeftLine; ChamferResult.nGlassRight = stThick.nRightLine; ChamferResult.ptChamferLine.x = (float)stThick.nRightLine-1; ChamferResult.ptChamferLine.y = (float)(ProfileResult.ptFullImg.y); ChamferResult.ptFull.x = (float)ptProfile->point.x; ChamferResult.ptFull.y = (float)ptProfile->point.y; ChamferResult.bChamferJudge = TRUE; //ChamferRandom(ChamferResult); if(ChamferResult.bUseJudge == TRUE) { if(ChamferRandom(ChamferResult) == FALSE) { if(ChamferResult.nChamferCalThick < ChamferResult.nJudgeReference-ChamferResult.nJudgeMinusChamfer || ChamferResult.nChamferCalThick > ChamferResult.nJudgeReference+ChamferResult.nJudgePlusChamfer) { if(pJudgePrm->AvgChamfer[emDim].bUse == TRUE && dChamferAvg > 0) { if(abs(ChamferResult.nChamferCalThick-(int)dChamferAvg) > pJudgePrm->AvgChamfer[emDim].nDiff) { ChamferResult.bChamferJudge = FALSE; m_pDefectControl->ExtractDefect_ProfileChamfer(emDim,m_iCamera,stFrame.nScanIdx,DefectLoc_Chamfer,ChamferResult.ptFull,ptProfile->iIndex); g_pLog->DisplayMessage(_T("%s Avg Chamfer Option NG : Index %d,Diff %d,Avg %.2f,Setting %d"),PANEL_SIDE[emDim],ChamferResult.iIndex,ChamferResult.nChamferCalThick,dChamferAvg,pJudgePrm->AvgChamfer[emDim].nDiff); } } else { ChamferResult.bChamferJudge = FALSE; m_pDefectControl->ExtractDefect_ProfileChamfer(emDim,m_iCamera,stFrame.nScanIdx,DefectLoc_Chamfer,ChamferResult.ptFull,ptProfile->iIndex); } } } } m_pGlassData->SetProfileChamferInspectData(ChamferResult.iIndex,ChamferResult.nUp,&ChamferResult); } g_pLog->DisplayMessage(_T("%s Calculate Profile Postion :Profile %d"),PANEL_SIDE[emDim],(int)vecProfile.size()); return TRUE; } STU_THICKPOS CInspectCamera::GetThickPosResultPos( stFrameIndex stFrame,int nYPos) { int nMinPos = INT_MAX; std::vector::iterator it; STU_THICKPOS stTmpThick; STU_THICKPOS stThick; for(it=m_resultThickPos[stFrame.nScanIdx].begin();it!=m_resultThickPos[stFrame.nScanIdx].end();it++) { stTmpThick = *it; if(abs(stTmpThick.nTopLine-nYPos) < nMinPos) { nMinPos = abs(stTmpThick.nTopLine-nYPos); stThick = stTmpThick; } } return stThick; } BOOL CInspectCamera::GetAvgProfileNChamfer(DimensionDir emDim,stFrameIndex stFrame,vector< pair > &vecProfile,double &dProfileAvg,double &dChamferAvg) { double dProfileCnt,dChamferCnt; CProfileInspect_Prm *pPrm = m_pRecipe->GetProfileParm(); vector< pair >::iterator it; STU_PROFILE_POSITION *ptProfile; STU_THICKPOS stThick; ST_PROFILE_SETTING stPrm; ST_CHAMFER_SETTING stChamferPrm; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return FALSE; double dConv = pCamera->m_dConvResolution[g_pBase->m_nStageNo]; if(dConv <= 0) return FALSE; INS_EDGE_RESULT_INFO *pResInfo = NULL; pResInfo = m_pGlassData->GetEdgeResultInfo(emDim); double dProfileSum,dChamferSum; dProfileAvg = dProfileCnt = dChamferAvg = dChamferCnt = 0.; dProfileSum = dChamferSum = 0.; CINSPECT_JUDGEMENT_PARM *pJudgePrm = m_pRecipe->GetJudgePrm(); if(pJudgePrm->AvgProfile[emDim].bUse == TRUE || pJudgePrm->AvgChamfer[emDim].bUse == TRUE) { for(it=vecProfile.begin();it!=vecProfile.end();it++) { ptProfile = it->second; if(ptProfile == NULL) continue; if(ptProfile->iIndex < 0 || ptProfile->iIndex >= MAX_PROFILE_SETTING_COUNT) continue; stPrm = pPrm->m_stProfile[ptProfile->iIndex]; int nPosY = ptProfile->point.y; nPosY -= pResInfo->nGlassStartLine; stThick = GetThickPosResultPos(stFrame,nPosY); if(pJudgePrm->AvgProfile[emDim].bUse == TRUE) { if(pCamera->m_enCamDir == CAMDIR_BOTTOM) { int nDiffProfile = (int)((double)(ptProfile->point.x - stThick.nLeftLine)*dConv); if(nDiffProfile < -1*(int)(stPrm.minus) || nDiffProfile > (int)(stPrm.plus)) { ; } else { dProfileSum += nDiffProfile; dProfileCnt++; } } } if(pJudgePrm->AvgChamfer[emDim].bUse == TRUE) { if(pCamera->m_enCamDir == CAMDIR_TOP) stChamferPrm = pPrm->m_stCamferUp[ptProfile->iIndex]; else stChamferPrm = pPrm->m_stCamferDn[ptProfile->iIndex]; int nChamferThick = (int)(stThick.dThick-(int)(stChamferPrm.reference)); if(nChamferThick < -1*(int)(stChamferPrm.minus) || nChamferThick > (int)(stChamferPrm.plus)) { ; } else { dChamferSum += (int)ROUND(stThick.dThick); dChamferCnt++; } } } } if(dProfileCnt > 0) dProfileAvg = dProfileSum/dProfileCnt; if(dChamferCnt > 0) dChamferAvg = dChamferSum/dChamferCnt; return TRUE; } BOOL CInspectCamera::MakeProfilePosition(DimensionDir emDim,stFrameIndex stFrame,CProfileCADProc *pProcess,vector< pair > &vecProfile,CvPoint2D32f *ptAlign) { if(m_pGlassData == NULL) { g_pLog->DisplayMessage(_T("%s MakeProfilePosition Glass NULL : Frame %d"),PANEL_SIDE[emDim],stFrame.nFrameIdx); return FALSE; } CProfileInspect_Prm *pPrm = m_pRecipe->GetProfileParm(); CvPoint2D32f ptOffset; double dConv,dScan; ptOffset.x = (float)pPrm->m_nCADOffsetX[emDim]; ptOffset.y = (float)pPrm->m_nCADOffsetY[emDim]; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) { g_pLog->DisplayMessage(_T("%s MakeProfilePosition Camera NULL : Frame %d"),PANEL_SIDE[emDim],stFrame.nFrameIdx); return FALSE; } dConv = pCamera->m_dConvResolution[g_pBase->m_nStageNo]; dScan = pCamera->m_dScanResolution[g_pBase->m_nStageNo]; if(dConv <= 0 || dScan <= 0) { g_pLog->DisplayMessage(_T("%s MakeProfilePosition Res Null : Frame %d, %d,%.3f,%.3f"),PANEL_SIDE[emDim],stFrame.nFrameIdx,g_pBase->m_nStageNo,dConv,dScan); return FALSE; } ptOffset.x = (float)((double)ptOffset.x / dConv); ptOffset.y = (float)((double)ptOffset.y / dScan); int nRcpIndex,nRcpIndexEnd; int iIndex; ST_PROFILE_SETTING stProfile; const int nRange = 5; STU_PROFILE_POSITION *pPfPositon; CString str; if(m_pGlassData->GetProfileInspectIndex(emDim,nRcpIndex,nRcpIndexEnd,str) == FALSE) { g_pLog->DisplayMessage(_T("%s MakeProfilePosition GetProfileInspectIndex NULL : Frame %d, %d,%d"),PANEL_SIDE[emDim],stFrame.nFrameIdx,nRcpIndex,nRcpIndexEnd); return FALSE; } pProcess->RotateProfileData(ptAlign[0],ptAlign[1],ptOffset); int nAlignOffset = 0; CINSPECT_JUDGEMENT_PARM *pJudgePrm = m_pRecipe->GetJudgePrm(); switch(emDim) { case DIMENSION_C: case DIMENSION_C_DN: nAlignOffset = 4777;//pJudgePrm->ptAlign[0][DIMENSION_C].y - pJudgePrm->ptAlign[0][DIMENSION_A].y; break; case DIMENSION_B: case DIMENSION_B_DN: case DIMENSION_D: case DIMENSION_D_DN: nAlignOffset = -9000;//1440;//pJudgePrm->ptAlign[0][pRecipe->GetDimension()].y - 1440; // Edge <-> Align : 1.44mm 父怒 哗霖促. break; } CPoint ptCADOrgin = pProcess->GetProfileOrigin(); for(iIndex=nRcpIndex;iIndexm_stProfile[iIndex]; if(stProfile.use == FALSE) continue; pPfPositon = new STU_PROFILE_POSITION; pPfPositon->nMin = (int)((stProfile.y)/dScan) -nRange; pPfPositon->nMax = (int)((stProfile.y)/dScan) + nRange; pPfPositon->nMin += (int)ptCADOrgin.y; pPfPositon->nMax += (int)ptCADOrgin.y; // pPfPositon->nMin += (int)(ptAlign[0].y-ptCADOrgin.y); // pPfPositon->nMax += (int)(ptAlign[0].y-ptCADOrgin.y); pPfPositon->nMin += (int)(nAlignOffset/dScan); pPfPositon->nMax += (int)(nAlignOffset/dScan); pPfPositon->iIndex = iIndex; pPfPositon->iScan = stFrame.nScanIdx; pPfPositon->iSide = (int)emDim; vecProfile.push_back(make_pair(pPfPositon->nMin, pPfPositon)); } std::sort(vecProfile.begin(),vecProfile.end(),less< pair >()); pProcess->GetProfilePosition(vecProfile); g_pLog->DisplayMessage(_T("%s Make Profile Postion :Profile %d"),PANEL_SIDE[emDim],(int)vecProfile.size()); return TRUE; } void CInspectCamera::CalculateStageTheta(DimensionDir emDim,int iScan, CvPoint2D32f *pAlign) { BOOL bLong; switch(emDim) { case DIMENSION_A: bLong = TRUE; break; case DIMENSION_B: bLong = FALSE; break; default: return; } CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,iScan); if(pCamera == NULL) return; double dx = (pAlign[1].x-pAlign[0].x) * pCamera->m_dConvResolution[g_pBase->m_nStageNo]; double dy = (pAlign[1].y-pAlign[0].y) * pCamera->m_dScanResolution[g_pBase->m_nStageNo]; double dTheta = asin(dx/dy) * RAD_TO_DEGREE; if(dTheta > 1.0 || dTheta < -1.0) { g_pLog->DisplayMessage(_T("%s Calculate %d Stage Theta Limit over[degree 1] : %.3f -> 0"),PANEL_SIDE[emDim],g_pBase->m_nStageNo,dTheta); dTheta = 0; } short nTheta = (short)(-1*dTheta*1000); m_pGlassData->SetStageTheta(g_pBase->m_nStageNo,bLong,nTheta); g_pLog->DisplayMessage(_T("%s Calculate %d Stage Theta : %.3f, Send %d"),PANEL_SIDE[emDim],g_pBase->m_nStageNo,dTheta,nTheta); } BOOL CInspectCamera::CheckProfile(int iThread,DimensionDir emDim,stFrameIndex stFrame) { INS_EDGE_RESULT_INFO *pResInfo = NULL; if(m_pGlassData != NULL) pResInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pResInfo == NULL) return FALSE; if(pResInfo->nPreGlassEndLine <= 0) return FALSE; if(pResInfo->nPreGlassEndFrame > stFrame.nFrameIdx) return FALSE; CSingleLock cLock(&m_csThreadProfile); cLock.Lock(); if(m_bProfileFind[stFrame.nScanIdx] == TRUE) { cLock.Unlock(); return FALSE; } m_bProfileFind[stFrame.nScanIdx] = TRUE; m_nProfileFindFrameIdx[stFrame.nScanIdx] = stFrame.nFrameIdx; cLock.Unlock(); return TRUE; } void CInspectCamera::FinallyVisionProc(DimensionDir eDim, stFrameIndex stFrame) { /* code */ INS_EDGE_RESULT_INFO *pResInfo = m_pGlassData->GetEdgeResultInfo(eDim); BOOL isFindSuccess = pResInfo->bMark[0] && pResInfo->bMark[1]; if (!isFindSuccess) return; PROFILECHAMFER_RESULT_DATA* pProfileResult = m_pGlassData->GetProfileInspectData(); double lastTime = GetTickCount(); int idx = (int)(eDim); double dTime = fabs(lastTime - m_fInspectLastTime[idx]); if (dTime < 3000) return; CBlSideData *pBlSideData = BlVision_GetVisionRecipe()->getSideData(eDim); //Set Info pBlSideData->m_bInspection_Complete = TRUE; pBlSideData->m_bTopMark_Find = FALSE; pBlSideData->m_bBotMark_Find = FALSE; pBlSideData->m_bTopPoint_Find = FALSE; pBlSideData->m_bBotPoint_Find = FALSE; pBlSideData->m_mTopMark.x = 0; pBlSideData->m_mTopMark.y = 0; pBlSideData->m_mBotMark.x = 0; pBlSideData->m_mBotMark.y = 0; pBlSideData->m_mTopPoint.x = 0; pBlSideData->m_mTopPoint.y = 0; pBlSideData->m_mBotPoint.x = 0; pBlSideData->m_mBotPoint.y = 0; pBlSideData->m_nStartLine = 0; pBlSideData->m_nEndLine = 0; //Set MarkPos if (pResInfo->bMark[0]) { pBlSideData->m_bTopMark_Find = TRUE; pBlSideData->m_mTopMark.x = (int)(pResInfo->ptMark[0].x); pBlSideData->m_mTopMark.y = (int)(pResInfo->ptMark[0].y); } //Set MarkPos if (pResInfo->bMark[1]) { pBlSideData->m_bBotMark_Find = TRUE; pBlSideData->m_mBotMark.x = (int)(pResInfo->ptMark[1].x); pBlSideData->m_mBotMark.y = (int)(pResInfo->ptMark[1].y); } pBlSideData->m_nStartLine = pResInfo->nGlassStartLine; pBlSideData->m_nEndLine = pResInfo->nGlassEndLine; m_fInspectLastTime[idx] = lastTime; pBlSideData->m_vDispVisionResult.clear(); int nStageNo = (g_pBase->m_nStageNo > 1 || g_pBase->m_nStageNo < 0) ? 0 : g_pBase->m_nStageNo; BlVision_GetSoftVisionApp()->setStage(g_pBase->m_nStageNo); NgInfo ngArray[NG_INF_TOTAL]; int ngNum = BlVision_GetSoftVisionApp()->Execute((int)(eDim), ngArray); if (ngNum < 1) return; #if 0 typedef struct _NgInfo { int eDir; //检测面 int id; //检测ID CString strName; //名称 int xPosPxl; //X位置 int yPosPxl; //Y位置 double result; //结果 double xResult; //结果 double yResult; //结果 double minValue; //下限 double maxValue; //限制 int ngType; //缺陷类型 bool isRes; //检测结果 int x1; //缺陷的最小矩形 int y1; //缺陷的最小矩形 int x2; //缺陷的最小矩形 int y2; //缺陷的最小矩形 int eVision; //视觉算法 }NgInfo; #endif CString test_data_kdist; CString test_file_path; CFile test_module; CFileException test_exception; CString glass_id; glass_id.Format(_T("%s"), g_pBase->m_strHPanelID); CString strTime = g_pBase->m_strLoadingTime; std::string array_dir_use[9] = { "A TOP","B TOP", "C TOP", "D TOP", "A BOT", "B BOT","C BOT", "D BOT", "NONE" }; std::string array_type_use[7] = { "Unknown", "OK", "Chip", "Broken", "Grind", "Dist Cut", "Corner Dist" }; test_file_path.Format(_T("D:\\TestData\\%s_TestData.csv"), g_pBase->m_strLoadingDay); ::CreateDirectory(_T("D:\\TestData"), NULL); int nFrameWidth, nFrameHeight; GetFrameSize(m_iCamera, stFrame.nScanIdx, nFrameWidth, nFrameHeight); INS_EDGE_RESULT_INFO* pEdgeInfo = m_pGlassData->GetEdgeResultInfo(eDim); if (pEdgeInfo == NULL) return; int nSetEnd = pEdgeInfo->nGlassEndLine <= 0 ? pEdgeInfo->nPreGlassEndLine : pEdgeInfo->nGlassEndLine; int nOffset = 1000; int nStartLine = pEdgeInfo->nGlassStartLine - nOffset / 2; int nEndLine = nSetEnd + nOffset / 2; int nDefectIdx = 0; int nChamferAlarmResultCount = 0; int nProAlarmResultCount_MinDiff = 0; CINSPECT_JUDGEMENT_PARM* pJudge = m_pRecipe->GetJudgePrm(); for (int i = 0; i < ngNum; i++) { NgInfo ng = ngArray[i]; //侶쟁警속댄轎홧屈 if (ng.eDir > 8 || ng.eDir < 0) continue; if (ng.ngType > 7 || ng.ngType < 0) continue; if (ng.x1 < 0 || ng.y1 < nStartLine) continue; if (ng.x2 > nFrameWidth || ng.y2 > nEndLine) continue; if ((1 == ng.ngType || 2 == ng.ngType)/* && (ng.result < ng.minValue || ng.result > ng.maxValue)*/) { CvPoint2D32f ptDefec; ptDefec.x = ng.xPosPxl; ptDefec.y = ng.yPosPxl; CRect rectDefect(ng.x1, ng.y1, ng.x2, ng.y2); if (m_pDefectControl != NULL/* && nDefectIdx < 1*/) m_pDefectControl->ExtractDefect_KDist(eDim, m_iCamera, stFrame.nScanIdx, DefectLoc_RCUTChip, ptDefec, rectDefect, (int)(ng.result)); //nDefectIdx++; } //Hole if ((6 == ng.ngType)/* && (ng.result < ng.minValue || ng.result > ng.maxValue)*/) { CvPoint2D32f ptDefec; ptDefec.x = ng.xPosPxl; ptDefec.y = ng.yPosPxl; CRect rectDefect(ng.x1, ng.y1, ng.x2, ng.y2); std::vector vecList; vecList.push_back(rectDefect); CPoint ps = rectDefect.CenterPoint(); int offx = ng.x2 - ps.x; int offy = ng.y2 - ps.y; if (m_pDefectControl != NULL/* && nDefectIdx < 1*/) { m_pDefectControl->ExtractDefect_Hole(eDim, m_iCamera, stFrame.nScanIdx, vecList, ngNum, DefectLoc_Hole, CRect(offx, offy, 2 * offx, 2 * offy)); g_pLog->m_nHoleAlarmState = 1; } } if (5 == ng.ngType) { if (ng.eDir==4) { if (ng.id<6) { pProfileResult->ProfileData[59 + i].nProfileDiff = ng.result; if (ng.maxValueng.result) { pProfileResult->ProfileData[59+i].bUseJudge = TRUE; pProfileResult->ProfileData[59 + i].bProfileJudge = FALSE; if (pJudge->nMinProfileNgCount_MinDiff[ng.eDir] < abs(ng.result)) { nProAlarmResultCount_MinDiff = 1; } } } else if (5ProfileData[69 + i].nProfileDiff = ng.result; if (ng.maxValueng.result) { pProfileResult->ProfileData[69 + i].bUseJudge = TRUE; pProfileResult->ProfileData[69+i].bProfileJudge = FALSE; if (pJudge->nMinProfileNgCount_MinDiff[ng.eDir] < abs(ng.result)) { nProAlarmResultCount_MinDiff = 1; } } } else if (10ResultChamfer[49 + i][1].nChamferCalThick = ng.result; if (ng.maxValueng.result) { pProfileResult->ResultChamfer[49 + i][1].bUseJudge = TRUE; pProfileResult->ResultChamfer[49 + i][1].bChamferJudge = FALSE; nChamferAlarmResultCount = 1; } } else if (15ResultChamfer[59 +i][1].nChamferCalThick = ng.result; if (ng.maxValueng.result) { pProfileResult->ResultChamfer[59 + i][1].bUseJudge = TRUE; pProfileResult->ResultChamfer[59+i][1].bChamferJudge = FALSE; nChamferAlarmResultCount = 1; } } } if (ng.eDir == 5) { if (ng.id < 6) { pProfileResult->ProfileData[64 + i].nProfileDiff = ng.result; if (ng.maxValueng.result) { pProfileResult->ProfileData[64 + i].bUseJudge = TRUE; pProfileResult->ProfileData[64 + i].bProfileJudge = FALSE; if (pJudge->nMinProfileNgCount_MinDiff[ng.eDir] < abs(ng.result)) { nProAlarmResultCount_MinDiff = 1; } } } else if (5 < ng.id && ng.id < 11) { pProfileResult->ResultChamfer[59 + i][1].nChamferCalThick = ng.result; if (ng.maxValueng.result) { pProfileResult->ResultChamfer[59 + i][1].bUseJudge = TRUE; pProfileResult->ResultChamfer[59 + i][1].bChamferJudge = FALSE; nChamferAlarmResultCount = 1; } } } if (ng.eDir == 7) { if (ng.id < 6) { pProfileResult->ProfileData[69 + i].nProfileDiff = ng.result; if (ng.maxValueng.result) { pProfileResult->ProfileData[69 + i].bUseJudge = TRUE; pProfileResult->ProfileData[69 + i].bProfileJudge = FALSE; if (pJudge->nMinProfileNgCount_MinDiff[ng.eDir] < abs(ng.result)) { nProAlarmResultCount_MinDiff = 1; } } } else if (5 < ng.id && ng.id < 11) { pProfileResult->ResultChamfer[64 + i][1].nChamferCalThick = ng.result; if (ng.maxValueng.result) { pProfileResult->ResultChamfer[64 + i][1].bUseJudge = TRUE; pProfileResult->ResultChamfer[64+i][1].bChamferJudge = FALSE; nChamferAlarmResultCount = 1; } } } } if (5 == ng.ngType && (ng.result < ng.minValue || ng.result > ng.maxValue)) { CvPoint2D32f ptDefec; ptDefec.x = ng.xPosPxl; ptDefec.y = ng.yPosPxl; if (test_data_kdist.IsEmpty()) { test_data_kdist.AppendFormat(_T("%s,%s,%s,%s,%s,%f"), strTime, glass_id, array_type_use[ng.ngType], array_dir_use[ng.eDir], ng.strName, ng.result); } else { test_data_kdist.AppendFormat(_T("%s,%s,%f"), array_dir_use[ng.eDir], ng.strName, ng.result); } CRect rectDefect(ng.x1, ng.y1, ng.x2, ng.y2); if (m_pDefectControl != NULL) m_pDefectControl->ExtractDefect_KDist(eDim, m_iCamera, stFrame.nScanIdx, DefectLoc_KDist, ptDefec, rectDefect, (int)(ng.result)); } g_pLog->DisplayMessage(_T("%d FinallyVisionProc return[%d]: %s %s (%d,%d) (%d %d %d %d) %.2f") , g_pBase->m_strHPanelID, i, array_dir_use[ng.eDir], array_type_use[ng.ngType], ng.xPosPxl, ng.yPosPxl , ng.x1, ng.y1, ng.x2, ng.y2, ng.result); } test_data_kdist.Append(_T("\r\n")); test_module.Open(test_file_path, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate | CFile::shareDenyWrite | CFile::shareDenyRead, &test_exception); if (test_exception.m_cause == CFileException::none) { if (test_module.SeekToEnd() == 0L) { CString test_title; test_title.Format(_T("Time,Glass ID,eDir,Defect Type,Name,Result\r\n")); test_module.Write(test_title, test_title.GetLength() * sizeof(TCHAR)); } test_module.Write(test_data_kdist, test_data_kdist.GetLength() * sizeof(TCHAR)); test_module.Close(); } } BOOL CInspectCamera::CheckThreadEnd(int iThread,stFrameIndex stFrame,BOOL bProfile) { int iScan = stFrame.nScanIdx; CSingleLock myLoc(&m_csThreadEnd); myLoc.Lock(); if(m_iThreadEnd[iScan][iThread] == 1) { myLoc.Unlock(); return FALSE; } BOOL bEnd = FALSE; if(bProfile == TRUE || CheckProcessEnd(iThread,stFrame) == TRUE) { m_iThreadEnd[iScan][iThread] = 1; bEnd = TRUE; myLoc.Unlock(); } if(bEnd == TRUE) { DimensionDir emDim = GetDimension(iScan); g_pLog->DisplayMessage(_T("%s Thread %d Process End : Scan %d, Frame %d"),PANEL_SIDE[emDim],iThread,iScan,stFrame.nFrameIdx); if(GetThreadEndCount(iScan) == MAX_THREAD || bProfile == TRUE) { SaveFullImageCopy(iScan); // Waiting All Frame Finish.. /* DWORD dwTickCount = GetTickCount(); while(GetTickCount() - dwTickCount < 5000) { if(CheckAllFrameFinish(iScan) == TRUE) break; } */ myLoc.Lock(); { HANDLE hThread = GetCurrentThread(); SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST); g_pLog->DisplayMessage(_T("FinallyVisionProc took start - %s"), PANEL_SIDE[emDim]); auto startTime = std::chrono::high_resolution_clock::now(); FinallyVisionProc(emDim, stFrame); auto endTime = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast(endTime - startTime); g_pLog->DisplayMessage(_T("FinallyVisionProc took %lld milliseconds - %s"), duration.count(), PANEL_SIDE[emDim]); g_pLog->DisplayMessage(_T("FinallyVisionProc took end - %s"), PANEL_SIDE[emDim]); SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL); } myLoc.Unlock(); if(m_pII2S != NULL) m_pII2S->II2S_InspectionEnd(m_iCamera,iScan); g_pLog->DisplayMessage(_T("%s Scan Process All End"),PANEL_SIDE[emDim]); m_MsgJob.nState = 2; m_MsgJob.nSide = (int)emDim; ::SendMessage(m_hWndViewScan, WM_POCESS_STATUS,(WPARAM)&m_MsgJob, (int)emDim); } } myLoc.Unlock(); return bEnd; } int CInspectCamera::GetThreadEndCount(int iScan) { int nCount = 0; for(int i=0;iGetHolePrm(); if(pHolePrm->m_nTotalHoleCount <= 0) return ret; INS_EDGE_RESULT_INFO *pResInfo = NULL; if(m_pGlassData != NULL) pResInfo = m_pGlassData->GetEdgeResultInfo(emDim); std::vector vecReg; GetHoleRegionFrame(iThread,emDim,stFrame,vecReg); if((int)vecReg.size() <= 0) return ret; std::vector::iterator it; HOLE_INS_PARM HoleReg; for(it=vecReg.begin();it!=vecReg.end();it++) { HoleReg = *it; CRect rtArea = HoleReg.m_rtArea; rtArea.OffsetRect(CPoint((int)pResInfo->ptMark[0].x, (int)pResInfo->ptMark[0].y)); rtArea.InflateRect(100,100); IplImage* pTargetImage = cvCreateImage(cvSize(rtArea.Width(), rtArea.Height()), 8, 1); cvZero(pTargetImage); int nStartX = rtArea.left; int nStartY = rtArea.top; BYTE* pGrabBuffer = NULL; for(int i=0; i< rtArea.Height(); i++) { pGrabBuffer = m_pGrabber->GetFrameHeaderLine(stFrame.nScanIdx,nStartY+i); if(pGrabBuffer == NULL) continue; memcpy(&pTargetImage->imageData[pTargetImage->widthStep*i], pGrabBuffer+nStartX, sizeof(BYTE)*pTargetImage->width); } std::vector vecDefectList; IplImage* pViewImage = NULL; CPoint ptMatchingPos; GetHoleControl(iThread)->HoleInspect_TemplateMatching(&HoleReg, pTargetImage, vecDefectList, ptMatchingPos, &pViewImage); rtArea.OffsetRect(ptMatchingPos); if((int)vecDefectList.size() > 0) { m_pDefectControl->ExtractDefect_Hole(emDim,m_iCamera,stFrame.nScanIdx,vecDefectList,(int)vecDefectList.size(),DefectLoc_Hole,rtArea); } cvReleaseImage(&pTargetImage); cvReleaseImage(&pViewImage); } return ret; } CRect CInspectCamera::GetSplineInsRect_New(int iThread,DimensionDir emDim,CSize szMaster,CPoint nOffset,stFrameIndex stFrame,int iFindLine,BOOL &bFlip) { CRect rectIns(0,0,0,0); CvPoint2D32f ptAlign = CvPoint2D32f(0,0); BOOL bFindAlign = FALSE; int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); int nStartLine,nEndLine; nStartLine = nEndLine = 0; INS_EDGE_RESULT_INFO *pResInfo = NULL; if(m_pGlassData != NULL) { pResInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pResInfo != NULL) { if(pResInfo->bMark[0] == TRUE) { bFindAlign = pResInfo->bMark[0]; ptAlign = pResInfo->ptMark[0]; } nStartLine = pResInfo->nGlassStartLine; nEndLine = pResInfo->nPreGlassEndLine; } } bFlip = FALSE; if(bFindAlign == TRUE && stFrame.nFrameIdx > (int)((ptAlign.y)/nFrameHeight) && pResInfo->bSpline[0] == FALSE) { rectIns.left = 0; rectIns.right = nFrameWidth; rectIns.top = nStartLine-nOffset.y; rectIns.bottom = nStartLine + szMaster.cx; pResInfo->bSpline[0] = TRUE; } else if(nEndLine > 0 && stFrame.nFrameIdx == nEndLine/nFrameHeight) { DWORD dwTact = GetTickCount(); do { if(pResInfo == NULL) break; if(pResInfo->bMark[1] == TRUE) break; if(GetTickCount()-dwTact >= 3000) { g_pLog->DisplayMessage(_T("%s Spline Ins Rect timeout : %d=%d"),PANEL_SIDE[emDim],nEndLine/nFrameHeight,stFrame.nFrameIdx); break; } Sleep(0); } while (1); rectIns.left = 0; rectIns.right = nFrameWidth; rectIns.bottom = nEndLine + nOffset.y; rectIns.top = nEndLine-szMaster.cy; bFlip = TRUE; pResInfo->bSpline[1] = TRUE; } else return rectIns; AssertFrameRectWidth(rectIns,stFrame.nScanIdx); int nWidth = align_4byte(rectIns.Width()); rectIns.left = rectIns.right - nWidth; if(rectIns.Width() <= 0 || rectIns.Height() <= 0 || rectIns.left > rectIns.right || rectIns.top > rectIns.bottom) return rectIns; return rectIns; } DIT_RESULT CInspectCamera::FindSpline_New(int iThread,DimensionDir emDim,stFrameIndex stFrame,int iFindLine) { DIT_RESULT ret= DIT_CONV_SUCCESS; switch(emDim) { case DIMENSION_B: case DIMENSION_B_DN: case DIMENSION_D: case DIMENSION_D_DN: return ret; } pSTU_SPLINE_INS_PARM pSplinePrm = m_pRecipe->GetInspectInfo()->GetSplineIns(emDim); pSTU_SPLINE_CHIP_PARM pChipPrm = m_pRecipe->GetInspectInfo()->GetSplineChipIns(emDim); CETC_PARM *pEtc = m_pRecipe->GetEtcPrm(emDim); INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pSplinePrm == NULL || pEtc == NULL || pChipPrm == NULL || pChipPrm->m_bSplineChip == FALSE) return ret; int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); CSize szMaster = CSize(pSplinePrm->m_nStartCornerSize_Y,pSplinePrm->m_nEndCornerSize_Y); CPoint nOffset(100,100); BOOL bFlip; CRect rectIns; rectIns = GetSplineInsRect_New(iThread,emDim,szMaster,nOffset,stFrame,iFindLine,bFlip); if(rectIns.IsRectEmpty() == TRUE) { // g_pLog->DisplayMessage(_T("Cam[%d] Scan[%d] iThread[%d] Frame[%d] %s Spline No Region!"),GetCameraID(),pRecipe->GetScanIndex(),iThread,iFrame,strCutDiv); return ret; } if(rectIns.top < 0 || rectIns.left < 0) return ret; CvPoint2D32f ptSplineCen = GetSplineCenter(iThread,emDim,stFrame,bFlip); if(ptSplineCen.x <= 0 || ptSplineCen.y <= 0) { g_pLog->DisplayMessage(_T("%s iThread[%d] Frame[%d] %d Spline Fail! : Center is Null"),PANEL_SIDE[emDim],iThread,stFrame.nFrameIdx,bFlip); return ret; } CProfileInspect_Prm *pPrm = m_pRecipe->GetProfileParm(); int nRcpIndex[2],nRcpSize[2]; GetRCutProfileIndex(emDim,nRcpIndex,nRcpSize); if(nRcpIndex[0] < 0 || nRcpIndex[0] >= MAX_PROFILE_SETTING_COUNT) { g_pLog->DisplayMessage(_T("%s iThread[%d] Frame[%d] %d Spline Fail! : Radius is Null"),PANEL_SIDE[emDim],iThread,stFrame.nFrameIdx,bFlip); return ret; } double dRadius = (bFlip == FALSE) ? pSplinePrm->m_nStartCornerRadius_um : pSplinePrm->m_nEndCornerRadius_um; // pPrm->m_stProfile[nRcpIndex[0]].radius; int nSpIndex = 0; if(bFlip == TRUE) nSpIndex = 1; SetRCutDisplayImage(iThread,emDim,stFrame,rectIns,bFlip,dRadius,ptSplineCen); double dStartTheta = pChipPrm->m_dChipStartTheta; double dEndTheta = pChipPrm->m_dChipEndTheta; double dDistTheta = pChipPrm->m_dChipDistTheta; CEdgeProc EdgeProc; int nEdgeThres = m_pRecipe->GetInspectInfo()->m_nRCutThresGlass[emDim][nSpIndex]; int nChamferThres = m_pRecipe->GetInspectInfo()->m_nRCutThresChamfer[emDim][nSpIndex]; int nChamferSize = -1; pSTU_SPLINE_INS_PARM pSpline = m_pRecipe->GetInspectInfo()->GetSplineIns(emDim); if(pSpline != NULL ) nChamferSize = (nSpIndex == 0) ? pSpline->m_nStartChamferSize : pSpline->m_nEndChamferSize; std::vector stuRes; double dConv,dScan; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return ret; dConv = pCamera->m_dConvResolution[g_pBase->m_nStageNo]; dScan = pCamera->m_dScanResolution[g_pBase->m_nStageNo]; if(dConv <= 0 || dScan <= 0) return ret; if(dStartTheta >= dEndTheta) { g_pLog->DisplayMessage(_T("%s iThread[%d] Frame[%d] %d Spline Fail! : theta %.2f,%.2f"),PANEL_SIDE[emDim],iThread,stFrame.nFrameIdx,bFlip,dStartTheta,dEndTheta); return ret; } if(dDistTheta <= 0) dDistTheta = 5; int nCount=0,nSum=0; STU_SPLINE_RCut stuRcut; double dCalTheta; for(double iTheta=dStartTheta;iTheta<=dEndTheta;iTheta+=dDistTheta) { dCalTheta = iTheta; if(bFlip == TRUE) { dCalTheta = -1*iTheta; } COwnerBuffer imgProcess; ROI imgROI = GetRCutChipImage(iThread,emDim,stFrame,imgProcess,dCalTheta,ptSplineCen,dRadius); if(imgProcess.IsValidBuffer() == FALSE) continue; if(g_pStatus->GetInspectOption()->bSaveSpline == TRUE) { CString str; str.Format(_T("D:\\Image\\spline\\%s_%d_%.2f_Org.bmp"),PANEL_SIDE[emDim],bFlip,dCalTheta); CBufferAttach attach(str); attach.AttachToFile(imgProcess); } COwnerBuffer bufBin(imgProcess.GetDataWidth(),imgProcess.GetHeight()); CSize szImg(bufBin.GetDataWidth(),bufBin.GetHeight()); CopyMemory(bufBin.GetDataAddress(),imgProcess.GetDataAddress(),imgProcess.GetDataSize()); EdgeProc.ThresholdProcessing(bufBin.GetDataAddress(),szImg,nChamferThres,0); if(g_pStatus->GetInspectOption()->bSaveSpline == TRUE) { CString str; str.Format(_T("D:\\Image\\spline\\%s_%d_%.2f_Bin.bmp"),PANEL_SIDE[emDim],bFlip,dCalTheta); CBufferAttach attach(str); attach.AttachToFile(bufBin); } CSISEdgeFind EdgeFind; double dPitch = imgProcess.GetWidth()/5; if(dPitch < 10) dPitch = 10; int nGlassEdge = 0; if(EdgeFind.FindEdge_ToRight(&imgProcess,nGlassEdge,dPitch,nEdgeThres,0.3,0) == FALSE) { continue; } int iStart,iEnd; iStart = nGlassEdge-50; if(iStart < 0) iStart = 0; iEnd = iStart + 200; if(iEnd >= bufBin.GetWidth()) iEnd = bufBin.GetWidth()-1; for(int y=0;y 0) { nCount++; nSum+=stuRcut.width; } stuRes.push_back(stuRcut); } } int nAvg = 0; if(nCount > 0) nAvg = nSum/nCount; if(nChamferSize > 0) nAvg = nChamferSize; if(nAvg <= 0) return ret; STU_SPLINE_RCut stuDet; int nDefectCnt = 0,nMaxDefect = 0; for(int i=0;i nAvg && abs(stuRcut.width-nAvg)*dConv >= pChipPrm->m_nChipSize_Y) { nDefectCnt++; stuDet = stuRcut; } else { BOOL bDefect = FALSE; if(nDefectCnt > 0) { if(pChipPrm->m_bChipJudgeAnd == TRUE) { if(nDefectCnt*dConv >= pChipPrm->m_nChipSize_X) bDefect = TRUE; } else { bDefect = TRUE; } } if(bDefect == TRUE) { double dSin = sin(M_RADIAN(stuDet.dTheta)); double dCos = cos(M_RADIAN(stuDet.dTheta)); IMPOS2D pDefect,p1,posCen; posCen.u = ptSplineCen.x; posCen.v = ptSplineCen.y; pDefect.u = (stuDet.x2+stuDet.x1+1)/2+stuRcut.imgX; pDefect.v = (stuDet.y-nDefectCnt/2)+stuRcut.imgY; EdgeProc.IMRotatePoint(pDefect, &p1, posCen, dSin, dCos) ; CvPoint2D32f ptDef; ptDef.x = (float)p1.u; ptDef.y = (float)p1.v; m_pDefectControl->ExtractDefect_SpNew(emDim,m_iCamera,stFrame.nScanIdx,DefectLoc_Chip,ptDef,0,bFlip,(int)(nDefectCnt*dConv)); } nDefectCnt=0; } } BOOL bDefect = FALSE; if(nDefectCnt > 0) { if(pChipPrm->m_bChipJudgeAnd == TRUE) { if(nDefectCnt*dConv >= pChipPrm->m_nChipSize_X) bDefect = TRUE; } else { bDefect = TRUE; } } if(bDefect == TRUE) { double dSin = sin(M_RADIAN(stuDet.dTheta)); double dCos = cos(M_RADIAN(stuDet.dTheta)); IMPOS2D pDefect,p1,posCen; posCen.u = ptSplineCen.x; posCen.v = ptSplineCen.y; pDefect.u = (stuDet.x2+stuDet.x1+1)/2+stuRcut.imgX; pDefect.v = (stuDet.y-nDefectCnt/2)+stuRcut.imgY; EdgeProc.IMRotatePoint(pDefect, &p1, posCen, dSin, dCos) ; CvPoint2D32f ptDef; ptDef.x = (float)p1.u; ptDef.y = (float)p1.v; m_pDefectControl->ExtractDefect_SpNew(emDim,m_iCamera,stFrame.nScanIdx,DefectLoc_Chip,ptDef,0,bFlip,(int)(nDefectCnt*dConv)); } return ret; } ROI CInspectCamera::GetRCutChipImage(int iThread,DimensionDir emDim,stFrameIndex stFrame,COwnerBuffer &pTgt,double dTheata,CvPoint2D32f ptCen,double dRadius) { int nDistance; ROI aoi; double dSin,dCos; IMPOS2D posCen; int n4ByteWidth; LPBYTE lpBuffer; CEdgeProc EdgeProc; CSISBuffer bufOrg; const int nSearchArea = 200; const int nSearchHeight = 40; dSin = sin(M_RADIAN(dTheata)); dCos = cos(M_RADIAN(dTheata)); posCen.u = ptCen.x; posCen.v = ptCen.y; nDistance = (int)GetRadus2Distance(emDim,stFrame, dTheata,dRadius,dSin,dCos); aoi.u1 = aoi.u2 = aoi.v1 = aoi.v2 = 0; lpBuffer = m_pGrabber->GetFrameHeaderLine(stFrame.nScanIdx,0); if(lpBuffer == NULL) return aoi; int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); bufOrg.SetBuffer(lpBuffer,nFrameWidth,g_pBase->m_pMemFrameNo[m_iCamera]*nFrameHeight); aoi.u1 = (int)ROUND(posCen.u) - nDistance - nSearchArea; aoi.u2 = (int)ROUND(posCen.u) - nDistance + nSearchArea; aoi.v1 = (int)ROUND(posCen.v) - nSearchHeight; aoi.v2 = (int)ROUND(posCen.v) + nSearchHeight; n4ByteWidth = align_4byte(EdgeProc.IMROIWidth(aoi)); aoi.u2 = aoi.u1 + n4ByteWidth; pTgt.SetSize(EdgeProc.IMROIWidth(aoi),EdgeProc.IMROIHeight(aoi)); // 22.6.13 memset(pTgt.GetDataAddress(0,0), 255, pTgt.GetDataSize()); EdgeProc.IMGetRotImg(aoi,bufOrg,pTgt,posCen,dSin,dCos); return aoi; } CvPoint2D32f CInspectCamera::GetSplineCenter(int iThread,DimensionDir emDim,stFrameIndex stFrame,BOOL bBot) { CvPoint2D32f ptCen = CvPoint2D32f(0,0); CvPoint2D32f ptAlign = CvPoint2D32f(0,0); int nIndex = 0; if(bBot == TRUE) nIndex = 1; if(m_pGlassData != NULL) { INS_EDGE_RESULT_INFO *pResInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pResInfo != NULL) { if(pResInfo->bMark[nIndex] == TRUE) { ptAlign = pResInfo->ptMark[nIndex]; } } } if(ptAlign.x <= 0 || ptAlign.y <= 0) return ptCen; CProfileInspect_Prm *pPrm = m_pRecipe->GetProfileParm(); double AlignX,AlignY; double dOrgOffsetX,dOrgOffsetY; double dConv,dScan; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return ptCen; dConv = pCamera->m_dConvResolution[g_pBase->m_nStageNo]; dScan = pCamera->m_dScanResolution[g_pBase->m_nStageNo]; if(dConv <= 0 || dScan <= 0) return ptCen; if(nIndex == 0) { dOrgOffsetX = (double)pPrm->m_nOriginAlignKeyOffsetX[emDim]/dConv; dOrgOffsetY = (double)pPrm->m_nOriginAlignKeyOffsetY[emDim]/dScan; } else if(nIndex == 1) { dOrgOffsetX = (double)pPrm->m_nOriginAlignKeyOffsetDwX[emDim]/dConv; dOrgOffsetY = (double)pPrm->m_nOriginAlignKeyOffsetDwY[emDim]/dScan; } AlignX = ptAlign.x; AlignY = ptAlign.y; ptCen.x = (float)(AlignX + dOrgOffsetX); ptCen.y = (float)(AlignY + dOrgOffsetY); return ptCen; } void CInspectCamera::SetRCutDisplayImage(int iThread,DimensionDir emDim,stFrameIndex stFrame,CRect rectIns,BOOL bFlip,double dRadius,CvPoint2D32f ptSplineCen) { switch(emDim) { case DIMENSION_A_DN: case DIMENSION_C_DN: case DIMENSION_B: case DIMENSION_B_DN: case DIMENSION_D: case DIMENSION_D_DN: return; } double dConv,dScan; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return; dConv = pCamera->m_dConvResolution[g_pBase->m_nStageNo]; dScan = pCamera->m_dScanResolution[g_pBase->m_nStageNo]; if(dConv <= 0 || dScan <= 0) return; int nDistance = (int)(dRadius/dConv); pINSPECTSPLINE_BUFFER pImgBuf = NULL; if(bFlip == FALSE) pImgBuf = GetSplineBuffer(0); else pImgBuf = GetSplineBuffer(1); if(m_iCamera == 0) { if(bFlip == FALSE) pImgBuf->ePosition = INSPECTSPLINE_BUFFER::SPLINE_BOTTOMLEFT; else pImgBuf->ePosition = INSPECTSPLINE_BUFFER::SPLINE_BOTTOMRIGHT; } else { if(bFlip == FALSE) pImgBuf->ePosition = INSPECTSPLINE_BUFFER::SPLINE_TOPLEFT; else pImgBuf->ePosition = INSPECTSPLINE_BUFFER::SPLINE_TOPRIGHT; } pImgBuf->iCam = m_iCamera; pImgBuf->iScan = stFrame.nScanIdx; pImgBuf->iType = (int)STU_SPLINE_RESULT::SPLINE_RCUT; pImgBuf->bPos = bFlip; int nOffset = 100; CRect rect; if(bFlip == FALSE) { rect.left = (int)ptSplineCen.x-nDistance-nOffset; rect.right = rect.left + (int)(nDistance*1.5) + nOffset; rect.top = (int)ptSplineCen.y-nDistance-nOffset; rect.bottom = (int)ptSplineCen.y+nOffset; } else { rect.left = (int)ptSplineCen.x-nDistance-nOffset; rect.right = rect.left + (int)(nDistance*1.5) + nOffset; rect.bottom = (int)ptSplineCen.y +nDistance+nOffset; rect.top = (int)ptSplineCen.y-nOffset; } rect.right = rect.left + align_4byte(rect.Width()); int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); if(rect.top < 0 || rect.right >= nFrameWidth || rect.left < 0) return; COwnerBuffer sisRcut(rect.Width(),rect.Height()); LPBYTE lpBuffer = m_pGrabber->GetFrameHeaderLine(stFrame.nScanIdx,rect.top); CRect rectCopy = rect; rectCopy.OffsetRect(0,-rectCopy.top); CopyRectImg(lpBuffer,sisRcut.GetDataAddress(),CSize(nFrameWidth,sisRcut.GetHeight()*2),rectCopy); pImgBuf->SetBuffer(sisRcut.GetDataAddress(),CSize(sisRcut.GetWidth(),sisRcut.GetHeight()),INSPECTSPLINE_BUFFER::SPLINE_TYPE_ORG); // Org } BOOL CInspectCamera::CopyRectImg(LPBYTE pOrg,LPBYTE pTgt,CSize szImg,CRect &rectIns) { if(rectIns.Width() > szImg.cx || rectIns.bottom >= szImg.cy || rectIns.top < 0 || rectIns.top > rectIns.bottom || rectIns.bottom < 0) return FALSE; int v; int dv = 0; for(v=rectIns.top;vIsLoadMasterImg() == FALSE) { return ret; } pSTU_SPLINE_INS_PARM pSplinePrm = m_pRecipe->GetInspectInfo()->GetSplineIns(emDim); pSTU_SPLINE_CHIP_PARM pChipPrm = m_pRecipe->GetInspectInfo()->GetSplineChipIns(emDim); CETC_PARM *pEtc = m_pRecipe->GetEtcPrm(emDim); INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pSplinePrm == NULL || pEtc == NULL || pChipPrm == NULL || pSplinePrm->m_bSpline == FALSE || pEdgeInfo == NULL) return ret; CString strCutDiv; CSize szMaster = pSplineModel->GetMasterDataSize(); CPoint nOffset(100,100); BOOL bFlip; CRect rectIns; rectIns = GetSplineInsRect(szMaster,nOffset,stFrame,iFindLine,pEdgeInfo,bFlip); if(rectIns.IsRectEmpty() == TRUE) { return ret; } if(rectIns.top < 0 || rectIns.left < 0) return ret; GetSplineControl(iThread)->SetMasterModelData(pSplineModel); double dCovRes,dScanRes; int nFrameHeight,nFrameWidth; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(g_pBase->m_nStageNo >= 0 && g_pBase->m_nStageNo <= 1) { dCovRes = pCamera->m_dConvResolution[g_pBase->m_nStageNo]; dScanRes = pCamera->m_dScanResolution[g_pBase->m_nStageNo]; } else { dCovRes = pCamera->m_dConvResolution[0]; dScanRes = pCamera->m_dScanResolution[0]; } GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); int iTopPixel = rectIns.top; int iLetPixel = rectIns.left; LPBYTE lpHeader = m_pGrabber->GetFrameHeaderLine(stFrame.nScanIdx,rectIns.top); CSISBuffer pSPBuf(lpHeader,nFrameWidth,rectIns.Height()); if(lpHeader == NULL || pSPBuf.IsValidBuffer() == FALSE) { g_pLog->DisplayMessage(_T("%s iThread[%d] Frame[%d] %s Spline Image Fail: %d [%d,%d,%d,%d]"),PANEL_SIDE[emDim],iThread,stFrame.nFrameIdx,strCutDiv ,rectIns.top/nFrameHeight,rectIns.left,rectIns.top,rectIns.right,rectIns.bottom); return ret; } SPLINE_ERR_MSG errMsg = SPLINE_INS_SUCESS; g_pLog->DisplayMessage(_T("%s iThread[%d] Frame[%d] %s Spline Inspect Start"),PANEL_SIDE[emDim],iThread,stFrame.nFrameIdx,strCutDiv); rectIns.OffsetRect(0,-rectIns.top); if(pSplinePrm->m_bSpline == TRUE) errMsg = GetSplineControl(iThread)->InspectSpline(pSPBuf,rectIns,pSplinePrm->m_DetMinDist,pSplinePrm->m_nLineThres,bFlip,g_pStatus->GetInspectOption()->bSaveSpline,TRUE); //g_pLog->DisplayMessage(_T("Cam[%d] Scan[%d] iThread[%d] Frame[%d] %s Spline Inspect. 02"),GetCameraID(),pRecipe->GetScanIndex(),iThread,iFrame,strCutDiv); CSISBuffer *pChipBuf = GetSplineControl(iThread)->GetSplineOrgImg(); if(pChipBuf->IsValidBuffer() == FALSE) pChipBuf = &pSPBuf; int iLeftLine = GetSplineFindEdge(pChipBuf,pEtc,iFindLine,100); SplineChipProcess(GetSplineControl(iThread),pChipBuf,iThread,emDim,stFrame,iLeftLine,bFlip,iTopPixel,iLetPixel,strCutDiv); SaveSplineImage(pSPBuf,GetSplineControl(iThread),pSplinePrm,iThread,emDim,stFrame,dCovRes,errMsg,bFlip); g_pLog->DisplayMessage(_T("%s iThread[%d] Frame[%d] %s Spline Inspect End"),PANEL_SIDE[emDim],iThread,stFrame.nFrameIdx,strCutDiv); return ret; } void CInspectCamera::SaveSplineImage(CSISBuffer &pSPBuf,CSplineInspect *pSplineIns,pSTU_SPLINE_INS_PARM pSplinePrm, int iThread,DimensionDir emDim,stFrameIndex stFrame,double dConvRes,SPLINE_ERR_MSG errMsg,BOOL bFlip) { if(emDim == DIMENSION_A || emDim == DIMENSION_C) { if(errMsg == SPLINE_INS_SUCESS) { if(g_pStatus->GetInspectOption()->bSaveSpline == TRUE) { CSISBuffer *pResBuf = GetSplineControl(iThread)->GetSplineResImg(); pINSPECTSPLINE_BUFFER pImgBuf = NULL; if(bFlip == FALSE) pImgBuf = GetSplineBuffer(0); else pImgBuf = GetSplineBuffer(1); if(m_iCamera == 0) { if(bFlip == FALSE) pImgBuf->ePosition = INSPECTSPLINE_BUFFER::SPLINE_BOTTOMLEFT; else pImgBuf->ePosition = INSPECTSPLINE_BUFFER::SPLINE_BOTTOMRIGHT; } else { if(bFlip == FALSE) pImgBuf->ePosition = INSPECTSPLINE_BUFFER::SPLINE_TOPLEFT; else pImgBuf->ePosition = INSPECTSPLINE_BUFFER::SPLINE_TOPRIGHT; } pImgBuf->iCam = m_iCamera; pImgBuf->iScan = stFrame.nScanIdx; if((int)pSplinePrm->m_emSPType == EM_SP_RCUT) pImgBuf->iType = STU_SPLINE_RESULT::SPLINE_RCUT; else pImgBuf->iType = STU_SPLINE_RESULT::SPLINE_CCUT; pImgBuf->bPos = bFlip; pImgBuf->SetBuffer(pSPBuf.GetDataAddress(),CSize(pSPBuf.GetWidth(),pSPBuf.GetHeight()),INSPECTSPLINE_BUFFER::SPLINE_TYPE_ORG); // Org } g_pLog->DisplayMessage(_T( "%s iThread[%d] Frame[%d] Spline success"),PANEL_SIDE[emDim],iThread,stFrame.nFrameIdx); } else { if(g_pStatus->GetInspectOption()->bSaveSpline== TRUE) { pINSPECTSPLINE_BUFFER pImgBuf = NULL; if(bFlip == FALSE) pImgBuf = GetSplineBuffer(0); else pImgBuf = GetSplineBuffer(1); pImgBuf->iCam = m_iCamera; pImgBuf->iScan = stFrame.nScanIdx; if((int)pSplinePrm->m_emSPType == EM_SP_RCUT) pImgBuf->iType = STU_SPLINE_RESULT::SPLINE_RCUT; else pImgBuf->iType = STU_SPLINE_RESULT::SPLINE_CCUT; pImgBuf->bPos = bFlip; pImgBuf->Clear(INSPECTSPLINE_BUFFER::SPLINE_TYPE_ORG); // Org pImgBuf->Clear(INSPECTSPLINE_BUFFER::SPLINE_TYPE_RES); // Res } CString str; switch(errMsg) { case ERR_SPLINE_EMPTYMASTER: str = _T("No Master Image"); break; case ERR_SPLINE_EDGEPROCESS: str = _T("Edge error"); break; case ERR_SPLINE_LABELPROCESS: str = _T("Label error"); break; case ERR_SPLINE_LABELCOUNT: str = _T("Label Count error"); break; case ERR_SPLINE_INDEXFIND: str = _T("Max Index Find error"); break; case ERR_SPLINE_LABELPOSITION: str = _T("Label Position error"); break; case ERR_SPLINE_POSITIONNULL: str = _T("Position Null"); break; case ERR_SPLINE_INS_CNT_SHORT: str = _T("line Count error"); break; case ERR_SPLINE_IMAGE_NULL: str = _T("Image Null"); break; default: str = "Error"; break; } g_pLog->DisplayMessage(_T( "%s iThread[%d] Frame[%d] Spline inspect fail! : %s"),PANEL_SIDE[emDim],iThread,stFrame.nFrameIdx,str); } } } BOOL CInspectCamera::SplineChipProcess(CSplineInspect *pInsSpline,CSISBuffer *imgOrg,int iThread,DimensionDir emDim,stFrameIndex stFrame,int iFindLine,BOOL bFlip,int iTopPixel,int iLeftPixel,CString strCutDiv) { if(imgOrg->IsValidBuffer() == FALSE || pInsSpline == NULL) return FALSE; pSTU_SPLINE_INS_PARM pSpInsPrm = m_pRecipe->GetInspectInfo()->GetSplineIns(emDim); pSTU_SPLINE_CHIP_PARM pSPChipPrm = m_pRecipe->GetInspectInfo()->GetSplineChipIns(emDim); if(pSPChipPrm->m_bSplineChip == FALSE) return FALSE; int nChipThres = pSPChipPrm->m_nChipThres; ////////////////////////////////////////////////////////////////////////// // 流急 备埃. CPoint pointLeft; CRect rectChip; CEdgeFind EdgeFind; CETC_PARM *pEtc = m_pRecipe->GetEtcPrm(emDim); pointLeft.x = GetSplineFindEdge(imgOrg,pEtc,iFindLine,10); pointLeft.y = pSpInsPrm->m_nStartCornerSize_Y; // pSpInsPrm->m_nYPos; DWORD tacttime = GetTickCount(); SplineChip_Curve(pInsSpline,imgOrg,iThread,emDim,stFrame,pointLeft,nChipThres,bFlip,iTopPixel,iLeftPixel,strCutDiv); return TRUE; } BOOL CInspectCamera::SplineChip_Curve(CSplineInspect *pInsSpline,CSISBuffer *imgOrg,int iThread,DimensionDir emDim,stFrameIndex stFrame,CPoint &pointLeft,int nChipThres,BOOL bFlip,int iTopPixel,int iLeftPixel,CString strCutDiv) { std::vector *pSPChipPos = m_pRecipe->GetInspectInfo()->GetSplineChipPos(emDim); if(pSPChipPos == NULL || (int)pSPChipPos->size() <= 0) return TRUE; int iReg = 0; int nModelCnt,nModSize; CHIP_INS_REGION insRegion,insRegTmp; std::vector::iterator it; CChipBlob *pBlobResult = NULL; int nDefectCount; pSTU_SPLINE_INS_PARM pSpInsPrm = m_pRecipe->GetInspectInfo()->GetSplineIns(emDim); pSTU_SPLINE_CHIP_PARM pSPChipPrm = m_pRecipe->GetInspectInfo()->GetSplineChipIns(emDim); CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); double dScanRes = pCamera->m_dScanResolution[g_pBase->m_nStageNo]; #define DIVIDE_SP_REGION_SIZE 80 int nMinStartPos = INT_MAX; CINSPECT_JUDGEMENT_PARM *pJudge = m_pRecipe->GetJudgePrm(); int nJudgeW,nJudgeH; BOOL bAnd; EM_CHIPPING_COMP eComp; if(pCamera->m_enCamDir == CAMDIR_BOTTOM) { nJudgeW = pJudge->defect[emDim][0].Chip_Side.szDefect.cx; nJudgeH = pJudge->defect[emDim][0].Chip_Side.szDefect.cy; bAnd = pJudge->defect[emDim][0].Chip_Side.bConditionAND; } else { nJudgeW = pJudge->defect[emDim][0].Chip_CF.szDefect.cx; nJudgeH = pJudge->defect[emDim][0].Chip_CF.szDefect.cy; bAnd = pJudge->defect[emDim][0].Chip_CF.bConditionAND; } if(bAnd == TRUE) eComp = CHIP_AND; else eComp = CHIP_OR; int iSaveIdx = 0; g_pLog->DisplayMessage(_T( "%s Frame[%d] %s Spline Chip Curve Inspect Start."),PANEL_SIDE[emDim],stFrame.nFrameIdx,strCutDiv); for(it=pSPChipPos->begin();it!=pSPChipPos->end();it++,iReg++) { if(pSpInsPrm->m_emSPType == EM_SP_CCUT) if(it->emPos != STU_SPLINE_CHIP_POS::MAKE_IMAGE_POS_CCUT) continue; else if(it->emPos != STU_SPLINE_CHIP_POS::MAKE_IMAGE_POS_RCUT) continue; insRegion.bUse = it->bUse; insRegion.nStartX = it->nStartX; insRegion.nEndX = it->nEndX; insRegion.nThres = it->nThres; insRegion.nFindThres = it->nFindThres; insRegion.nInsRange = it->nInsRange; insRegion.nSkipDist = it->nSkipDist; insRegion.emMethod = (MAKE_IMAGE_METHOD)it->emMethod; insRegion.emDir = (SPLINE_INS_DIR)it->emDir; if(insRegion.bUse == FALSE) continue; nModelCnt = abs(insRegion.nStartX-insRegion.nEndX)/DIVIDE_SP_REGION_SIZE; nModSize = abs(insRegion.nStartX-insRegion.nEndX)%DIVIDE_SP_REGION_SIZE; if(nMinStartPos > insRegion.nStartX) nMinStartPos = insRegion.nStartX; int iCnt = 0; if(nModelCnt > 0) { int nChipMargin = 20; insRegTmp = insRegion; for(iCnt=0;iCntGetWidth() <= insRegTmp.nEndX) insRegTmp.nEndX = imgOrg->GetWidth()-1; // g_pLog->DisplayMessage(_T( "%s Frame[%d] %s Spline Chip Curve Inspect 02._%d"),PANEL_SIDE[pRecipe->GetDimension()],iFrame,strCutDiv,iCnt); COwnerBuffer pSpChipRes; pInsSpline->InspectSplineChip(*imgOrg,pointLeft.x,insRegTmp,dScanRes,pSPChipPrm->m_nChipSize_Y,pSpChipRes,bFlip,iSaveIdx,g_pStatus->GetInspectOption()->bSaveSplineChip); iSaveIdx++; pBlobResult = pInsSpline->GetResultDefect(); nDefectCount = pInsSpline->GetResultDefectCount(); if(pBlobResult != NULL && nDefectCount > 0) { m_pDefectControl->ExtractDefect_SplineChip(emDim,m_iCamera,stFrame.nScanIdx,pBlobResult,nDefectCount,DefectLoc_Chip,CPoint(iLeftPixel,iTopPixel),(int)pSpInsPrm->m_emSPType,bFlip,nJudgeW,nJudgeH,eComp); } // g_pLog->DisplayMessage(_T( "%s Frame[%d] %s Spline Chip Curve Inspect 03.[%d] "),PANEL_SIDE[pRecipe->GetDimension()],iFrame,strCutDiv,iCnt,m_pInspectOpt->bSaveSplineChip); } if(nModSize > 0) { insRegTmp.nEndX = insRegion.nEndX; insRegTmp.nStartX = insRegTmp.nEndX-nModSize; // g_pLog->DisplayMessage(_T( "%s Frame[%d] %s Spline Chip Curve Inspect 04. Mod"),PANEL_SIDE[pRecipe->GetDimension()],iFrame,strCutDiv); COwnerBuffer pSpChipRes; pInsSpline->InspectSplineChip(*imgOrg,pointLeft.x,insRegTmp,dScanRes,pSPChipPrm->m_nChipSize_Y,pSpChipRes,bFlip,iSaveIdx,g_pStatus->GetInspectOption()->bSaveSplineChip); iSaveIdx++; pBlobResult = pInsSpline->GetResultDefect(); nDefectCount = pInsSpline->GetResultDefectCount(); if(pBlobResult != NULL && nDefectCount > 0) { m_pDefectControl->ExtractDefect_SplineChip(emDim,m_iCamera,stFrame.nScanIdx,pBlobResult,nDefectCount,DefectLoc_Chip,CPoint(iLeftPixel,iTopPixel),(int)pSpInsPrm->m_emSPType,bFlip,nJudgeW,nJudgeH,eComp); } // g_pLog->DisplayMessage(_T( "%s Frame[%d] %s Spline Chip Curve Inspect 05.Mod"),PANEL_SIDE[pRecipe->GetDimension()],iFrame,strCutDiv); } } else { // g_pLog->DisplayMessage(_T( "%s Frame[%d] %s Spline Chip Curve Inspect 06. Mod : %d"),PANEL_SIDE[pRecipe->GetDimension()],iFrame,strCutDiv,nModelCnt); COwnerBuffer pSpChipRes; pInsSpline->InspectSplineChip(*imgOrg,pointLeft.x,insRegion,dScanRes,pSPChipPrm->m_nChipSize_Y,pSpChipRes,bFlip,iSaveIdx,g_pStatus->GetInspectOption()->bSaveSplineChip); iSaveIdx++; pBlobResult = pInsSpline->GetResultDefect(); nDefectCount = pInsSpline->GetResultDefectCount(); if(pBlobResult != NULL && nDefectCount > 0) { m_pDefectControl->ExtractDefect_SplineChip(emDim,m_iCamera,stFrame.nScanIdx,pBlobResult,nDefectCount,DefectLoc_Chip,CPoint(iLeftPixel,iTopPixel),(int)pSpInsPrm->m_emSPType,bFlip,nJudgeW,nJudgeH,eComp); } } } g_pLog->DisplayMessage(_T("%s Frame[%d] %s Spline Chip Curve Inspect complete."),PANEL_SIDE[emDim],stFrame.nFrameIdx,strCutDiv); //nMinStartPos = 30; if(nMinStartPos != INT_MAX || pSPChipPrm->m_bCornerUse == FALSE || pSPChipPrm->m_nCornerRange <= 10) { insRegTmp.Reset(); insRegTmp.nStartX = -20; insRegTmp.nEndX = nMinStartPos+100; insRegTmp.nCornerYSize = pSPChipPrm->m_nCornerRange; insRegTmp.nFindThres = pSPChipPrm->m_nCornerThres; insRegTmp.emMethod = MAKE_METHOD_REGRESSION; insRegTmp.bUse = pSPChipPrm->m_bCornerUse; COwnerBuffer pSpChipRes; double dAvgThick = pInsSpline->InspectSplineChip(*imgOrg,pointLeft.x,insRegTmp,dScanRes,pSPChipPrm->m_nChipSize_Y,pSpChipRes,bFlip,0,g_pStatus->GetInspectOption()->bSaveSplineChip); pBlobResult = pInsSpline->GetResultDefect(); nDefectCount = pInsSpline->GetResultDefectCount(); if(pBlobResult != NULL && nDefectCount > 0) { m_pDefectControl->ExtractDefect_SplineChip(emDim,m_iCamera,stFrame.nScanIdx,pBlobResult,nDefectCount,DefectLoc_Chip,CPoint(iLeftPixel,iTopPixel),(int)pSpInsPrm->m_emSPType,bFlip,nJudgeW,nJudgeH,eComp); } } return TRUE; } int CInspectCamera::GetSplineFindEdge(CSISBuffer *pSPBuf,CETC_PARM *pEtc,int iFindLine,int nOffset) { CRect rectEdge; int iEdgeLine; rectEdge.left = 0; rectEdge.right = align_4byte(rectEdge.left + pEtc->m_nGlassFindWidth_Left); rectEdge.top = pSPBuf->GetHeight()-nOffset; rectEdge.bottom = pSPBuf->GetHeight(); #define SPLINE_EDGE_AUTO_RATIO 0.3 #define SPLINE_EDGE_AUTO_PITCH 15 #define SPLINE_EDGE_CONTINUE 3 CEdgeFind EdgeFind; iEdgeLine = EdgeFind.Find_LeftEdge(pSPBuf,SPLINE_EDGE_AUTO_PITCH,pEtc->m_nGlassFindthres_Left,SPLINE_EDGE_AUTO_RATIO,SPLINE_EDGE_CONTINUE); if(iEdgeLine <= 0) iEdgeLine = iFindLine; return iEdgeLine; } CRect CInspectCamera::GetSplineInsRect(CSize szMaster,CPoint nOffset,stFrameIndex stFrame,int iFindLine,INS_EDGE_RESULT_INFO *pEdgeInfo,BOOL &bFlip) { CRect rectIns(0,0,0,0); int nFrameHeight,nFrameWidth; if(m_bSplineInspect[0] == TRUE && m_bSplineInspect[1] == TRUE) return rectIns; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); int nStartFrame = (pEdgeInfo->nGlassStartLine+szMaster.cy+nOffset.y)/nFrameHeight+1; bFlip = FALSE; if(m_bSplineInspect[0] == FALSE && stFrame.nFrameIdx >= nStartFrame) { m_bSplineInspect[0] = TRUE; rectIns.left = 0; rectIns.right = nFrameWidth; rectIns.top = pEdgeInfo->nGlassStartLine-nOffset.y; rectIns.bottom = pEdgeInfo->nGlassStartLine + szMaster.cy; } else if(m_bSplineInspect[1] == FALSE && pEdgeInfo->nGlassEndFrame > 0) { m_bSplineInspect[1] = TRUE; rectIns.left = 0; rectIns.right = nFrameWidth; rectIns.bottom = pEdgeInfo->nGlassEndLine + nOffset.y; rectIns.top = pEdgeInfo->nGlassEndLine-szMaster.cy; bFlip = TRUE; } else return rectIns; // AssertFrameRectWidth(rectIns,stFrame.nScanIdx); // int nWidth = align_4byte(rectIns.Width()); // rectIns.left = rectIns.right - nWidth; // // if(rectIns.Width() <= 0 || rectIns.Height() <= 0 || rectIns.left > rectIns.right || rectIns.top > rectIns.bottom) // return rectIns; return rectIns; } void CInspectCamera::SaveFullImageCopy(int iScan) { if(m_pGlassData == NULL) return; if(m_pGrabber == NULL) return; CSingleLock cLock(&m_csSaveimage); cLock.Lock(); DimensionDir eDim = GetDimension(iScan); INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(eDim); if(pEdgeInfo == NULL) return; int nSetEnd = pEdgeInfo->nGlassEndLine<=0?pEdgeInfo->nPreGlassEndLine:pEdgeInfo->nGlassEndLine; int nOffset = 1000; int nStartLine = pEdgeInfo->nGlassStartLine -nOffset/2; int nEndLine = nSetEnd +nOffset/2; int nFullSize = nSetEnd-pEdgeInfo->nGlassStartLine+nOffset; if(nStartLine < 0) { nStartLine = 0; } if(nFullSize <= 0) return; if(nFullSize <= nStartLine) return; pINSPECTFULLIMAGE_BUFFER pImgBuf = GetFullImgBuffer(iScan); int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,iScan,nFrameWidth,nFrameHeight); if(nFrameWidth <= 0 || nFrameHeight <= 0) return; if(nFullSize/nFrameHeight > m_pGrabber->GetFrameBuffer()->GetFrameCount()) nFullSize = (m_pGrabber->GetFrameBuffer()->GetFrameCount()-1)*nFrameHeight; if(nFullSize <= 100) return; pImgBuf->iCamIdx = m_iCamera; pImgBuf->iScanIdx = iScan; pImgBuf->SetSize(CSize(nFrameWidth,nFullSize)); g_pLog->DisplayMessage(_T( "%s Full Image Memory Copy Start : Start[%d]-End[%d]"),PANEL_SIDE[eDim],nStartLine,nEndLine); LPBYTE lpHeader = m_pGrabber->GetFrameHeaderLine(iScan,nStartLine); if(lpHeader == NULL) { g_pLog->DisplayMessage(_T( "%s Full Image Memory Copy fail : Start[%d]-End[%d]"),PANEL_SIDE[eDim],nStartLine,nEndLine); return; } CopyMemory(pImgBuf->pImage,lpHeader,sizeof(BYTE)*pImgBuf->szImage.cx*pImgBuf->szImage.cy); cLock.Unlock(); g_pLog->DisplayMessage(_T( "%s Full Image Memory Copy complete : Start[%d]-End[%d]"),PANEL_SIDE[eDim],nStartLine,nEndLine); } BOOL CInspectCamera::CheckProcessEnd(int iThread,stFrameIndex stFrame) { if(m_pGlassData == NULL) return FALSE; if(m_pGrabber == NULL) return FALSE; INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(GetDimension(stFrame.nScanIdx)); if(pEdgeInfo == NULL) return FALSE; if(g_pStatus->GetGrabFrameCount(stFrame.nScanIdx)-1 <= stFrame.nFrameIdx) return TRUE; if(pEdgeInfo->nPreGlassEndFrame+MAX_THREAD <= stFrame.nFrameIdx) return TRUE; // BOOL bRemain = m_pGrabber->FindGrabIdx(stFrame.nScanIdx,pEdgeInfo->nPreGlassEndFrame+MAX_THREAD); // if(bRemain == FALSE) // { // return TRUE; // } return FALSE; } void CInspectCamera::ScanRegionSet(stFrameIndex stFrame) { SetGrabEnd(stFrame.nScanIdx); MakeAlignRegion(stFrame.nScanIdx); PresetChamferPrm(stFrame.nScanIdx,GetDimension(stFrame.nScanIdx)); MakeChamferRegion(stFrame.nScanIdx); MakeHoleRegion(stFrame.nScanIdx); } BOOL CInspectCamera::CheckStartLineFrame(DimensionDir eDim,int iFrame) { INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(eDim); if(pEdgeInfo == NULL) return FALSE; if(pEdgeInfo->nGlassStartFrame > iFrame) return FALSE; return TRUE; } void CInspectCamera::GetChamferRect(stFrameIndex stFrame,DimensionDir eDim,std::vector &vecIns) { INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(eDim); if(pEdgeInfo == NULL) return; int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); CRect rtChamfer = pEdgeInfo->rtChamfer; CRect rtFrame = CRect(0,stFrame.nFrameIdx*nFrameHeight,nFrameWidth,stFrame.nFrameIdx*nFrameHeight+nFrameHeight); CRect rtIns; if(rtIns.IntersectRect(rtChamfer,rtFrame) == TRUE) { CInspectOption *pInsOpt = g_pStatus->GetInspectOption(); CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return; int nStageNo = g_pBase->m_nStageNo; if(nStageNo < 0 || nStageNo > 1) nStageNo = 0; rtIns.left = 0; rtIns.right = (int)(pInsOpt->nChippingInsArea/pCamera->m_dConvResolution[nStageNo]); vecIns.push_back(rtIns); } } void CInspectCamera::GetChamferThres(DimensionDir eDim,int &nChamferThres,int &nChipThres,int &nChipThres_White, int &nChipDiffThres, int &nBurrThres,int &nCrackThres,int &nBrokenThres) { pSTU_CUTAREA_PARM pCutArea = m_pRecipe->GetCutAreaPrm(eDim); pSTU_CHIP_PARM pChipPrm = m_pRecipe->GetInspectInfo()->GetChipParm(eDim); nChamferThres = pCutArea->m_nThresT; nChipThres = pChipPrm->m_nChippingThres_T; nChipThres_White = pChipPrm->m_nChippingThres_T_White; nBurrThres = pChipPrm->m_nChippingThres_T_L; nChipDiffThres = pChipPrm->m_nChipDiffThres; nCrackThres = pChipPrm->m_nCrackThres; nBrokenThres = pChipPrm->m_nBrokenThres; } DIT_RESULT CInspectCamera::FindThickness(int iThread,DimensionDir eDim,stFrameIndex stFrame,int iFindLeft) { DIT_RESULT ret= DIT_CONV_SUCCESS; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return ret; std::vector vectIns; GetChamferRect(stFrame,eDim,vectIns); if((int)vectIns.size() <= 0) return ret; int nStageNo = g_pBase->m_nStageNo; if(nStageNo < 0 || nStageNo > 1) nStageNo = 0; double dScanRes = pCamera->m_dScanResolution[nStageNo]; int nPrePos[2]; ChipErrCode eChipErr; int nChamferThres,nChamferChipThres,nChamferChipThreshold_White,nChipDiffThres,nBurrThres,nCrackThres,nBrokenThres; GetChamferThres(eDim,nChamferThres,nChamferChipThres,nChamferChipThreshold_White,nChipDiffThres,nBurrThres,nCrackThres,nBrokenThres); GetChamferControl(iThread)->SetConvResolution(pCamera->m_dConvResolution[nStageNo]); ZeroMemory(nPrePos,sizeof(int)*2); CRect rtIns; for(std::vector::iterator it=vectIns.begin();it!=vectIns.end();it++) { rtIns = *it; eChipErr = (ChipErrCode)ChamferInspectProcess(iThread,stFrame,eDim,iFindLeft,rtIns,nChamferThres,nChamferChipThres,nChamferChipThreshold_White,nChipDiffThres,nCrackThres,nBrokenThres,nBurrThres,nPrePos); if(eChipErr == ERR_CHIP_SUCCESS) { nPrePos[0] = GetChamferControl(iThread)->GetForeEdgeLine(0); nPrePos[1] = GetChamferControl(iThread)->GetForeEdgeLine(1); int nDefect = ChamferExtractResult(iThread,eDim,stFrame,rtIns); } else { } } if(m_pGlassData != NULL) { INS_EDGE_RESULT_INFO *pResInfo = m_pGlassData->GetEdgeResultInfo(eDim); if(pResInfo != NULL) { pResInfo->nGrindframe[0][stFrame.nFrameIdx] = GetChamferControl(iThread)->GetForeEdgeLine(0); pResInfo->nGrindframe[1][stFrame.nFrameIdx] = GetChamferControl(iThread)->GetForeEdgeLine(1); pResInfo->nGrindframe[2][stFrame.nFrameIdx] = GetChamferControl(iThread)->GetForeEdgeLine(1); } } return ret; } int CInspectCamera::ChamferExtractResult(int iThread,DimensionDir eDim,stFrameIndex stFrame,CRect &rect) { int nDefect = 0; STU_THICKPOS stRes; INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(eDim); if(pEdgeInfo == NULL) return FALSE; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return 0; int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); stRes.dThick = GetChamferControl(iThread)->GetAvgThick()*GetChamferControl(iThread)->GetConvRes(); stRes.nLeftLine = GetChamferControl(iThread)->GetForeEdgeLine(0); stRes.nRightLine = GetChamferControl(iThread)->GetForeEdgeLine(1); stRes.nTopLine = (stFrame.nFrameIdx*nFrameHeight) + nFrameHeight/2 -pEdgeInfo->nGlassStartLine; CINSPECT_JUDGEMENT_PARM *pJudge = m_pRecipe->GetJudgePrm(); stRes.nJudgeBase = pJudge->chamfer[eDim][pCamera->m_enCamDir].nRefernce; stRes.nJudgeDiff_Plus = pJudge->chamfer[eDim][pCamera->m_enCamDir].nTolPlus; stRes.nJudgeDiff_Minus = pJudge->chamfer[eDim][pCamera->m_enCamDir].nTolMinus; CSingleLock cLock(&m_csResultThick); cLock.Lock(); m_resultThickPos[stFrame.nScanIdx].push_back(stRes); cLock.Unlock(); CChipBlob *pChipRes = GetChamferControl(iThread)->GetDefectResult(); int nDefectCount = GetChamferControl(iThread)->GetDefectCount(); if(pChipRes != NULL && nDefectCount > 0) { if(m_pDefectControl != NULL) nDefect = m_pDefectControl->ExtractDefect_Chip(eDim,m_iCamera,stFrame.nScanIdx,pChipRes,nDefectCount,DefectLoc_Chip,rect.top); } return nDefect; } int CInspectCamera::ChamferInspectProcess(int iThread,stFrameIndex stFrame,DimensionDir eDim,int iFindLeft,CRect rtIns, int nThresChamfer,int nThresChip,int nThresChip_White,int nThresChipDiff,int nThresCrack,int nThresBroken,int nBurrThres,int *nPrePos) { CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return 0; int nBufWidth = align_4byte(rtIns.Width()); CRect rectChamfer = CRect(iFindLeft,rtIns.top,iFindLeft,rtIns.bottom); rectChamfer.InflateRect(nBufWidth/2,0); AssertFrameRectWidth(rectChamfer,stFrame.nScanIdx); LPBYTE lpHeader = m_pGrabber->GetFrameHeaderLine(stFrame.nScanIdx,rectChamfer.top); if(lpHeader == NULL) return ERR_CHIP_IMAGE_NULL_01; pSTU_CHIP_PARM pChipPrm = m_pRecipe->GetInspectInfo()->GetChipParm(eDim); if(pChipPrm == NULL) return ERR_CHIP_IMAGE_NULL_01; int nStageNo = g_pBase->m_nStageNo; if(nStageNo < 0 || nStageNo > 1) nStageNo = 0; double dConvRes = pCamera->m_dConvResolution[nStageNo]; if (m_pRecipe != nullptr) { CProfileInspect_Prm& profile = m_pRecipe->m_ProfilePrm; double dProfileRes = profile.m_dConvResolution[eDim][nStageNo]; if (dProfileRes > 0.0) { dConvRes = dProfileRes; } } int nChipInsArea = (int)((double)pChipPrm->m_nChippingInsArea / dConvRes); CRect rectChip = CRect(rectChamfer.CenterPoint().x, 0, rectChamfer.CenterPoint().x, rectChamfer.Height()); int nCrackInsArea = (int)((double)pChipPrm->m_nCrackINsArea / dConvRes); CRect rectCrack = CRect(rectChamfer.CenterPoint().x, 0, rectChamfer.CenterPoint().x, rectChamfer.Height()); int nBrokenInsArea = (int)((double)pChipPrm->m_nBrokenInsArea / dConvRes); CRect rectBroken = CRect(rectChamfer.CenterPoint().x, 0, rectChamfer.CenterPoint().x, rectChamfer.Height()); rectChip.InflateRect(align_4byte(nChipInsArea)/2,0); rectCrack.InflateRect(align_4byte(nCrackInsArea)/2,0); rectBroken.InflateRect(align_4byte(nBrokenInsArea)/2,0); AssertFrameRectWidth(rectChip,stFrame.nScanIdx); AssertFrameRectWidth(rectCrack,stFrame.nScanIdx); AssertFrameRectWidth(rectBroken,stFrame.nScanIdx); int nResult; InspectParam insParam; insParam.bUse_ChipIns = m_pRecipe->GetJudgePrm()->defect[(int)eDim][0].Chip_Side.bError;//nThresChip; insParam.bUse_CrackIns = m_pRecipe->GetJudgePrm()->defect[(int)eDim][0].Crack.bError;; insParam.bUse_BrokenIns = m_pRecipe->GetJudgePrm()->defect[(int)eDim][0].Broken.bError;; insParam.bUse_BurrIns = m_pRecipe->GetJudgePrm()->defect[(int)eDim][0].Burr_Side.bError;; insParam.nChipThres = nThresChip; insParam.nChipThres_White = nThresChip_White; insParam.nChipDiffThres = nThresChipDiff; insParam.nCrackThres = nThresCrack; insParam.nBrokenThres = nThresBroken; insParam.nBurrThres = nBurrThres; insParam.nChipMinSize = pChipPrm->m_nChippingInsMinSize; insParam.nCrackMinSize = pChipPrm->m_nCrackInsMinSize; insParam.nBrokenMinSize = pChipPrm->m_nBrokenInsMinSize; insParam.nBurrMinSize = pChipPrm->m_nBurrInsMinSize; insParam.nChipSideFilterSize = pChipPrm->m_nChippingInsSideFilterRange; insParam.nCrackSideFilterSize = pChipPrm->m_nCrackInsSideFilterRange; insParam.nBrokenSideFilterSize = pChipPrm->m_nBrokenInsSideFilterRnage; insParam.nBurrSideFilterSize = pChipPrm->m_nBurrInsSideFilterRange; DWORD dwTact = GetTickCount(); int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); if(nFrameWidth == 0 || nFrameHeight == 0) return ERR_CHIP_IMAGE_NULL_01; CInspectOption *pInsOpt = g_pStatus->GetInspectOption(); rectChamfer.OffsetRect(0,-rectChamfer.top); ChipErrCode errCode = GetChamferControl(iThread)->Inspection(&nResult,lpHeader,nFrameWidth,rectChamfer,nThresChamfer,TRUE,TRUE,rectChip,rectCrack,rectBroken,insParam,pInsOpt->bSaveCutArea,nPrePos,THRESMODE_FIXED,stFrame.nFrameIdx); // dwTact = GetTickCount() - dwTact; // // if(200 < dwTact) // g_pLog->DisplayMessage(_T("Processing to Slow.. frame %d, SpanTime %d ms"), stFrame.nFrameIdx, dwTact); return errCode; } void CInspectCamera::AssertFrameRectWidth(CRect &rect,int iScan) { int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,iScan,nFrameWidth,nFrameHeight); if(nFrameWidth == 0 || nFrameHeight == 0) return; if(rect.left < 0) rect.left = 0; if(rect.right >= nFrameWidth) rect.right = nFrameWidth; } int CInspectCamera::GetLeftMargin(int iCam,int iScan) { CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(iCam,iScan); if(pCamera == NULL) return 0; return pCamera->m_nMarginLeft; } void CInspectCamera::GetFrameSize(int iCam,int iScan,int &nFrameWidth,int &nFrameHeight) { nFrameWidth = nFrameHeight = 0; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(iCam,iScan); if(pCamera == NULL) return; nFrameWidth = pCamera->m_FrameSize.cx; nFrameHeight = pCamera->m_FrameSize.cy; } BOOL CInspectCamera::FindEndLine(int iThread,DimensionDir emDim,stFrameIndex stFrame) { INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pEdgeInfo == NULL) return FALSE; if(pEdgeInfo->nPreGlassEndFrame != stFrame.nFrameIdx) return FALSE; m_MsgJob.nState = 1; m_MsgJob.nSide = (int)emDim; ::SendMessage(m_hWndViewScan, WM_POCESS_STATUS,(WPARAM)&m_MsgJob, (int)emDim); #define BOUNDARY_GLASSSTARTWIDTH_ENDLINE 140 #define BOUNDARYHORIZ_LEVELDIST_ENDLINE 50 // 荐乞 版拌急阑 茫阑 锭狼 厚背芭府 int j,k; int nFrameWidth; int nFrameHeight; int nThreshold = m_pRecipe->GetEtcPrm(emDim)->m_nGlassFindthres_Top * BOUNDARY_GLASSSTARTWIDTH_ENDLINE; int nCountinueCount = 0; int nHStart, nGab; BOOL bContinue = FALSE; int nDivHor = 0; int iScan = stFrame.nScanIdx; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); CGLASS_INFO_PARM *pGlsInfo = m_pRecipe->GetGlassInfo(); int nSetHor = pGlsInfo->m_nGlassFindHorPos[emDim]; if(nSetHor < BOUNDARY_GLASSSTARTWIDTH_ENDLINE / 2) nSetHor = BOUNDARY_GLASSSTARTWIDTH_ENDLINE / 2; if(nSetHor > nFrameWidth-(BOUNDARY_GLASSSTARTWIDTH_ENDLINE / 2)) nSetHor = nFrameWidth-(BOUNDARY_GLASSSTARTWIDTH_ENDLINE / 2); g_pLog->DisplayMessage(_T( "%s End Edge Find position Frame[%d],Hor[%d]"),PANEL_SIDE[emDim],stFrame.nFrameIdx,nSetHor); nHStart = nSetHor - BOUNDARY_GLASSSTARTWIDTH_ENDLINE / 2; const int nCalculateFrame = 3; int nStartFrame = stFrame.nFrameIdx - 2; int nVTop = nStartFrame*nFrameHeight; LPBYTE lpHeader = m_pGrabber->GetFrameHeaderLine(stFrame.nScanIdx,nVTop); CSISBuffer pVertBuffer(lpHeader,nFrameWidth,nCalculateFrame*nFrameHeight); if(lpHeader == NULL || pVertBuffer.IsValidBuffer() == FALSE) return FALSE; // { // CString str; // str.Format(_T("D:\\Image\\EndLine.bmp")); // CBufferAttach attach(str); // attach.AttachToFile(pVertBuffer); // } int *pnSum = new int[nFrameHeight*nCalculateFrame + BOUNDARYHORIZ_LEVELDIST_ENDLINE]; ZeroMemory(pnSum, sizeof(int) * (nFrameHeight*nCalculateFrame + BOUNDARYHORIZ_LEVELDIST_ENDLINE)); for (j = pVertBuffer.GetHeight()-1 ; j >= 0; j--) { for (k = nHStart; k < nHStart+BOUNDARY_GLASSSTARTWIDTH_ENDLINE; k++) { pnSum[j] += *pVertBuffer.GetDataAddress(k,j); } if (j <= (pVertBuffer.GetHeight()-1-BOUNDARYHORIZ_LEVELDIST_ENDLINE)) { nGab = abs(pnSum[j + BOUNDARYHORIZ_LEVELDIST_ENDLINE] - pnSum[j]); if (nGab > nThreshold) nCountinueCount++; else nCountinueCount = 0; if (nCountinueCount >= 2) { // Glass狼 矫累瘤痢阑 茫酒辑 备炼眉俊 持绰促. pEdgeInfo->nGlassEndLine = nStartFrame*nFrameHeight + j + 2; pEdgeInfo->nGlassEndFrame = pEdgeInfo->nGlassEndLine/nFrameHeight; pEdgeInfo->nGlassEdgeXPos[1] = nSetHor; g_pLog->DisplayMessage(_T( "%s End Line Frame %d, Line %d"), PANEL_SIDE[emDim],pEdgeInfo->nGlassEndLine / nFrameHeight, pEdgeInfo->nGlassEndLine); // Log g_pLog->DisplayMessage(_T( "")); g_pLog->DisplayMessage(_T( "%s Glass EndLine :: %d,%d (nFrameNo=%d)") , PANEL_SIDE[emDim] , pEdgeInfo->nGlassEdgeXPos[1] , pEdgeInfo->nGlassEndLine , pEdgeInfo->nGlassEndLine / nFrameHeight); delete [] pnSum; SaveGlassLineImage(stFrame.nScanIdx,pEdgeInfo->nGlassEndLine,pEdgeInfo->nGlassEdgeXPos[1],emDim,_T("EndLine")); return TRUE; } } } pEdgeInfo->nGlassEndLine = pEdgeInfo->nPreGlassEndLine; pEdgeInfo->nGlassEndFrame = pEdgeInfo->nPreGlassEndFrame; delete [] pnSum; g_pLog->DisplayMessage(_T("%s EndLine Frame %d Fail"),PANEL_SIDE[emDim],stFrame.nFrameIdx); return FALSE; } void CInspectCamera::SaveGlassLineImage(int iScan,int nLine,int nXPos,DimensionDir emDim,CString strName) { int nImgWidth = 100; int nImgHeight = 200; CvRect saveRect = cvRect(nXPos-nImgWidth,0,nImgWidth*2,nImgHeight*2); int iStartV = nLine-nImgHeight; if(iStartV < 0) iStartV = 0; LPBYTE pImg = m_pGrabber->GetFrameHeaderLine(iScan,iStartV); if(pImg == NULL) return; IplImage *IpImg = cvCreateImage(cvSize(saveRect.width,saveRect.height),8,1); cvZero(IpImg); int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,iScan,nFrameWidth,nFrameHeight); for(int i=0; i< saveRect.height; i++) { memcpy(&IpImg->imageData[IpImg->widthStep*i], pImg+saveRect.x, sizeof(BYTE)*IpImg->width); pImg += nFrameWidth; } CString strFile; strFile.Format(_T("%sIMG_MARK\\%s\\%s\\%s\\%s_%s.jpg"),PATH_INSPECTION_DATA,g_pBase->m_strLoadingDay,g_pBase->m_strHPanelID,g_pBase->m_strLoadingTime,PANEL_SIDE[emDim],strName); g_pStatus->CheckDirectory(strFile); cv::Point startPoint(0,saveRect.height/2); cv::Point endPoint(saveRect.width,saveRect.height/2); cvLine(IpImg, startPoint, endPoint, cvScalar(255, 0, 0), 1); USES_CONVERSION; char str_filename[1024]; sprintf_s(str_filename, "%s", W2A(strFile)); #if MARKIMG_NOTSAVE cvSaveImage(str_filename, IpImg); #endif // 0 cvReleaseImage(&IpImg); } int CInspectCamera::FindLeftLine(int iThread,DimensionDir emDim,stFrameIndex stFrame) { int nFindLine=-1; CETC_PARM *pEtc = m_pRecipe->GetEtcPrm(emDim); if(pEtc == NULL) { g_pLog->DisplayMessage(_T("Cam[%d],Scan %d,Frame[%d] Find Left Fail : Parameter is Null"),m_iCamera,stFrame.nScanIdx,stFrame.nFrameIdx); return GetLeftMargin(m_iCamera,stFrame.nScanIdx); } int nThres = pEtc->m_nGlassFindthres_Left; #define DEFECT_EDGE_AUTO_RATIO 0.3 #define DEFECT_EDGE_AUTO_PITCH 20 #define DEFECT_EDGE_CONTINUE 3 CSISEdgeFind EdgeFind; CRect rtROI; const int nSearchWidth = 500; const int nSearchHeight = 100; int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); CSISBuffer frameBuffer(m_pGrabber->GetFrameHeader(stFrame.nScanIdx,stFrame.nFrameIdx),nFrameWidth,nFrameHeight); if(frameBuffer.IsValidBuffer() == FALSE) return nFindLine; rtROI = CRect(GetLeftMargin(m_iCamera,stFrame.nScanIdx),0,frameBuffer.GetWidth()-1,frameBuffer.GetHeight()); INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pEdgeInfo == NULL) return nFindLine; if(pEdgeInfo->nGlassStartLine != 0 && abs(pEdgeInfo->nGlassStartFrame - stFrame.nFrameIdx) <= 3) { rtROI.bottom = frameBuffer.GetHeight()-1; rtROI.top = rtROI.bottom - nSearchHeight; } else if(pEdgeInfo->nGlassEndLine != 0 && pEdgeInfo->nGlassEndLine/frameBuffer.GetHeight() == stFrame.nFrameIdx) { rtROI.top = 0; rtROI.bottom = rtROI.top + nSearchHeight; } else { rtROI.top = frameBuffer.GetHeight()/2 - nSearchHeight/2; rtROI.bottom = rtROI.top + nSearchHeight; } if(rtROI.left < 0) rtROI.left = 0; if(rtROI.right >= frameBuffer.GetWidth()) rtROI.right = frameBuffer.GetWidth()-1; if(rtROI.top < 0) rtROI.top = 0; if(rtROI.bottom >= frameBuffer.GetHeight()) rtROI.bottom = frameBuffer.GetHeight()-1; EdgeFind.FindEdge_ToRightROI(&frameBuffer,nFindLine,DEFECT_EDGE_AUTO_PITCH,nThres,DEFECT_EDGE_AUTO_RATIO,0,rtROI); if(nFindLine > 0 && nFindLine < frameBuffer.GetWidth()) { m_iSideLine[stFrame.nScanIdx] = nFindLine; pEdgeInfo->nGrindframe[0][stFrame.nFrameIdx] = nFindLine; //Log_GetDebug()->TraceInfo("Trace Left Edge: %d, %d, (%d, %d, %d, %d), %d, %d", // (int)emDim, stFrame.nScanIdx, // rtROI.left, rtROI.top, rtROI.Width(), rtROI.Height(), // nFindLine, __LINE__); } // { // CString str; // str.Format(_T("%sIMG_MARK\\FrameEdge_%s_%03d_%d_%04d_roi_%d_%d_%d_%d.bmp"),PATH_INSPECTION_DATA,PANEL_SIDE[emDim],stFrame.nFrameIdx,iThread,nFindLine,rtROI.left,rtROI.top,rtROI.right,rtROI.bottom); // CBufferAttach attach(str); // attach.AttachToFile(frameBuffer); // } return nFindLine; } BOOL CInspectCamera::OnThreadEnd(int iThread, CInspectThread *pInspectThread) { return TRUE; } BOOL CInspectCamera::OnThreadEndAll() { return TRUE; } void CInspectCamera::ReleaseSplineBuffer() { int i; for(i=0;iDisplayMessage(_T("SetParameter Cam[%d] start"),m_iCamera); m_bExitThread = TRUE; ZeroMemory(m_iThreadEnd,sizeof(m_iThreadEnd)); ZeroMemory(m_bFindGlassStart,sizeof(m_bFindGlassStart)); ZeroMemory(m_iSideLine,sizeof(m_iSideLine)); ZeroMemory(m_dMark2MarkX,sizeof(m_dMark2MarkX)); ZeroMemory(m_dMark2MarkY,sizeof(m_dMark2MarkY)); ZeroMemory(m_bSplineInspect,sizeof(m_bSplineInspect)); ZeroMemory(m_bProfileFind,sizeof(m_bProfileFind)); ZeroMemory(m_nProfileFindFrameIdx,sizeof(m_nProfileFindFrameIdx)); for(int i=0;iClearGrabIdx(); //m_pGrabber->ClearBuffer(); } if(m_pDefectControl != NULL) { m_pDefectControl->SetHWRecipe(pHW); m_pDefectControl->ResetDefectControl(); } g_pLog->DisplayMessage(_T("SetParameter Cam[%d] end"),m_iCamera); } void CInspectCamera::DeleteCADAlign(std::multimap *pSetProfileAlignMap) { CSingleLock csLock(&m_csProfile); csLock.Lock(); std::multimap::iterator it; CPoint *pPoint; for(it=pSetProfileAlignMap->begin();it!=pSetProfileAlignMap->end();it++) { pPoint = it->second; if(pPoint == NULL) continue; delete pPoint; pPoint = NULL; } pSetProfileAlignMap->clear(); csLock.Unlock(); } void CInspectCamera::SetCADData(std::multimap *pProfileMap,CPoint *ptOrigin) { if(m_pRecipe == NULL || pProfileMap == NULL) return; CProfileCADProc *pProfile = NULL; int iScan,iSide; std::multimap *pSetProfileMap; std::multimap *pSetProfileAlignMap; for(iScan=0;iScanSetProfileData(pSetProfileMap,pSetProfileAlignMap); pProfile->SetProfileOrigin(ptOrigin[iSide]); } } } void CInspectCamera::SetGlassData(CGlass_Data *pGlassData) { m_pGlassData=pGlassData; if(m_pDefectControl != NULL) m_pDefectControl->SetGlassData(pGlassData); if(m_pGrabber != NULL) { for(int i=0;iGetEdgeResultInfo((DimensionDir)i); if(pEdgeInfo == NULL) continue; pEdgeInfo->nPreGlassEndFrame = m_pGrabber->GetFrameBuffer()->GetFrameCount()-1; } } } void CInspectCamera::SetGrabber(CGrabberControl *pGrabber) { m_pGrabber = pGrabber; if(m_pDefectControl != NULL) m_pDefectControl->SetGrabber(pGrabber); } DimensionDir CInspectCamera::GetDimension(int iScan) { CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,iScan); if(pCamera == NULL) return DIMENSION_NONE; return pCamera->m_eDimension; } void CInspectCamera::SetGrabEnd(int iScan) { CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,iScan); if(pCamera == NULL) return; DimensionDir emDim = pCamera->m_eDimension; INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pEdgeInfo == NULL) return; int nStageNo = g_pBase->m_nStageNo; if(nStageNo < 0 || nStageNo > 1) nStageNo = 0; int nGlassSize; if(pCamera->m_eScanDir == SCANGLASS_X) nGlassSize = (int)((double)m_pRecipe->GetGlassInfo()->m_nGlassSizeX/pCamera->m_dScanResolution[nStageNo]); else nGlassSize = (int)((double)m_pRecipe->GetGlassInfo()->m_nGlassSizeY/pCamera->m_dScanResolution[nStageNo]); pEdgeInfo->nPreGlassEndLine = pEdgeInfo->nGlassStartLine + nGlassSize; pEdgeInfo->nPreGlassEndFrame = pEdgeInfo->nPreGlassEndLine/pCamera->m_FrameSize.cy; g_pStatus->SetGrabFrametoScan(iScan,pEdgeInfo->nPreGlassEndFrame+5); g_pLog->DisplayMessage(_T("%s Scan %d : Set Grab EndFrame - GlassEnd %d, Frame %d, GrabBuf %d"),PANEL_SIDE[emDim],iScan,pEdgeInfo->nPreGlassEndLine,pEdgeInfo->nPreGlassEndFrame,pEdgeInfo->nPreGlassEndFrame+5); } #define CHAMFER_MIN_INSPECT_HEIGHT 1000 #define CHAMFER_MIN_INSPECT_SEARCHWIDTH 500 #define CHIPPING_MIN_INSPECT_AREA 150 void CInspectCamera::PresetChamferPrm(int iScan,DimensionDir eDim) { pSTU_CUTAREA_PARM pCutArea = m_pRecipe->GetInspectInfo()->GetCutAreaParm(eDim); pSTU_CHIP_PARM pChipPrm = m_pRecipe->GetInspectInfo()->GetChipParm(eDim); CInspectOption *pInsOpt = g_pStatus->GetInspectOption(); if(pCutArea->m_bCutArea == TRUE) { pCutArea->m_nCalArea = pInsOpt->nChamferInsHeight; pCutArea->m_nSearchWidth = pInsOpt->nChamferSearchWidth; if(pCutArea->m_nCalArea < CHAMFER_MIN_INSPECT_HEIGHT) pCutArea->m_nCalArea = CHAMFER_MIN_INSPECT_HEIGHT; if(pCutArea->m_nSearchWidth < CHAMFER_MIN_INSPECT_SEARCHWIDTH) pCutArea->m_nSearchWidth = CHAMFER_MIN_INSPECT_SEARCHWIDTH; } pChipPrm->m_bChipping = pCutArea->m_bCutArea; if(pChipPrm->m_bChipping == TRUE) { pChipPrm->m_nChippingInsArea = pInsOpt->nChippingInsArea; if(pChipPrm->m_nChippingInsArea < CHIPPING_MIN_INSPECT_AREA) pChipPrm->m_nChippingInsArea = CHIPPING_MIN_INSPECT_AREA; pChipPrm->m_nCrackINsArea = pInsOpt->nCrackINsArea; if(pChipPrm->m_nCrackINsArea < CHIPPING_MIN_INSPECT_AREA) pChipPrm->m_nCrackINsArea = CHIPPING_MIN_INSPECT_AREA; pChipPrm->m_nBrokenInsArea = pInsOpt->nBrokenInsArea; if(pChipPrm->m_nBrokenInsArea < CHIPPING_MIN_INSPECT_AREA) pChipPrm->m_nBrokenInsArea = CHIPPING_MIN_INSPECT_AREA; // 20. 11. 14 pChipPrm->m_nChippingInsMinSize = pInsOpt->nChippingInsMinSize; pChipPrm->m_nCrackInsMinSize = pInsOpt->nCrackInsMinSize; pChipPrm->m_nBrokenInsMinSize = pInsOpt->nBrokenInsMinSize; pChipPrm->m_nBurrInsMinSize = pInsOpt->nBurrInsMinSize; pChipPrm->m_nChippingInsSideFilterRange = pInsOpt->nChippingInsSideFilterRange; pChipPrm->m_nCrackInsSideFilterRange = pInsOpt->nCrackInsSideFilterRange; pChipPrm->m_nBrokenInsSideFilterRnage = pInsOpt->nBrokenInsSideFilterRnage; pChipPrm->m_nBurrInsSideFilterRange = pInsOpt->nBurrInsSideFilterRange; } CETC_PARM *pEtcPrm = m_pRecipe->GetEtcPrm(eDim); if(pEtcPrm != NULL) { CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,iScan); if(pCamera == NULL) return; pEtcPrm->m_nGlassFindWidth_Left = pCamera->m_FrameSize.cx-1; pEtcPrm->m_nGlassLeftLineMargin[0] = pEtcPrm->m_nGlassLeftLineMargin[1] = 0; pEtcPrm->m_nGlassFindthres_Left = pEtcPrm->m_nGlassFindthres_Top; } } void CInspectCamera::MakeHoleRegion(int iScan) { DimensionDir emDim = GetDimension(iScan); INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pEdgeInfo == NULL) return; CSingleLock cLock(&m_csHoleReion); cLock.Lock(); m_vecHoleReg[iScan].clear(); for(int i=0; iGetHolePrm()->m_nTotalHoleCount; i++) { if(m_pRecipe->GetHolePrm()->m_vecHoleInsParam[i].m_nSideIdx == iScan) { m_vecHoleReg[iScan].push_back(m_pRecipe->GetHolePrm()->m_vecHoleInsParam[i]); } } cLock.Unlock(); } void CInspectCamera::GetHoleRegionFrame(int iThread,DimensionDir emDim,stFrameIndex stFrame, std::vector &vecArray) { INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pEdgeInfo == NULL) return; vector::iterator it; if(pEdgeInfo->bMark[0] == FALSE) return; int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,stFrame.nScanIdx,nFrameWidth,nFrameHeight); vecArray.clear(); for(it=m_vecHoleReg[stFrame.nScanIdx].begin();it!=m_vecHoleReg[stFrame.nScanIdx].end();it++) { if(stFrame.nFrameIdx == (it->m_rtArea.bottom + (int)pEdgeInfo->ptMark[0].y)/nFrameHeight) { vecArray.push_back(*it); } } } void CInspectCamera::MakeChamferRegion(int iScan) { DimensionDir emDim = GetDimension(iScan); CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,iScan); if(pCamera == NULL) return; INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pEdgeInfo == NULL) return; int nStageNo = g_pBase->m_nStageNo; if(nStageNo < 0 || nStageNo > 1) nStageNo = 0; double dConv = pCamera->m_dConvResolution[nStageNo]; double dScan = pCamera->m_dScanResolution[nStageNo]; if(dConv <= 0 || dScan <= 0) return; int nGlassSize; if(pCamera->m_eScanDir == SCANGLASS_X) nGlassSize = (int)(m_pRecipe->GetGlassInfo()->m_nGlassSizeX/dScan); else nGlassSize = (int)(m_pRecipe->GetGlassInfo()->m_nGlassSizeY/dScan); int nRSize = 0,nRSize2 = 0; pSTU_SPLINE_INS_PARM pSpline = m_pRecipe->GetInspectInfo()->GetSplineIns(emDim); if(pSpline != NULL ) { nRSize = pSpline->m_nStartCornerSize_Y; nRSize2 = pSpline->m_nEndCornerSize_Y; } pEdgeInfo->rtChamfer = CRect(0,nRSize,pCamera->m_FrameSize.cx,nGlassSize-nRSize2); pEdgeInfo->rtChamfer.OffsetRect(0,pEdgeInfo->nGlassStartLine); g_pLog->DisplayMessage(_T("%s Scan %d : Chamfer Region %d,%d"),PANEL_SIDE[emDim],iScan,pEdgeInfo->rtChamfer.top,pEdgeInfo->rtChamfer.bottom); } void CInspectCamera::MakeAlignRegion(int iScan) { CINSPECT_JUDGEMENT_PARM *pJudgePrm = m_pRecipe->GetJudgePrm(); CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,iScan); if(pCamera == NULL) return; DimensionDir emDim = GetDimension(iScan); INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pEdgeInfo == NULL) return; int nStageNo = g_pBase->m_nStageNo; if(nStageNo < 0 || nStageNo > 1) nStageNo = 0; double dConv = pCamera->m_dConvResolution[nStageNo]; double dScan = pCamera->m_dScanResolution[nStageNo]; if(dConv <= 0 || dScan <= 0) return; int nGlassSize; if(pCamera->m_eScanDir == SCANGLASS_X) nGlassSize = m_pRecipe->GetGlassInfo()->m_nGlassSizeX; else nGlassSize = m_pRecipe->GetGlassInfo()->m_nGlassSizeY; int iSide = (int)emDim; m_dMark2MarkX[iScan][0] = (pJudgePrm->ptAlign[0][iSide].x/dConv); m_dMark2MarkX[iScan][1] = (pJudgePrm->ptAlign[1][iSide].x/dConv); m_dMark2MarkY[iScan][0] = (pJudgePrm->ptAlign[0][iSide].y/dScan); m_dMark2MarkY[iScan][1] = ((nGlassSize-pJudgePrm->ptAlign[1][iSide].y)/dScan); m_dMark2MarkX[iScan][2] = (pJudgePrm->ptAlign[2][iSide%MAX_PANEL_SIDE].x/dConv); m_dMark2MarkY[iScan][2] = ((pJudgePrm->ptAlign[2][iSide%MAX_PANEL_SIDE].y)/dScan); CInspectOption *pOpt = g_pStatus->GetInspectOption(); int nAlignRange = pOpt->nAlignMarkRange; int nAlignRangeY = pOpt->nAlignMarkRangeY; if(nAlignRange < 100) nAlignRange = 100; if(nAlignRangeY < 100) nAlignRangeY = 100; pEdgeInfo->rtMark[0] = CRect((int)m_dMark2MarkX[iScan][0],(int)m_dMark2MarkY[iScan][0],(int)m_dMark2MarkX[iScan][0],(int)m_dMark2MarkY[iScan][0]); pEdgeInfo->rtMark[0].InflateRect(nAlignRange,nAlignRangeY); pEdgeInfo->rtMark[1] = CRect((int)m_dMark2MarkX[iScan][1],(int)m_dMark2MarkY[iScan][1],(int)m_dMark2MarkX[iScan][1],(int)m_dMark2MarkY[iScan][1]); pEdgeInfo->rtMark[1].InflateRect(nAlignRange,nAlignRangeY); pEdgeInfo->rtMark[2] = CRect((int)m_dMark2MarkX[iScan][2],(int)m_dMark2MarkY[iScan][2],(int)m_dMark2MarkX[iScan][2],(int)m_dMark2MarkY[iScan][2]); pEdgeInfo->rtMark[2].InflateRect(nAlignRange,nAlignRangeY); pEdgeInfo->rtMark[0].OffsetRect(0,pEdgeInfo->nGlassStartLine); pEdgeInfo->rtMark[1].OffsetRect(0,pEdgeInfo->nGlassStartLine); pEdgeInfo->rtMark[2].OffsetRect(0,pEdgeInfo->nGlassStartLine); } BOOL CInspectCamera::FindAlignProcess(int iThread,DimensionDir eDim,int iScan,int iFrame) { int iPos; for(iPos=0;iPos<3;iPos++) { FindAlignMark(iThread,eDim,iScan,iFrame,iPos); } return TRUE; } BOOL CInspectCamera::FindAlignMark(int iThread,DimensionDir eDim, int iScan,int iFrame,int iPos) { BOOL bRet = FALSE; INS_EDGE_RESULT_INFO *pResInfo = m_pGlassData->GetEdgeResultInfo(eDim); CALIGNMARK_PARM *pAlignPrm = m_pRecipe->GetAlignMarkPrm(eDim); if(pResInfo != NULL && pResInfo->bMark[iPos] == TRUE) return TRUE; CRect rectAlign = pResInfo->rtMark[iPos]; if(rectAlign.top <= 0) return FALSE; int nFrameWidth,nFrameHeight; GetFrameSize(m_iCamera,iScan,nFrameWidth,nFrameHeight); switch(iPos) { case 0: if(iFrame != rectAlign.bottom/nFrameHeight+2) return FALSE; break; default: if(iFrame != rectAlign.bottom/nFrameHeight) return FALSE; break; } LPBYTE lpBuffer = m_pGrabber->GetFrameHeaderLine(iScan,rectAlign.top); if(lpBuffer == NULL) return FALSE; CSISBuffer frameBuffer(lpBuffer, nFrameWidth, rectAlign.Height()); if(!frameBuffer.IsValidBuffer()) { return FALSE; } if(iPos == 1) { int aa = 100; } stFrameIndex stFrame(iScan,iFrame); m_iSideLine[iScan] = FindLeftLine(iThread,eDim,stFrame); if(m_iSideLine[iScan] <= 0 || m_iSideLine[iScan] >= nFrameWidth) { if(iPos == 1) { if(pResInfo != NULL) { for(int i=iFrame-1;i>=pResInfo->nGlassStartLine/nFrameHeight-1;i--) { if(pResInfo->nGrindframe[0][i] > 0 && pResInfo->nGrindframe[0][i] < nFrameWidth) { m_iSideLine[iScan] = pResInfo->nGrindframe[0][i]; break; } } } } if(m_iSideLine[iScan] <= 0) { g_pLog->DisplayMessage(_T("%s Align Mark Find[%d] Edge Find Fail : Frame %d, %d,%d,%d,%d"),PANEL_SIDE[eDim],iPos, iFrame,rectAlign.left,rectAlign.top,rectAlign.right,rectAlign.bottom); m_iSideLine[iScan] = GetLeftMargin(m_iCamera,iScan); } } CString strPos = _T("TOP"); CString strRes = _T("Fail"); switch(iPos) { case 1: strPos = _T("BOT"); break; case 2: strPos = _T("SUB"); break; } rectAlign.OffsetRect(m_iSideLine[iScan],0); if(pAlignPrm->m_bIsMark[iPos] == FALSE) { pResInfo->bMark[iPos] = TRUE; pResInfo->ptMark[iPos].x = (float)rectAlign.CenterPoint().x; pResInfo->ptMark[iPos].y = (float)rectAlign.CenterPoint().y; pResInfo->nMarkToEdge_X[iPos] = (int)(pResInfo->ptMark[iPos].x - m_iSideLine[iScan]); pResInfo->nMarkToEdge_Y[iPos] = (int)(pResInfo->ptMark[iPos].y - pResInfo->nGlassStartLine); g_pLog->DisplayMessage(_T("%s Virtual Align Mark Pos[%d] x : %.2f, y : %.2f"),PANEL_SIDE[eDim],iPos,pResInfo->ptMark[iPos].x,pResInfo->ptMark[iPos].y); return TRUE; } g_pLog->DisplayMessage(_T("%s Align Mark Pos[%d] %d,%d,%d,%d"),PANEL_SIDE[eDim],iPos,rectAlign.left,rectAlign.top,rectAlign.right,rectAlign.bottom); double dRate = 0.5; TEMP_RESULT matchResut; CRect rectIns = rectAlign; rectIns.OffsetRect(0,-rectIns.top); matchResut.fRat = 0.; matchResut.pt.x = matchResut.pt.y = 0; for(int iMarkPos=0;iMarkPosIpMarkImg[iPos][iMarkPos] == NULL) continue; CRect rtFind = rectIns; if(rtFind.Width() <= pAlignPrm->IpMarkImg[iPos][iMarkPos]->width) rtFind.right = rtFind.left + pAlignPrm->IpMarkImg[iPos][iMarkPos]->width + 10; if(rtFind.Height() <= pAlignPrm->IpMarkImg[iPos][iMarkPos]->height) rtFind.bottom = rtFind.top + pAlignPrm->IpMarkImg[iPos][iMarkPos]->height + 10; CEdgeProc insProc; insProc.CvFindMark(frameBuffer,pAlignPrm->IpMarkImg[iPos][iMarkPos],rtFind,matchResut,dRate); if(matchResut.fRat*100. >= pAlignPrm->m_dMarkMatchingRate[iPos][iMarkPos]) { if(pResInfo != NULL) { pResInfo->bMark[iPos] = TRUE; pResInfo->ptMark[iPos].x = matchResut.pt.x; pResInfo->ptMark[iPos].y = matchResut.pt.y + rectAlign.top; pResInfo->nMarkToEdge_X[iPos] = (int)(pResInfo->ptMark[iPos].x - m_iSideLine[iScan]); pResInfo->nMarkToEdge_Y[iPos] = (int)(pResInfo->ptMark[iPos].y - pResInfo->nGlassStartLine); //pResInfo->nGrindframe[0][iFrame] = pResInfo->nGrindframe[1][iFrame] = m_iSideLine[iScan]; } g_pLog->DisplayMessage(_T("%s Align Mark Find[%d][%d] Success x %.2f, y %.2f"),PANEL_SIDE[eDim],iPos,iMarkPos,matchResut.pt.x, matchResut.pt.y + rectAlign.top); strRes = _T("Success"); break; } } if(pResInfo != NULL && pResInfo->bMark[iPos] == FALSE) { if(iPos != 2) // sub mark绰 力寇 { pResInfo->bMark[iPos] = TRUE; pResInfo->ptMark[iPos].x = (float)rectAlign.CenterPoint().x; pResInfo->ptMark[iPos].y = (float)rectAlign.CenterPoint().y; pResInfo->nMarkToEdge_X[iPos] = (int)(pResInfo->ptMark[iPos].x - m_iSideLine[iScan]); pResInfo->nMarkToEdge_Y[iPos] = (int)(pResInfo->ptMark[iPos].y - pResInfo->nGlassStartLine); g_pLog->DisplayMessage(_T("%s Virtual Align Mark Pos[%d] x : %.2f, y : %.2f"),PANEL_SIDE[eDim],iPos,pResInfo->ptMark[iPos].x,pResInfo->ptMark[iPos].y); } //pResInfo->nGrindframe[0][iFrame] = pResInfo->nGrindframe[1][iFrame] = m_iSideLine[iScan]; g_pLog->DisplayMessage(_T("%s Align Mark Find[%d] Fail x %.2f, y %.2f"),PANEL_SIDE[eDim],iPos,m_iSideLine[iScan] + m_dMark2MarkX[iScan][iPos], pResInfo->nGlassStartLine + m_dMark2MarkY[iScan][iPos]); } /* CString strFile; CvRect saveRect = cvRect((int)matchResut.pt.x-rectAlign.Width()/2,(int)(matchResut.pt.y + rectAlign.top)-rectAlign.Height()/2,rectAlign.Width(),rectAlign.Height()); strFile.Format(_T("%sIMG_MARK\\%s\\%s\\%s\\Align_%s_%s_%s.jpg"),PATH_INSPECTION_DATA,g_pBase->m_strLoadingDay,g_pBase->m_strHPanelID,g_pBase->m_strLoadingTime,strPos,PANEL_SIDE[eDim],strRes); SaveCvAlignImage(saveRect,strFile,iScan,nFrameWidth,nFrameHeight); */ if(iPos == 0) // 霉锅掳 Align 茫疽阑措父 True bRet = TRUE; return bRet; } BOOL CInspectCamera::SaveCvAlignImage(CvRect saveRoi,CString strFile,int iScan,int nFrameWidth,int nFrameHeight) { LPBYTE pImg = m_pGrabber->GetFrameHeaderLine(iScan,saveRoi.y); if(pImg == NULL) return FALSE; IplImage* IpImg = cvCreateImage(cvSize(saveRoi.width, saveRoi.height), 8, 1); cvZero(IpImg); for(int i=0; i< saveRoi.height; i++) { memcpy(&IpImg->imageData[IpImg->widthStep*i], pImg+saveRoi.x, sizeof(BYTE)*IpImg->width); pImg += nFrameWidth; } g_pStatus->CheckDirectory(strFile); int nLineSize = 10; CPoint posC = CPoint(saveRoi.width/2,saveRoi.height/2); cv::Point startPoint(posC.x-nLineSize,posC.y); cv::Point endPoint(posC.x+nLineSize,posC.y); cvLine(IpImg, startPoint, endPoint, cvScalar(192, 192, 192), 1); cv::Point startPoint2(posC.x,posC.y-nLineSize); cv::Point endPoint2(posC.x,posC.y+nLineSize); cvLine(IpImg, startPoint2, endPoint2, cvScalar(192, 192, 192), 1); USES_CONVERSION; char str_filename[1024]; sprintf_s(str_filename, "%s", W2A(strFile)); cvSaveImage(str_filename,IpImg); cvReleaseImage(&IpImg); return TRUE; } BOOL CInspectCamera::FindGlassStartLine(DimensionDir emDim,stFrameIndex stFrame) { INS_EDGE_RESULT_INFO *pEdgeInfo = m_pGlassData->GetEdgeResultInfo(emDim); if(pEdgeInfo == NULL) return FALSE; CCameraSettings *pCamera = m_pHardparm->GetCameraSettings(m_iCamera,stFrame.nScanIdx); if(pCamera == NULL) return FALSE; CGLASS_INFO_PARM *pGlsInfo = m_pRecipe->GetGlassInfo(); int nFrame = m_pGrabber->GetFrameBuffer()->GetFrameCount()/2; int nFrameWidth = pCamera->m_FrameSize.cx; int nFrameHeight = pCamera->m_FrameSize.cy; int nSkipFrame = pGlsInfo->m_nFindGlassStartOffset[emDim] / nFrameHeight; int nSetHor = pGlsInfo->m_nGlassFindHorPos[emDim]; if(nSetHor < BOUNDARY_GLASSSTARTWIDTH / 2) nSetHor = BOUNDARY_GLASSSTARTWIDTH / 2; if(nSetHor > nFrameWidth-(BOUNDARY_GLASSSTARTWIDTH / 2)) nSetHor = nFrameWidth-(BOUNDARY_GLASSSTARTWIDTH / 2); pEdgeInfo->nGlassEdgeXPos[0] = nSetHor; int j, k, nGab; int nThreshold = m_pRecipe->GetEtcPrm(emDim)->m_nGlassFindthres_Top * BOUNDARY_GLASSSTARTWIDTH; int *pnSum = new int[nFrameHeight + BOUNDARYHORIZ_LEVELDIST]; LPBYTE lpHeader; int nCountinueCount = 0; int nRet,nHStart; DWORD dwT1 = GetTickCount(); g_pLog->DisplayMessage(_T("%s Find Glass Start Start : Scan %d "),PANEL_SIDE[emDim],stFrame.nScanIdx); for(int iFrame=0;iFrameIsAcqFrame(iFrame); if (nRet < 2) // 酒流 救嘛躯栏骨肺 措扁. { if (GetTickCount() - dwT1 > TIME_WAIT_GLASS_START) { g_pLog->DisplayMessage(_T("%s Find Glass Start : no image(Grab Time Out),%d sec"),PANEL_SIDE[emDim],TIME_WAIT_GLASS_START/1000); delete [] pnSum; return FALSE; } Sleep(1); } else break; } if (nSkipFrame > iFrame) continue; lpHeader = m_pGrabber->GetFrameHeader(stFrame.nScanIdx,iFrame); if(lpHeader == NULL) { g_pLog->DisplayMessage(_T("%s Find Glass Start : no image(Image is Null)"),PANEL_SIDE[emDim]); delete [] pnSum; return FALSE; } ZeroMemory(pnSum, sizeof(int) * (nFrameHeight + BOUNDARYHORIZ_LEVELDIST)); int nLine = 0; nHStart = nSetHor - BOUNDARY_GLASSSTARTWIDTH / 2; BOOL bChangeBuffer = FALSE; for (j = 0; j < nFrameHeight + BOUNDARYHORIZ_LEVELDIST; j++) //for (j = 0; j < nFrameHeight; j++) { for (k = nHStart; k < nHStart + BOUNDARY_GLASSSTARTWIDTH; k++) { // Grab牢 版快 滚欺狼 版拌俊辑 1024扼牢阑 逞绢哎 版快 促澜 滚欺甫 啊廉客具 茄促. if (j == nFrameHeight && nLine != 0 && bChangeBuffer == FALSE) { DWORD dwTick = GetTickCount(); while (TRUE) { if(GetTickCount()-dwTick > TIME_WAIT_GLASS_START/2) { g_pLog->DisplayMessage(_T("%s No Image"),PANEL_SIDE[emDim]); delete [] pnSum; return FALSE; } if(m_pGrabber->IsAcqFrame(iFrame + 1) > 1) { lpHeader = m_pGrabber->GetFrameHeader(stFrame.nScanIdx,iFrame + 1); if(lpHeader != NULL) { bChangeBuffer = TRUE; //g_pLog->DisplayMessage(_T("%s Change Buffer %d, %d, %d"),PANEL_SIDE[pRecipe->GetDimension()], j, i, g_pBase->m_pMemFrameNo[GetCameraID()]); break; } } } nLine = 0; } pnSum[j] += *(lpHeader + nLine * nFrameWidth + k); } nLine++; if (j >= BOUNDARYHORIZ_LEVELDIST) { nGab = abs(pnSum[j - BOUNDARYHORIZ_LEVELDIST] - pnSum[j]); if (nGab > nThreshold) nCountinueCount++; else nCountinueCount = 0; if (nCountinueCount >= 2) { g_pLog->DisplayMessage(_T("%s Start Line Frame %d, Line %d, Gab %d, Thres %d"), PANEL_SIDE[emDim],iFrame, j, nGab, nThreshold); // Glass狼 矫累瘤痢阑 茫酒辑 备炼眉俊 持绰促. pEdgeInfo->nGlassStartLine = iFrame * nFrameHeight + j - 2; pEdgeInfo->nGlassStartFrame = pEdgeInfo->nGlassStartLine/nFrameHeight; // Log g_pLog->DisplayMessage(_T("")); g_pLog->DisplayMessage(_T("%s Glass StartLine :: Pixel %ld nFrameNo=%d") , PANEL_SIDE[emDim] , pEdgeInfo->nGlassStartLine , pEdgeInfo->nGlassStartLine / nFrameHeight); delete [] pnSum; SaveGlassLineImage(stFrame.nScanIdx,pEdgeInfo->nGlassStartLine,pEdgeInfo->nGlassEdgeXPos[0],emDim,_T("StartLine")); return TRUE; } } } } delete [] pnSum; return FALSE; } int CInspectCamera::GetDefectCount() { if(m_pDefectControl == NULL) return 0; return m_pDefectControl->GetDefectCount(); } CDefect *CInspectCamera::GetDefect(int iDefect) { if(m_pDefectControl == NULL) return NULL; return m_pDefectControl->GetDefect(iDefect); }