#include "stdafx.h" #include "ThresDotProcess.h" #include "VisionRecipe.h" #include "VisionBufferPro.h" #include "FileRecipe.h" #include "SoftVisionApp.h" CThresDotProcess::CThresDotProcess(){ /* code */ m_nUse = 0; m_id = 0; m_strName.Format(_T("Point%d"), m_id); m_minThres = 60; m_maxThres = 255; m_minSizeX = 50; m_minSizeY = 50; m_eDir = DIMENSION_NONE; m_nPoseX = 0; m_nPoseY = 0; m_width = 0; m_height = 0; m_nSmoothSize = 2; m_poseType = 0; } CThresDotProcess::CThresDotProcess(DimensionDir eDir, int id) { /* code */ m_eDir = eDir; m_nUse = 0; m_id = id; switch (eDir) { case GLOBAL_DEFINE::DIMENSION_A: m_strName.Format(_T("TopA_T%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B: m_strName.Format(_T("TopB_T%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C: m_strName.Format(_T("TopC_T%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D: m_strName.Format(_T("TopD_T%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_A_DN: m_strName.Format(_T("BotA_T%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B_DN: m_strName.Format(_T("BotB_T%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C_DN: m_strName.Format(_T("BotC_T%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D_DN: m_strName.Format(_T("BotD_T%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_NONE: m_strName.Format(_T("P_T%d"), m_id); break; default: m_strName.Format(_T("P_T%d"), m_id); break; } m_minThres = 60; m_maxThres = 255; m_nSmoothSize = 2; m_minSizeX = 50; m_minSizeY = 50; m_nPoseX = 0; m_nPoseY = 0; m_width = 0; m_height = 0; m_poseType = 0; } CThresDotProcess::~CThresDotProcess(){ /* code */ m_nNgInfo.clear(); } bool CThresDotProcess::ExecChip(DimensionDir eDir) { /* code */ CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir); if (NULL == pSideData) return false; if (0 == m_poseType) { if (!pSideData->m_bTopMark_Find) return false; if (!pSideData->m_bBotMark_Find) return false; } else { if (!pSideData->m_bTopPoint_Find) return false; if (!pSideData->m_bBotPoint_Find) return false; } int x = 0; int y = 0; int width = 0; int height = 0; getRegion(x, y, width, height); int x1 = x; int y1 = y; int x2 = x1 + width - 1; int y2 = y1 + height - 1; m_ptLeftTop.x = x1; m_ptLeftTop.y = y1; // Log_GetDebug()->TraceInfo("Trace ROI: (%d, %d, %d, %d), %d", x1, y1, x2, y2, __LINE__); if (nullptr != pSideData) { //ÇøÓòµÄÏÔʾ½á¹û DispVisionResult inf; inf.nType = 1; inf.strName = m_strName; inf.eDir = (int)(m_eDir); inf.eVision = (int)(VISION_THRES); inf.rectX1 = x1; inf.rectY1 = y1; inf.rectX2 = x2; inf.rectY2 = y2; pSideData->m_vDispVisionResult.push_back(inf); inf.nType = 4; inf.strName = m_strName; inf.eDir = (int)(m_eDir); inf.eVision = (int)(VISION_THRES); inf.pointX = x1; inf.pointY = y1; pSideData->m_vDispVisionResult.push_back(inf); } HalconCpp::HObject hImage; if (!CVisionBufferPro::getImageROI(eDir, hImage, x1, y1, x2, y2)) return false; //HalconCpp::HWindow hDispWin(0, 0, m_width, m_height); //hDispWin.SetPart(0, 0, m_height - 1, m_width - 1); //hDispWin.SetColored(6); //hDispWin.DispObj(hImage); //hDispWin.Click(); int fzX = (int)(m_minSizeX / m_xMmvsp + 0.5f); int fzY = (int)(m_minSizeY / m_yMmvsp + 0.5f); HalconCpp::HObject hResultRegion; HalconCpp::GenEmptyObj(&hResultRegion); if (m_minThres > 0) { HalconCpp::HObject hRegion, hConRegion, hSelRegionX, hSelRegionY, hConcatRegion, hOpenRegion; HalconCpp::Threshold(hImage, &hRegion, 0, m_minThres); if (m_nSmoothSize > 0) { double fSmoothSize = m_nSmoothSize + 0.5; HalconCpp::OpeningCircle(hRegion, &hOpenRegion, fSmoothSize); } else { hOpenRegion = hRegion; } 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, &hResultRegion); } if (m_maxThres < 255) { HalconCpp::HObject hRegion, hConRegion, hSelRegionX, hSelRegionY, hConcatRegion, hOpenRegion, hUnionRegion; HalconCpp::Threshold(hImage, &hRegion, m_maxThres, 255); if (m_nSmoothSize > 0) { double fSmoothSize = m_nSmoothSize + 0.5; HalconCpp::OpeningCircle(hRegion, &hOpenRegion, fSmoothSize); } else { hOpenRegion = hRegion; } 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, &hUnionRegion); HalconCpp::GenEmptyObj(&hResultRegion); hResultRegion.Clear(); hResultRegion = hUnionRegion; } HalconCpp::HObject hConRegion; HalconCpp::Connection(hResultRegion, &hConRegion); //hDispWin.DispObj(hConRegion); //hDispWin.Click(); int nCount = ClsVision::ObjectNumber(hConRegion); if (nCount < 1) return false; getResult(hConRegion); return true; } void CThresDotProcess::getResult(HalconCpp::HObject &hResultRegion) { /* code */ int nCount = ClsVision::ObjectNumber(hResultRegion); if (nCount < 1) return; NgDotInfo ng; ng.eDir = m_eDir; ng.id = m_id; ng.isRes = false; ng.minValue = m_minSizeX; ng.maxValue = m_minSizeY; ng.strName = m_strName; ng.ngType = DefectLoc_Chip; //ȱÏÝÀàÐÍ ng.eVision = VISION_THRES; CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir); HalconCpp::HTuple hvArea, hvY, hvX; HalconCpp::HTuple hvY1, hvX1, hvY2, hvX2; HalconCpp::AreaCenter(hResultRegion, &hvArea, &hvY, &hvX); HalconCpp::SmallestRectangle1(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_ptLeftTop.x; ng.yPosPxl = (int)(0.5 * (hvY1[i].I() + hvY2[i].I())) + m_ptLeftTop.y; ng.x1 = hvX1[i].I() + m_ptLeftTop.x - 1; ng.y1 = hvY1[i].I() + m_ptLeftTop.y - 1; ng.x2 = hvX2[i].I() + m_ptLeftTop.x + 1; ng.y2 = hvY2[i].I() + m_ptLeftTop.y + 1; if (nullptr != pSideData) { //ÇøÓòµÄÏÔʾ½á¹û DispVisionResult inf; inf.nType = 1; inf.strName = m_strName; inf.eDir = (int)(m_eDir); inf.eVision = (int)(VISION_THRES); 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 CThresDotProcess::getRegion(int &xPos, int &yPos, int &width, int &height) { /* code */ CBlSideData* pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir); if (nullptr == pSideData) return; //»ñÈ¡×ø±ê Point2I offset, result; offset.x = m_nPoseX; offset.y = m_nPoseY; if (!CVisionBufferPro::TransformToReal(m_eDir, offset, m_poseType, result)) return; xPos = result.x; yPos = result.y; width = (int)(m_width / pSideData->m_dPixelSizeX); height = (int)(m_height / pSideData->m_dPixelSizeY); } void CThresDotProcess::setRegion(int xPos, int yPos, int width, int height) { /* code */ //ÉèÖÃͼÏñµÄROIÇøÓò 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", x1, y1, x2, y2, __LINE__); Point2I point; point.x = x1; point.y = y1; Point2I pose, ptTop, ptBot; if (!CSoftVisionApp::getInstance()->transformToStandard(m_eDir, point, pose, ptTop, ptBot)) return; if (0 == m_poseType) { m_nPoseX = pose.x; m_nPoseY = pose.y; } else if (1 == m_poseType) { m_nPoseX = ptTop.x; m_nPoseY = ptTop.y; } else if (2 == m_poseType) { m_nPoseX = ptBot.x; m_nPoseY = ptBot.y; } m_width = (int)(pSideData->m_dPixelSizeX * width); m_height = (int)(pSideData->m_dPixelSizeY * height); } int CThresDotProcess::getID(void) { /* code*/ return m_id; } void CThresDotProcess::setID(int id) { /* code */ m_id = id; } void CThresDotProcess::getMmvsp(DimensionDir eDir) { /* code */ CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(eDir); m_xMmvsp = pSideData->m_dPixelSizeX; m_yMmvsp = pSideData->m_dPixelSizeY; } int CThresDotProcess::Execute(DimensionDir eDir, Point2I offset) { /* code */ m_nNgInfo.clear(); if (1 != m_nUse) return 0; if (m_width < 1 || m_height < 1) return 0; //1. »ñÈ¡½âÊÍ¶È getMmvsp(eDir); //2. Ö´Ðмì²â ExecChip(eDir); int nRet = (int)(m_nNgInfo.size()); return nRet; } Json::Value CThresDotProcess::WriteToJson(void) { /* code */ std::string strName = CFileRecipe::toString(m_strName); Json::Value jsValue; jsValue["alg type"] = THRES_VISION_TYPE; jsValue["side"] = (int)(m_eDir); jsValue["id"] = m_id; jsValue["name"] = strName; jsValue["pos x"] = m_nPoseX; jsValue["pos y"] = m_nPoseY; jsValue["width"] = m_width; jsValue["height"] = m_height; jsValue["use"] = m_nUse; jsValue["min thres"] = m_minThres; jsValue["max thres"] = m_maxThres; jsValue["min size x"] = m_minSizeX; jsValue["min size y"] = m_minSizeY; jsValue["smooth size"] = m_nSmoothSize; jsValue["pose type"] = m_poseType; return jsValue; } void CThresDotProcess::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(); } //8. width strName = "width"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_width = jsValue[strName.c_str()].asInt(); } //9. height strName = "height"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_height = jsValue[strName.c_str()].asInt(); } //10. min thres strName = "min thres"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_minThres = jsValue[strName.c_str()].asInt(); } //11. min strName = "max thres"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_maxThres = jsValue[strName.c_str()].asInt(); } //12. min size x strName = "min size x"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_minSizeX = jsValue[strName.c_str()].asInt(); } //13. min size y strName = "min size y"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_minSizeY = jsValue[strName.c_str()].asInt(); } //14. smooth size strName = "smooth size"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nSmoothSize = jsValue[strName.c_str()].asInt(); } //15. smooth size strName = "pose type"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nSmoothSize = jsValue[strName.c_str()].asInt(); } }