#include "stdafx.h" #include "RcutDotProcess.h" #include "VisionBufferPro.h" #include "FileRecipe.h" #include "Bspline.h" #include "SoftVisionApp.h" CRcutDotProcess::CRcutDotProcess(){ /* code */ m_id = 0; m_strName.Format(_T("Point%d"), m_id); m_nPoseX = 0; m_nPoseY = 0; m_width = 0; m_height = 0; m_nUse = 0; m_nChamferSize = 25; m_nChipRange = 30; m_nChipThres = 50; m_nStartAngle = 8; m_nEndAngle = 82; m_nDx = 50; m_nDy = 50; m_nBinThres = 20; m_nEdgeSmooth = 1; m_x1 = -1; m_y1 = -1; m_xMmvsp = 0.0f; m_yMmvsp = 0.0f; HalconCpp::GenEmptyObj(&m_hImage); HalconCpp::GenEmptyObj(&m_hRoiRegion); HalconCpp::GenEmptyObj(&m_hChipRegion); } CRcutDotProcess::CRcutDotProcess(DimensionDir eDir, int id) { /* code */ m_eDir = eDir; m_id = id; switch (eDir) { case GLOBAL_DEFINE::DIMENSION_A: m_strName.Format(_T("TopA_RCut_Dot%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B: m_strName.Format(_T("TopB_RCut_Dot%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C: m_strName.Format(_T("TopC_RCut_Dot%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D: m_strName.Format(_T("TopD_RCut_Dot%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_A_DN: m_strName.Format(_T("BotA_RCut_Dot%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B_DN: m_strName.Format(_T("BotB_RCut_Dot%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C_DN: m_strName.Format(_T("BotC_RCut_Dot%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D_DN: m_strName.Format(_T("BotD_RCut_Dot%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_NONE: m_strName.Format(_T("P_RCut_Dot%d"), m_id); break; default: m_strName.Format(_T("P_RCut_Dot%d"), m_id); break; } m_nPoseX = 0; m_nPoseY = 0; m_width = 0; m_height = 0; m_nUse = 0; m_nChamferSize = 25; m_nChipRange = 30; m_nChipThres = 50; m_nStartAngle = 8; m_nEndAngle = 82; m_nDx = 50; m_nDy = 50; m_nBinThres = 20; m_nEdgeSmooth = 1; m_x1 = -1; m_y1 = -1; m_xMmvsp = 0.0f; m_yMmvsp = 0.0f; HalconCpp::GenEmptyObj(&m_hImage); HalconCpp::GenEmptyObj(&m_hRoiRegion); HalconCpp::GenEmptyObj(&m_hChipRegion); } CRcutDotProcess::~CRcutDotProcess(){ /* code */ } void CRcutDotProcess::getRegion(int &xPos, int &yPos, int &width, int &height){ /* code */ CBlSideData* pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir); if (nullptr == pSideData) return; //½øÐÐ×ø±êת»» Point2I offset; offset.x = m_nPoseX; offset.y = m_nPoseY; Point2I result; int nType = 1; if (1 != m_id) { nType = 2; } if (!CVisionBufferPro::TransformToReal(m_eDir, offset, nType, result)) return; int dWidth = (int)(m_width / pSideData->m_dPixelSizeX + 0.5f); int dHeight = (int)(m_height / pSideData->m_dPixelSizeY + 0.5f); if (1 == m_id) { //Top RCut) xPos = result.x - dWidth + 1; yPos = result.y - dHeight + 1; width = dWidth; height = dHeight; } else { xPos = result.x - dWidth + 1; yPos = result.y; width = dWidth; height = dHeight; } } void CRcutDotProcess::setRegion(int xPos, int yPos, int width, int height) { /* code */ //»ñÈ¡²à¹âÊý¾Ý CBlSideData* pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir); if (nullptr == pSideData) return; //ÉèÖÃͼÏñµÄÇøÓò int x1 = xPos; int y1 = yPos; int x2 = xPos + width - 1; int y2 = yPos + height - 1; // Log_GetDebug()->TraceInfo("Trace Set ROI: %d, %d, %d, %d, %d, %d, %d", (int)m_eDir, m_id, x1, y1, x2, y2, __LINE__); //¼Ç¼ÓÒÏÂ×ó±ß Point2I point; if (1 == m_id) { //Top RCut point.x = x2; point.y = y2; } else { //Bot RCut point.x = x2; point.y = y1; } Point2I pose, ptTop, ptBot; if (!CSoftVisionApp::getInstance()->transformToStandard(m_eDir,point, pose, ptTop, ptBot)) return; //ÉèÖÃposeºÍ¿í¸ß if (1 == m_id) { //Top RCut m_nPoseX = ptTop.x; m_nPoseY = ptTop.y; } else { m_nPoseX = ptBot.x; m_nPoseY = ptBot.y; } m_width = (int)(pSideData->m_dPixelSizeX * width); m_height = (int)(pSideData->m_dPixelSizeY * height); #if 0 int x = 0; int y = 0; int w = 0; int h = 0; getRegion(x, y, w, h); Log_GetDebug()->TraceInfo("Trace Input Info: %d, %d, %d, %d, %d", xPos, yPos, width, height, __LINE__); Log_GetDebug()->TraceInfo("Trace Input Info: %d, %d, %d, %d, %d", x, y, w, h, __LINE__); #endif } int CRcutDotProcess::getID(void) { /* code*/ return m_id; } void CRcutDotProcess::setID(int id) { /* code */ m_id = id; } int CRcutDotProcess::Execute(DimensionDir eDir, Point2I offset) { /* code */ m_fMaxChamber = 0.0f; m_nNgInfo.clear(); HalconCpp::GenEmptyObj(&m_hResultRegion); if (1 != m_nUse) return 0; if (m_width < 1 || m_height < 1) return 0; //1. »ñÈ¡½âÊÍ¶È getMmvsp(eDir); //2. »ñȡͼÏñ if (!getProcImage(eDir, offset, m_hImage)) return 0; //3. Chip¼ì²â InspectChip(m_hImage); //5. »ñÈ¡½á¹û getResult(); //5. ´òÓ¡Êý¾Ý int nRet = (int)(m_nNgInfo.size()); //KeyDebugInfo inf = CVisionRecipe::getInstance()->getKeyInfo(); //if (inf.isDebugTool && inf.isCutLineKey) { // for (int i = 0; i < nRet; i++) { // Log_GetDebug()->TraceInfo("Trace CutLine Result %d: %d, %d, (%.1f, %.1f), %d", // (int)m_eDir, m_id, i, m_nNgInfo[i].xResult, m_nNgInfo[i].yResult, __LINE__); // } //} return nRet; } void CRcutDotProcess::getResult(void) { /* code */ int nCount = ClsVision::ObjectNumber(m_hResultRegion); if (nCount < 1) return; NgDotInfo ng; ng.eDir = m_eDir; ng.id = m_id; ng.isRes = false; ng.minValue = m_nDx; ng.maxValue = m_nDy; ng.strName = m_strName; ng.ngType = DefectLoc_Chip; //ȱÏÝÀàÐÍ ng.eVision = VISION_RCUT; CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir); HalconCpp::HTuple hvArea, hvY, hvX; HalconCpp::HTuple hvY1, hvX1, hvY2, hvX2; HalconCpp::AreaCenter(m_hResultRegion, &hvArea, &hvY, &hvX); HalconCpp::SmallestRectangle1(m_hResultRegion, &hvY1, &hvX1, &hvY2, &hvX2); for (int i = 0; i < nCount; i++) { ng.result = hvArea[i].I(); ng.xResult = (int)(m_xMmvsp * (hvX2[i].I() - hvX1[i].I() + 1)); ng.yResult = (int)(m_yMmvsp * (hvY2[i].I() - hvY1[i].I() + 1)); ng.xPosPxl = (int)(0.5 * (hvX2[i].I() + hvX1[i].I())) + m_x1; ng.yPosPxl = (int)(0.5 * (hvY1[i].I() + hvY2[i].I())) + m_y1; ng.x1 = hvX1[i].I() + m_x1 - 1; ng.y1 = hvY1[i].I() + m_y1 - 1; ng.x2 = hvX2[i].I() + m_x1 + 1; ng.y2 = hvY2[i].I() + m_y1 + 1; if (nullptr != pSideData) { //ÇøÓòµÄÏÔʾ½á¹û DispVisionResult inf; inf.nType = 1; inf.eDir = (int)(m_eDir); inf.eVision = (int)(VISION_RCUT); inf.rectX1 = ng.x1; inf.rectY1 = ng.y1; inf.rectX2 = ng.x2; inf.rectY2 = ng.y2; pSideData->m_vDispVisionResult.push_back(inf); } m_nNgInfo.push_back(ng); } } void CRcutDotProcess::getMmvsp(DimensionDir eDir) { /* code */ CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(eDir); m_xMmvsp = pSideData->m_dPixelSizeX; m_yMmvsp = pSideData->m_dPixelSizeY; } bool CRcutDotProcess::getProcImage(DimensionDir eDir, Point2I offset, HalconCpp::HObject &hImage) { /* code */ m_eDir = eDir; if (m_width < 1 && m_height < 1) return false; if (0 == m_nPoseX && 0 == m_nPoseY) return false; int left = 0; int top = 0; int width = 0; int height = 0; getRegion(left, top, width, height); int x1 = left; int y1 = top; int x2 = left + width - 1; int y2 = top + height - 1; m_x1 = x1; m_y1 = y1; bool bRes = CVisionBufferPro::getImageROI(eDir, hImage, x1, y1, x2, y2); // Log_GetDebug()->TraceInfo("Trace ROI: %d, %d, %d, %d, %d, %d", (int)m_eDir, m_id, x1, y1, x2, y2); return bRes; } void CRcutDotProcess::TechImage(void) { /* code */ ClsVision::GenEmptyObject(m_hChipRegion); ClsVision::GenEmptyObject(m_hRoiRegion); Point2I offset; offset.x = 0; offset.y = 0; Execute(m_eDir, offset); } void CRcutDotProcess::InspectChip(HalconCpp::HObject &hImage) { /* code */ if (1 == m_id) { InspectTopChip(hImage); return; } InspectBotChip(hImage); } bool CRcutDotProcess::getRcutEdgePoints(HalconCpp::HObject &hImage, std::vector &vStartPoints, std::vector &vEndPoints, std::vector &vResPoints, double &maxDist, double &minDist) { /* code */ int width = 0; int height = 0; minDist = 0.0f; maxDist = 0.0f; ClsVision::GetImageSize(hImage, width, height); if (width < 1 || height < 1) return false; //HalconCpp::HWindow hDispWin(0, 0, width, height); //hDispWin.SetPart(0, 0, height - 1, width - 1); //hDispWin.SetColored(6); //hDispWin.DispObj(hImage); //hDispWin.Click(); maxDist = 0; minDist = MAXINT; if (1 == m_id) { double radius = sqrt(1.0 * width * width + 1.0 * height * height); double centerX = width - 1; double centerY = height - 1; double angle0 = m_nStartAngle; double angle1 = m_nEndAngle; double step = 1.0 * (m_nEndAngle - m_nStartAngle) / 15; std::vector vLines; std::vector vStartResults; std::vector vEndResuts; std::vector vFindRes; for (int i = 0; i < 16; i++) { double angle = angle0 + i * step - 90; double phi = DegToRad(angle); double cosx = cos(phi); double sinx = sin(phi); double x0 = centerX - radius * cosx; double y0 = centerY + radius * sinx; HalconCpp::HTuple hvRow, hvColumn, hvIsOverlapping, hvLength; HalconCpp::IntersectionLines(y0, x0, centerY, centerX, 5, -100, 5, 100, &hvRow, &hvColumn, &hvIsOverlapping); HalconCpp::TupleLength(hvRow, &hvLength); if (hvLength[0].I() < 1) continue; double x1 = hvColumn[0].D(); double y1 = hvRow[0].D(); if (x1 < 0) { HalconCpp::IntersectionLines(y0, x0, centerY, centerX, -100, 5, 100, 5, &hvRow, &hvColumn, &hvIsOverlapping); HalconCpp::TupleLength(hvRow, &hvLength); if (hvLength[0].I() < 1) continue; x1 = hvColumn[0].D(); y1 = hvRow[0].D(); } Line2D line; line.pt0.x = x1; line.pt0.y = y1; line.pt1.x = centerX; line.pt1.y = centerY; Point2D ptStart, ptEnd; bool isFind = CVisionBufferPro::GetMeasureRCutPos(hImage, line, m_nBinThres, 0, m_nChamferSize, -1.0f, ptStart, ptEnd); vLines.push_back(line); vStartResults.push_back(ptStart); vEndResuts.push_back(ptEnd); vFindRes.push_back(isFind); //HalconCpp::HObject hDispRegion, hDispObj1, hDispObj2; //HalconCpp::GenRegionLine(&hDispRegion, y1, x1, centerY, centerX); //if (!isFind) { // hDispWin.SetColor("blue"); // hDispWin.DispObj(hDispRegion); // hDispWin.Click(); //} if (!isFind) continue; //HalconCpp::GenCrossContourXld(&hDispObj1, ptStart.y, ptStart.x, 15, 0); //HalconCpp::GenCrossContourXld(&hDispObj2, ptEnd.y, ptEnd.x, 15, 0); //hDispWin.SetColor("blue"); //hDispWin.DispObj(hDispRegion); //hDispWin.SetColor("green"); //hDispWin.DispObj(hDispObj1); //hDispWin.DispObj(hDispObj2); //hDispWin.Click(); } double avg = 0.0f; double nCount = 0; for (int i = 0; i < 16; i++) { if (!vFindRes[i]) continue; double dist = ClsVision::DistancePP(vStartResults[i], vEndResuts[i]); avg += dist; nCount += 1; } if (nCount < 3) { avg = 0.8 * m_nChamferSize; } else { avg = avg / nCount; } for (int i = 0; i < 16; i++) { if (vFindRes[i]) continue; Line2D line = vLines[i]; Point2D ptStart, ptEnd; // Log_GetDebug()->TraceInfo("Line %d: %.2f, %.2f, %.2f, %.2f", i, line.pt0.x, line.pt0.y, line.pt1.x, line.pt1.y); bool isFind = CVisionBufferPro::GetMeasureRCutPos(hImage, line, m_nBinThres, 0, m_nChamferSize, avg, ptStart, ptEnd); //HalconCpp::HObject hDispRegion, hDispObj1, hDispObj2; //HalconCpp::GenRegionLine(&hDispRegion, line.pt0.y, line.pt0.x, line.pt1.y, line.pt1.x); //if (!isFind) { // hDispWin.SetColor("blue"); // hDispWin.DispObj(hDispRegion); // hDispWin.Click(); //} if (!isFind) continue; vFindRes[i] = isFind; vStartResults[i] = ptStart; vEndResuts[i] = ptEnd; //HalconCpp::GenCrossContourXld(&hDispObj1, ptStart.y, ptStart.x, 15, 0); //HalconCpp::GenCrossContourXld(&hDispObj2, ptEnd.y, ptEnd.x, 15, 0); //hDispWin.SetColor("blue"); //hDispWin.DispObj(hDispRegion); //hDispWin.SetColor("green"); //hDispWin.DispObj(hDispObj1); //hDispWin.DispObj(hDispObj2); //hDispWin.Click(); } for (int i = 0; i < 16; i++) { if (!vFindRes[i]) continue; double dist = ClsVision::DistancePP(vStartResults[i], vEndResuts[i]); if (dist > maxDist) maxDist = dist; if (dist < minDist) minDist = dist; vStartPoints.push_back(vStartResults[i]); vEndPoints.push_back(vEndResuts[i]); Point2D midPoint; midPoint.x = 0.5 * (vStartResults[i].x + vEndResuts[i].x); midPoint.y = 0.5 * (vStartResults[i].y + vEndResuts[i].y); vResPoints.push_back(midPoint); } } else { double radius = sqrt(1.0 * width * width + 1.0 * height * height); double centerX = width - 1; double centerY = 0; double angle0 = m_nStartAngle; double angle1 = m_nEndAngle; double step = 1.0 * (m_nEndAngle - m_nStartAngle) / 15; std::vector vLines; std::vector vStartResults; std::vector vEndResuts; std::vector vFindRes; for (int i = 0; i < 16; i++) { double angle = angle0 + i * step; double phi = DegToRad(angle); double cosx = cos(phi); double sinx = sin(phi); double x0 = centerX - radius * cosx; double y0 = centerY + radius * sinx; HalconCpp::HTuple hvRow, hvColumn, hvIsOverlapping, hvLength; HalconCpp::IntersectionLines(y0, x0, centerY, centerX, height - 6, -100, height - 6, 100, &hvRow, &hvColumn, &hvIsOverlapping); HalconCpp::TupleLength(hvRow, &hvLength); if (hvLength[0].I() < 1) continue; double x1 = hvColumn[0].D(); double y1 = hvRow[0].D(); if (x1 < 0) { HalconCpp::IntersectionLines(y0, x0, centerY, centerX, -100, 5, 100, 5, &hvRow, &hvColumn, &hvIsOverlapping); HalconCpp::TupleLength(hvRow, &hvLength); if (hvLength[0].I() < 1) continue; x1 = hvColumn[0].D(); y1 = hvRow[0].D(); } Line2D line; line.pt0.x = x1; line.pt0.y = y1; line.pt1.x = centerX; line.pt1.y = centerY; Point2D ptStart, ptEnd; bool isFind = CVisionBufferPro::GetMeasureRCutPos(hImage, line, m_nBinThres, 0, m_nChamferSize, -1.0f, ptStart, ptEnd); vLines.push_back(line); vStartResults.push_back(ptStart); vEndResuts.push_back(ptEnd); vFindRes.push_back(isFind); //HalconCpp::HObject hDispRegion, hDispObj1, hDispObj2; //HalconCpp::GenRegionLine(&hDispRegion, y1, x1, centerY, centerX); //if (!isFind) { // hDispWin.SetColor("blue"); // hDispWin.DispObj(hDispRegion); // hDispWin.Click(); //} if (!isFind) continue; //HalconCpp::GenCrossContourXld(&hDispObj1, ptStart.y, ptStart.x, 15, 0); //HalconCpp::GenCrossContourXld(&hDispObj2, ptEnd.y, ptEnd.x, 15, 0); //hDispWin.SetColor("blue"); //hDispWin.DispObj(hDispRegion); //hDispWin.SetColor("green"); //hDispWin.DispObj(hDispObj1); //hDispWin.DispObj(hDispObj2); //hDispWin.Click(); } double avg = 0.0f; double nCount = 0; for (int i = 0; i < 16; i++) { if (!vFindRes[i]) continue; double dist = ClsVision::DistancePP(vStartResults[i], vEndResuts[i]); avg += dist; nCount += 1; } if (nCount < 3) { avg = 0.8 * m_nChamferSize; } else { avg = avg / nCount; } for (int i = 0; i < 16; i++) { if (vFindRes[i]) continue; Line2D line = vLines[i]; Point2D ptStart, ptEnd; bool isFind = CVisionBufferPro::GetMeasureRCutPos(hImage, line, m_nBinThres, 0, m_nChamferSize, avg, ptStart, ptEnd); //HalconCpp::HObject hDispRegion, hDispObj1, hDispObj2; //HalconCpp::GenRegionLine(&hDispRegion, line.pt0.y, line.pt0.x, line.pt1.y, line.pt1.x); //if (!isFind) { // hDispWin.SetColor("blue"); // hDispWin.DispObj(hDispRegion); // hDispWin.Click(); //} if (!isFind) continue; vFindRes[i] = isFind; vStartResults[i] = ptStart; vEndResuts[i] = ptEnd; //HalconCpp::GenCrossContourXld(&hDispObj1, ptStart.y, ptStart.x, 15, 0); //HalconCpp::GenCrossContourXld(&hDispObj2, ptEnd.y, ptEnd.x, 15, 0); //hDispWin.SetColor("blue"); //hDispWin.DispObj(hDispRegion); //hDispWin.SetColor("green"); //hDispWin.DispObj(hDispObj1); //hDispWin.DispObj(hDispObj2); //hDispWin.Click(); } for (int i = 0; i < 16; i++) { if (!vFindRes[i]) continue; double dist = ClsVision::DistancePP(vStartResults[i], vEndResuts[i]); if (dist > maxDist) maxDist = dist; if (dist < minDist) minDist = dist; vStartPoints.push_back(vStartResults[i]); vEndPoints.push_back(vEndResuts[i]); Point2D midPoint; midPoint.x = 0.5 * (vStartResults[i].x + vEndResuts[i].x); midPoint.y = 0.5 * (vStartResults[i].y + vEndResuts[i].y); vResPoints.push_back(midPoint); } } int num = (int)(vStartPoints.size()); if (num < 3) return false; return true; } void CRcutDotProcess::InspectBotChip(HalconCpp::HObject &hImage) { /* code */ if (2 != m_id) return; double maxDist = 0.0f; double minDist = 0.0f; std::vector vStartPoints; std::vector vEndPoints; std::vector vResPoints; if (!getRcutEdgePoints(hImage, vStartPoints, vEndPoints, vResPoints, maxDist, minDist)) return; m_fMaxChamber = maxDist; m_fMinChamber = minDist; //int width = 0; //int height = 0; //ClsVision::GetImageSize(hImage, width, height); //HalconCpp::HWindow hDispWin(0, 0, width, height); //hDispWin.SetPart(0, 0, height - 1, width - 1); //hDispWin.SetColored(6); //hDispWin.DispObj(hImage); //int sz = (int)(vStartPoints.size()); //for (int i = 0; i < sz; i++) { // HalconCpp::HObject dispObject1, dispObject2; // HalconCpp::GenCrossContourXld(&dispObject1, vStartPoints[i].y, vStartPoints[i].x, 15, 0); // HalconCpp::GenCrossContourXld(&dispObject2, vEndPoints[i].y, vEndPoints[i].x, 15, 0); // hDispWin.DispObj(dispObject1); // hDispWin.DispObj(dispObject2); //} //hDispWin.Click(); HalconCpp::HObject hContourRegion, hChipRegion; if (!getRCutRegion(vStartPoints, vEndPoints, hContourRegion, hChipRegion)) return; m_hRoiRegion = hContourRegion; m_hChipRegion = hChipRegion; //hDispWin.SetDraw("fill"); //hDispWin.DispObj(hContourRegion); //hDispWin.Click(); //hDispWin.DispObj(hImage); //hDispWin.DispObj(hChipRegion); //hDispWin.Click(); HalconCpp::HObject hRectRegion, hSelectRegion; genRectangeRegion(hRectRegion); ImageBinThreshold(hImage, hRectRegion, hContourRegion, hSelectRegion); //hDispWin.DispObj(hImage); //hDispWin.DispObj(hSelectRegion); //hDispWin.Click(); HalconCpp::HObject hRealRegion; if (!getRCutRealRegion(hSelectRegion, vResPoints, hRealRegion)) return; //hDispWin.DispObj(hImage); //hDispWin.DispObj(hRealRegion); //hDispWin.Click(); InpsectChipRegion(hRealRegion, hChipRegion); } bool CRcutDotProcess::getRCutRegion(std::vector vStartPoints, std::vector vEndPoints, HalconCpp::HObject &hTargetRegion, HalconCpp::HObject &hRoiRegion) { /* code */ int num = (int)(vStartPoints.size()); if (num < 1) return false; //1. »ñÈ¡µãÕó std::vector vStartCtrlPoint; std::vector vEndCtrlPoint; for (int i = 0; i < num; i++) { float x0 = (float)vStartPoints[i].x; float y0 = (float)vStartPoints[i].y; vec p0(x0, y0, 0); vStartCtrlPoint.push_back(p0); float x1 = (float)vEndPoints[i].x; float y1 = (float)vEndPoints[i].y; vec p1(x1, y1, 0); vEndCtrlPoint.push_back(p1); } //2. Ö´ÐÐÑùÌõ²åÖµ CBspline bcurve(vStartCtrlPoint); bcurve.execute(); CBspline endBcurve(vEndCtrlPoint); endBcurve.execute(); //3. »ñÈ¡ÑùÌõ²îÖµÂÖÀªµã int sz = (int)(bcurve.m_vPtResults.size()); if (sz < 1) return false; HalconCpp::HTuple hvY, hvX; for (int i = 0; i < sz; i++) { hvX[i] = bcurve.m_vPtResults[i][0]; hvY[i] = bcurve.m_vPtResults[i][1]; } int idx = sz; sz = (int)(endBcurve.m_vPtResults.size()); if (sz > 0) { for (int i = sz - 1; i >= 0; i--) { hvX[idx] = endBcurve.m_vPtResults[i][0]; hvY[idx] = endBcurve.m_vPtResults[i][1]; idx += 1; } } if (idx < 3) return false; //4. Éú²úÇøÓò HalconCpp::GenRegionPolygonFilled(&hTargetRegion, hvY, hvX); HalconCpp::HTuple hvY0, hvX0; for (int i = 0; i < sz; i++) { hvX0[i] = endBcurve.m_vPtResults[i][0]; hvY0[i] = endBcurve.m_vPtResults[i][1]; } if (1 == m_id) { hvX0[sz] = m_width - 1; hvY0[sz] = m_height - 1; } else { hvX0[sz] = m_width - 1; hvY0[sz] = 0; } HalconCpp::HObject hRegion1; HalconCpp::GenRegionPolygonFilled(&hRegion1, hvY0, hvX0); HalconCpp::HObject hTempRegion, hTempRegio2, hTempRegio3; HalconCpp::DilationCircle(hTargetRegion, &hTempRegion, 2.5); HalconCpp::DilationCircle(hTempRegion, &hTempRegio2, m_nChipRange); HalconCpp::Difference(hTempRegio2, hTempRegion, &hTempRegio3); HalconCpp::Intersection(hTempRegio3, hRegion1, &hRoiRegion); return true; } void CRcutDotProcess::genRectangeRegion(HalconCpp::HObject &hRectRegion) { /* code */ if (1 == m_id) { double centerX = m_width - 1; double centerY = m_height - 1; double radius = sqrt(1.0 * m_width * m_width + 1.0 * m_height * m_height); double x1 = 0.0f; double y1 = 0.0f; if (true) { //Ö±Ïß double angle = m_nStartAngle - 90.0f; double phi = DegToRad(angle); double cosx = cos(phi); double sinx = sin(phi); double x0 = centerX - radius * cosx; double y0 = centerY + radius * sinx; HalconCpp::HTuple hvY, hvX, hvIsOverlapping; HalconCpp::IntersectionLines(y0, x0, centerY, centerX, 0, -100, 0, 100, &hvY, &hvX, &hvIsOverlapping); x1 = hvX[0].D(); y1 = hvY[0].D(); } double x2 = 0.0f; double y2 = 0.0f; if (true) { //Ö±Ïß double angle = m_nEndAngle - 90.0f; double phi = DegToRad(angle); double cosx = cos(phi); double sinx = sin(phi); double x0 = centerX - radius * cosx; double y0 = centerY + radius * sinx; HalconCpp::HTuple hvY, hvX, hvIsOverlapping; HalconCpp::IntersectionLines(y0, x0, centerY, centerX, -100, 0, 100, 0, &hvY, &hvX, &hvIsOverlapping); x2 = hvX[0].D(); y2 = hvY[0].D(); } HalconCpp::GenRectangle1(&hRectRegion, 0, 0, y2, x1); } else { double centerX = m_width - 1; double centerY = 0; double radius = sqrt(1.0 * m_width * m_width + 1.0 * m_height * m_height); double x1 = 0.0f; double y1 = 0.0f; if (true) { //Ö±Ïß double angle = m_nStartAngle; double phi = DegToRad(angle); double cosx = cos(phi); double sinx = sin(phi); double x0 = centerX - radius * cosx; double y0 = centerY + radius * sinx; HalconCpp::HTuple hvY, hvX, hvIsOverlapping; HalconCpp::IntersectionLines(y0, x0, centerY, centerX, 0, 0, m_height-1, 0, &hvY, &hvX, &hvIsOverlapping); y1 = hvY[0].D(); } double x2 = 0.0f; double y2 = 0.0f; if (true) { //Ö±Ïß double angle = m_nEndAngle; double phi = DegToRad(angle); double cosx = cos(phi); double sinx = sin(phi); double x0 = centerX - radius * cosx; double y0 = centerY + radius * sinx; HalconCpp::HTuple hvY, hvX, hvIsOverlapping; HalconCpp::IntersectionLines(y0, x0, centerY, centerX, m_height - 1, -100, m_height - 1, 100, &hvY, &hvX, &hvIsOverlapping); x2 = hvX[0].D(); y2 = m_height - 1; } HalconCpp::GenRectangle1(&hRectRegion, y1, x1, y2, x2); } } void CRcutDotProcess::ImageBinThreshold(HalconCpp::HObject &hImage, HalconCpp::HObject &hRectRegion, HalconCpp::HObject &hRcutRegion, HalconCpp::HObject &hTarget) { /* code */ int width = 0; int height = 0; ClsVision::GetImageSize(hImage, width, height); if (width < 1 || height < 1) { HalconCpp::GenEmptyObj(&hTarget); return; } //1. ͼÏñ·Ö¸î HalconCpp::HObject hProcObject, hBinRegion; HalconCpp::ReduceDomain(hImage, hRectRegion, &hProcObject); HalconCpp::Threshold(hProcObject, &hBinRegion, 0, m_nChipThres); //2. ͼÏñºÏ²¢ HalconCpp::HObject hTempRegion, hUionRegion, hConRegion; HalconCpp::ConcatObj(hRcutRegion, hBinRegion, &hTempRegion); HalconCpp::Union1(hTempRegion, &hUionRegion); HalconCpp::Connection(hUionRegion, &hConRegion); HalconCpp::SelectShape(hConRegion, &hTarget, "area", "and", 500, MAXINT); } bool CRcutDotProcess::getRCutRealRegion(HalconCpp::HObject &hSelectRegion, std::vector vPoints, HalconCpp::HObject &hTarget) { /* code */ int num = (int)(vPoints.size()); if (num < 3) return false; HalconCpp::HObject hSelRegion; HalconCpp::HTuple hvIndex, hvLength; HalconCpp::GenEmptyObj(&hSelRegion); //1. µÚÒ»¸öµã HalconCpp::GetRegionIndex(hSelectRegion, (int)vPoints[1].y, (int)vPoints[1].x, &hvIndex); HalconCpp::TupleLength(hvIndex, &hvLength); if (hvLength[0].I() > 0) { if (hvIndex[0].I() >= 1) { HalconCpp::SelectObj(hSelectRegion, &hSelRegion, hvIndex[0].I()); } } //2. Öмäµã int idx = (int)((num - 1) / 2); HalconCpp::GetRegionIndex(hSelectRegion, (int)vPoints[idx].y, (int)vPoints[idx].x, &hvIndex); HalconCpp::TupleLength(hvIndex, &hvLength); if (hvLength[0].I() > 0) { if (hvIndex[0].I() >= 1) { HalconCpp::HObject hTempRegion, hTempRegion1; HalconCpp::SelectObj(hSelectRegion, &hTempRegion, hvIndex[0].I()); HalconCpp::ConcatObj(hTempRegion, hSelRegion, &hTempRegion1); hSelRegion.Clear(); hSelRegion = hTempRegion1; } } //3. ×îºóÒ»¸ö HalconCpp::GetRegionIndex(hSelectRegion, (int)vPoints[num - 2].y, (int)vPoints[num - 2].x, &hvIndex); HalconCpp::TupleLength(hvIndex, &hvLength); if (hvLength[0].I() > 0) { if (hvIndex[0].I() >= 1) { HalconCpp::HObject hTempRegion, hTempRegion1; HalconCpp::SelectObj(hSelectRegion, &hTempRegion, hvIndex[0].I()); HalconCpp::ConcatObj(hTempRegion, hSelRegion, &hTempRegion1); hSelRegion.Clear(); hSelRegion = hTempRegion1; } } HalconCpp::Union1(hSelRegion, &hTarget); return true; } void CRcutDotProcess::InspectTopChip(HalconCpp::HObject &hImage) { /* code */ if (1 != m_id) return; double maxDist = 0.0f; double minDist = 0.0f; std::vector vStartPoints; std::vector vEndPoints; std::vector vResPoints; if (!getRcutEdgePoints(hImage, vStartPoints, vEndPoints, vResPoints, maxDist, minDist)) return; m_fMaxChamber = maxDist; m_fMinChamber = minDist; //int width = 0; //int height = 0; //ClsVision::GetImageSize(hImage, width, height); //HalconCpp::HWindow hDispWin(0, 0, width, height); //hDispWin.SetPart(0, 0, height - 1, width - 1); //hDispWin.SetColored(6); //hDispWin.DispObj(hImage); //int sz = (int)(vStartPoints.size()); //for (int i = 0; i < sz; i++) { // HalconCpp::HObject dispObject1, dispObject2; // HalconCpp::GenCrossContourXld(&dispObject1, vStartPoints[i].y, vStartPoints[i].x, 15, 0); // HalconCpp::GenCrossContourXld(&dispObject2, vEndPoints[i].y, vEndPoints[i].x, 15, 0); // hDispWin.DispObj(dispObject1); // hDispWin.DispObj(dispObject2); //} //hDispWin.Click(); HalconCpp::HObject hContourRegion, hChipRegion; if (!getRCutRegion(vStartPoints, vEndPoints, hContourRegion, hChipRegion)) return; m_hRoiRegion = hContourRegion; m_hChipRegion = hChipRegion; //hDispWin.SetDraw("fill"); //hDispWin.DispObj(hContourRegion); //hDispWin.Click(); //hDispWin.DispObj(hImage); //hDispWin.DispObj(hChipRegion); //hDispWin.Click(); HalconCpp::HObject hRectRegion, hSelectRegion; genRectangeRegion(hRectRegion); ImageBinThreshold(hImage, hRectRegion, hContourRegion, hSelectRegion); //hDispWin.DispObj(hImage); //hDispWin.DispObj(hSelectRegion); //hDispWin.Click(); HalconCpp::HObject hRealRegion; if (!getRCutRealRegion(hSelectRegion, vResPoints, hRealRegion)) return; //hDispWin.DispObj(hImage); //hDispWin.DispObj(hRealRegion); //hDispWin.Click(); InpsectChipRegion(hRealRegion, hChipRegion); //hDispWin.DispObj(hImage); //hDispWin.DispObj(m_hResultRegion); //hDispWin.Click(); } void CRcutDotProcess::InpsectChipRegion(HalconCpp::HObject &hChipRegion, HalconCpp::HObject &hRoiRegion) { /* code */ if (m_xMmvsp < 0.001 || m_yMmvsp < 0.001) return; int fzX = (int)(m_nDx / m_xMmvsp + 0.5f); int fzY = (int)(m_nDy / m_yMmvsp + 0.5f); HalconCpp::HObject hTempRegion, hConRegion, hSelRegionX, hSelRegionY, hConcatRegion, hUionRegion, hResRegion, hOpenRegion; HalconCpp::Intersection(hChipRegion, hRoiRegion, &hTempRegion); if (m_nEdgeSmooth > 0) { HalconCpp::OpeningCircle(hTempRegion, &hOpenRegion, m_nEdgeSmooth + 0.5); } else { hOpenRegion = hTempRegion; } HalconCpp::Connection(hOpenRegion, &hConRegion); HalconCpp::SelectShape(hConRegion, &hSelRegionX, "width", "and", fzX, MAXINT); HalconCpp::SelectShape(hConRegion, &hSelRegionY, "height", "and", fzY, MAXINT); HalconCpp::ConcatObj(hSelRegionX, hSelRegionY, &hConcatRegion); HalconCpp::Union1(hConcatRegion, &hUionRegion); HalconCpp::Connection(hUionRegion, &hResRegion); int num = ClsVision::ObjectNumber(hResRegion); if (num < 1) return; m_hResultRegion = hResRegion; } HalconCpp::HObject& CRcutDotProcess::getImage(void) { /* code */ return m_hImage; } HalconCpp::HObject& CRcutDotProcess::getChipRegion(void) { /* code */ return m_hChipRegion; } HalconCpp::HObject& CRcutDotProcess::getRoiRegion(void) { /* code */ return m_hRoiRegion; } Json::Value CRcutDotProcess::WriteToJson(void) { /* code */ std::string strName = CFileRecipe::toString(m_strName); Json::Value jsValue; jsValue["alg type"] = RCUT_VISION_TYPE; jsValue["side"] = (int)(m_eDir); jsValue["id"] = m_id; jsValue["name"] = strName; jsValue["use"] = m_nUse; jsValue["pos x"] = m_nPoseX; jsValue["pos y"] = m_nPoseY; jsValue["width"] = m_width; jsValue["height"] = m_height; jsValue["chamber size"] = m_nChamferSize; jsValue["chamber range"] = m_nChipRange; jsValue["chamber thres"] = m_nChipThres; jsValue["start angle"] = m_nStartAngle; jsValue["end angle"] = m_nEndAngle; jsValue["dx"] = m_nDx; jsValue["dy"] = m_nDy; jsValue["bin thres"] = m_nBinThres; jsValue["edge smooth"] = m_nEdgeSmooth; return jsValue; } void CRcutDotProcess::DecodeJson(Json::Value &jsValue) { /* code */ int num = (int)(jsValue.size()); if (num < 1) return; //1. side std::string strName = "side"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_eDir = (DimensionDir)(jsValue[strName.c_str()].asInt()); } //2. id strName = "id"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_id = jsValue[strName.c_str()].asInt(); } //3. name strName = "name"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isString()) { m_strName = CFileRecipe::toCString(jsValue[strName.c_str()].asString()); } //4. use strName = "use"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nUse = jsValue[strName.c_str()].asInt(); } //5. pos x strName = "pos x"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nPoseX = jsValue[strName.c_str()].asInt(); } //6. pos y strName = "pos y"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nPoseY = jsValue[strName.c_str()].asInt(); } //7. width strName = "width"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_width = jsValue[strName.c_str()].asInt(); } //8. height strName = "height"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_height = jsValue[strName.c_str()].asInt(); } //9. chamber size strName = "chamber size"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nChamferSize = jsValue[strName.c_str()].asInt(); } //10. chamber range strName = "chamber range"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nChipRange = jsValue[strName.c_str()].asInt(); } //11. chamber thres strName = "chamber thres"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nChipThres = jsValue[strName.c_str()].asInt(); } //12. start angle strName = "start angle"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nStartAngle = jsValue[strName.c_str()].asInt(); } //13. end angle strName = "end angle"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nEndAngle = jsValue[strName.c_str()].asInt(); } //14. dx strName = "dx"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nDx = jsValue[strName.c_str()].asInt(); } //15. dy strName = "dy"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nDy = jsValue[strName.c_str()].asInt(); } //16. bin thres strName = "bin thres"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nBinThres = jsValue[strName.c_str()].asInt(); } //17. edge smooth strName = "edge smooth"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nEdgeSmooth = jsValue[strName.c_str()].asInt(); } }