#include "stdafx.h" #include "HoleDotProcess.h" #include "VisionRecipe.h" #include "VisionBufferPro.h" #include "FileRecipe.h" CHoleDotProcess::CHoleDotProcess(){ /* code */ m_eDir = DIMENSION_NONE; m_id = 0; m_strName.Format(_T("P%d"), m_id); m_nUse = 0; m_nPoseType = 0; m_nShapeType = 2; m_nSmoothSize = 8; m_nBrightType = 1; m_nDefectSize = 3; m_nThres = 45; m_xScale = 1.0f; m_yScale = 1.0f; m_isScale = false; m_nPoseX = 0; m_nPoseX = 0; m_score = 0.5f; m_idModel = HalconCpp::HTuple(); m_nNgInfo.clear(); HalconCpp::GenEmptyObj(&m_hImage); HalconCpp::GenEmptyObj(&m_hImageResult); } CHoleDotProcess::CHoleDotProcess(DimensionDir eDir, int id) { /* code */ m_eDir = eDir; m_id = id; switch (eDir) { case GLOBAL_DEFINE::DIMENSION_A: m_strName.Format(_T("TopA_H%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B: m_strName.Format(_T("TopB_H%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C: m_strName.Format(_T("TopC_H%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D: m_strName.Format(_T("TopD_H%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_A_DN: m_strName.Format(_T("BotA_H%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B_DN: m_strName.Format(_T("BotB_H%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C_DN: m_strName.Format(_T("BotC_H%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D_DN: m_strName.Format(_T("BotD_H%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_NONE: m_strName.Format(_T("P_H%d"), m_id); break; default: m_strName.Format(_T("P_H%d"), m_id); break; } m_nUse = 0; m_nPoseType = 0; m_nShapeType = 2; m_nSmoothSize = 8; m_nBrightType = 1; m_nDefectSize = 3; m_nThres = 45; m_xScale = 1.0f; m_yScale = 1.0f; m_isScale = false; m_nPoseX = 0; m_nPoseX = 0; m_score = 0.5f; m_idModel = HalconCpp::HTuple(); m_nNgInfo.clear(); HalconCpp::GenEmptyObj(&m_hImage); HalconCpp::GenEmptyObj(&m_hImageResult); } CHoleDotProcess::~CHoleDotProcess(){ /* code */ } Json::Value CHoleDotProcess::WriteToJson(std::string& strDir) { /* code */ int iValue = 0; if (ClsVision::ObjectExist(m_hImage)) { int width = 0; int height = 0; ClsVision::GetImageSize(m_hImage, width, height); if (width > 0 && height > 0) { iValue = 1; } } std::string strImage = "NULL"; if (1 == iValue) { strImage = ClsVision::FormatString("%d_%d_hole", m_eDir, m_id); } Json::Value jsValue; std::string strName = CFileRecipe::toString(m_strName); jsValue["alg type"] = HOLE_VISION_TYPE; jsValue["side"] = (int)(m_eDir); jsValue["id"] = m_id; jsValue["name"] = strName; jsValue["use"] = m_nUse; jsValue["Pos Type"] = m_nPoseType; jsValue["Shape Type"] = m_nShapeType; jsValue["Bright Type"] = m_nBrightType; jsValue["Thres"] = m_nThres; jsValue["Smooth Size"] = m_nSmoothSize; jsValue["Defect Size"] = m_nDefectSize; jsValue["Pose X"] = m_nPoseX; jsValue["Pose Y"] = m_nPoseY; jsValue["image file"] = strImage; std::string imageDir = strDir + "Image\\"; if (!CFileRecipe::fileIsExist(imageDir)) { CFileRecipe::makeDir(imageDir); } if (0 == iValue) return jsValue; std::string fileName = imageDir + strImage + ".jpg"; HalconCpp::WriteImage(m_hImage, "jpeg", 0, fileName.c_str()); return jsValue; } void CHoleDotProcess::DecodeJson(std::string& strDir, Json::Value& jsValue) { /* code */ if (strDir.empty()) return; 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. Pose Type strName = "Pos Type"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nPoseType = jsValue[strName.c_str()].asInt(); } //6. Shape Type strName = "Shape Type"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nShapeType = jsValue[strName.c_str()].asInt(); } //7. Bright Type strName = "Bright Type"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nBrightType = jsValue[strName.c_str()].asInt(); } //8. Thres strName = "Thres"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nThres = jsValue[strName.c_str()].asInt(); } //9. Smooth Size strName = "Smooth Size"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nSmoothSize = jsValue[strName.c_str()].asInt(); } //10. Defect Size strName = "Defect Size"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nDefectSize = jsValue[strName.c_str()].asInt(); } //11. Pose X strName = "Pose X"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nPoseX = jsValue[strName.c_str()].asInt(); } //12. Pose Y strName = "Pose Y"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nPoseY = jsValue[strName.c_str()].asInt(); } //13. image file strName = "image file"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isString()) { std::string imgFile = jsValue[strName.c_str()].asString(); if (0 != imgFile.compare("NULL")) { std::string fileName = strDir + "Image\\" + imgFile + ".jpg"; if (CFileRecipe::fileIsExist(fileName)) { HalconCpp::ReadImage(&m_hImage, fileName.c_str()); CreateMatchModel(); } } else { HalconCpp::GenEmptyObj(&m_hImage); CreateMatchModel(); } } } int CHoleDotProcess::Execute(DimensionDir eDir, Point2I offset) { /* code */ m_ptLocResult.x = 0.0f; m_ptLocResult.y = 0.0f; m_nNgInfo.clear(); HalconCpp::GenEmptyObj(&m_hImageResult); if (1 != m_nUse) return 0; if (ClsVision::TupleNil(m_idModel)) { CreateMatchModel(); } if (ClsVision::TupleNil(m_idModel)) return 0; //²éÕÒÄ¿±êλÖ㬽ØÈ¡·ÖÎöͼÏñ if (!FindTargetPos(eDir, offset)) return 0; FindDefectInfo(); int nRet = (int)(m_nNgInfo.size()); return nRet; } bool CHoleDotProcess::FindDefectInfo(void) { /* code */ CBlSideData* pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir); if (NULL == pSideData) return false; if (nullptr != pSideData) { DispVisionResult inf; inf.nType = 4; inf.eDir = (int)(m_eDir); inf.eVision = (int)(VISION_THRES); inf.strName = m_strName; inf.pointX = (int)(m_ptLocResult.x); inf.pointY = (int)(m_ptLocResult.y); pSideData->m_vDispVisionResult.push_back(inf); } int width = 0; int height = 0; ClsVision::GetImageSize(m_hImage, width, height); int x1 = (int)(m_ptLocResult.x - 0.5 * width); int y1 = (int)(m_ptLocResult.y - 0.5 * height); int x2 = x1 + width - 1; int y2 = y1 + height - 1; if (nullptr != pSideData) { DispVisionResult inf; inf.nType = 1; inf.eDir = (int)(m_eDir); inf.eVision = (int)(VISION_THRES); inf.strName = m_strName; inf.rectX1 = x1; inf.rectY1 = y1; inf.rectX2 = x2; inf.rectY2 = y2; pSideData->m_vDispVisionResult.push_back(inf); } if (!CVisionBufferPro::getImageROI(m_eDir, m_hImageResult, x1, y1, x2, y2)) return false; //²éÕÒȱÏÝÐÅÏ¢ HalconCpp::HObject hRoiRegion; if (0 == m_nShapeType) { return false; } else if (1 == m_nShapeType) { HalconCpp::GenRectangle1(&hRoiRegion, m_nSmoothSize, m_nSmoothSize, height - 1 - m_nSmoothSize, width - 1 - m_nSmoothSize); } else { HalconCpp::GenEllipse(&hRoiRegion, 0.5 * (height-1), 0.5 * (width-1), 0, 0.5 * width - m_nSmoothSize, 0.5 * height - m_nSmoothSize); } //HalconCpp::HWindow hDispWin(0, 0, width, height); //hDispWin.SetPart(0, 0, height - 1, width - 1); //hDispWin.DispObj(m_hImageResult); //hDispWin.SetDraw("margin"); //hDispWin.SetColored(6); //hDispWin.DispObj(hRoiRegion); //hDispWin.Click(); HalconCpp::HObject hProcImage; HalconCpp::ReduceDomain(m_hImageResult, hRoiRegion, &hProcImage); HalconCpp::HObject hRegion, hConnectedRegion, hSelectedRegion; if (1 == m_nBrightType) { HalconCpp::Threshold(hProcImage, &hRegion, 0, m_nThres); } else { HalconCpp::Threshold(hProcImage, &hRegion, m_nThres, 255); } HalconCpp::Connection(hRegion, &hConnectedRegion); HalconCpp::SelectShape(hConnectedRegion, &hSelectedRegion, "area", "and", 10, MAXINT); //hDispWin.SetDraw("fill"); //hDispWin.DispObj(hSelectedRegion); //hDispWin.Click(); HalconCpp::HTuple hvNum; HalconCpp::CountObj(hSelectedRegion, &hvNum); int nCount = hvNum[0].I(); if (nCount <1) return false; NgDotInfo ng; ng.eDir = m_eDir; ng.id = m_id; ng.isRes = false; ng.minValue = m_nDefectSize; ng.maxValue = m_nDefectSize; ng.strName = m_strName; ng.ngType = DefectLoc_HOLE; //ȱÏÝÀàÐÍ ng.eVision = VISION_HOLE; double xMmvsp = pSideData->m_dPixelSizeX; double yMmvsp = pSideData->m_dPixelSizeY; HalconCpp::HTuple hvArea, hvY, hvX; HalconCpp::HTuple hvY1, hvX1, hvY2, hvX2; HalconCpp::AreaCenter(hSelectedRegion, &hvArea, &hvY, &hvX); HalconCpp::SmallestRectangle1(hSelectedRegion, &hvY1, &hvX1, &hvY2, &hvX2); for (int i = 0; i < nCount; i++) { int wTmp = hvX2[i].I() - hvX1[i].I() + 1; int hTmp = hvY2[i].I() - hvY1[i].I() + 1; if (wTmp > m_nDefectSize || hTmp > m_nDefectSize) { ng.result = hvArea[i].I(); ng.xResult = (int)(xMmvsp * (hvX2[i].I() - hvX1[i].I() + 1)); ng.yResult = (int)(yMmvsp * (hvY2[i].I() - hvY1[i].I() + 1)); ng.xPosPxl = (int)(0.5 * (hvX2[i].I() + hvX1[i].I())) + x1; ng.yPosPxl = (int)(0.5 * (hvY1[i].I() + hvY2[i].I())) + y1; ng.x1 = hvX1[i].I() + x1 - 1; ng.y1 = hvY1[i].I() + y1 - 1; ng.x2 = hvX2[i].I() + x1 + 1; ng.y2 = hvY2[i].I() + y1 + 1; if (nullptr != pSideData) { //ÇøÓòµÄÏÔʾ½á¹û DispVisionResult inf; inf.nType = 1; inf.eDir = (int)(m_eDir); inf.strName = m_strName; 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); } } return true; } bool CHoleDotProcess::FindTargetPos(DimensionDir eDir, Point2I offset) { /* code */ if (ClsVision::TupleNil(m_idModel)) return false; //»ñȡָ¶¨·½ÏòµÄ²à±ßÊý¾Ý CBlSideData* pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir); //Èç¹û²à±ßÊý¾ÝΪ¿Õ£¬Ôò·µ»Øfalse if (nullptr == pSideData) return false; if (0 == m_nPoseType) { //Èç¹û²à±ßÊý¾ÝÖÐûÓÐÕÒµ½Éϵ㣬Ôò·µ»Øfalse if (!pSideData->m_bBotMark_Find) return false; //Èç¹û²à±ßÊý¾ÝÖÐûÓÐÕÒµ½Ïµ㣬Ôò·µ»Øfalse if (!pSideData->m_bTopMark_Find) return false; } else { //Èç¹û²à±ßÊý¾ÝÖÐûÓÐÕÒµ½Éϵ㣬Ôò·µ»Øfalse if (!pSideData->m_bTopPoint_Find) return false; //Èç¹û²à±ßÊý¾ÝÖÐûÓÐÕÒµ½Ïµ㣬Ôò·µ»Øfalse if (!pSideData->m_bBotPoint_Find) return false; } //1. Ê×ÏȼÆËãKeyµÄλÖà Point2I ptOffset; ptOffset.x = m_nPoseX; ptOffset.y = m_nPoseY; Point2I result; if (!CVisionBufferPro::TransformToReal(m_eDir, ptOffset, m_nPoseType, result)) return false; //2. ¼ÆËãImageµÄROI int width = 0; int height = 0; ClsVision::GetImageSize(m_hImage, width, height); int x1 = (int)(result.x - 0.5 * width - 200); int y1 = (int)(result.y - 0.5 * height - 200); int x2 = (int)(result.x + 0.5 * width + 200); int y2 = (int)(result.y + 0.5 * height + 200); HalconCpp::HObject hSearchImage; if (!CVisionBufferPro::getImageROI(m_eDir, hSearchImage, x1, y1, x2, y2)) return false; //3. ²éÕÒÄ£°å std::vector vResult; if (m_isScale) { HalconCpp::HObject hZoomImage; HalconCpp::ZoomImageFactor(hSearchImage, &hZoomImage, m_xScale, m_yScale, "constant"); ClsVision::FindNccModel(hZoomImage, m_idModel, m_score, 1, vResult); int sz = (int)(vResult.size()); for (int i = 0; i < sz; i++) { vResult[i].pt.x = vResult[i].pt.x / m_xScale; vResult[i].pt.y = vResult[i].pt.y / m_yScale; } } else { ClsVision::FindNccModel(hSearchImage, m_idModel, m_score, 1, vResult); } int nCount = (int)(vResult.size()); if (nCount < 1) return false; m_ptLocResult = vResult[0].pt; m_ptLocResult.x += x1; m_ptLocResult.y += y1; return true; } int CHoleDotProcess::CreateMatchModel(void) { /* code */ if (!ClsVision::TupleNil(m_idModel)) { ClsVision::ClearNccModel(m_idModel); m_idModel = HalconCpp::HTuple(); } if (!ClsVision::ObjectExist(m_hImage)) return -1; int width = 0; int height = 0; ClsVision::GetImageSize(m_hImage, width, height); if (width < 1 || height < 1) return -1; m_isScale = false; if (width > 120 || height > 120) { m_isScale = true; m_xScale = 1.0f; m_yScale = 1.0f; if (width > 120) { m_xScale = 120.0f / width; } if (height > 120) { m_yScale = 120.0f / height; } HalconCpp::HObject hZoomImage; HalconCpp::ZoomImageFactor(m_hImage, &hZoomImage, m_xScale, m_yScale, "constant"); m_idModel = ClsVision::CreateNccModel(hZoomImage, -15, 15, 0, 2000); } else { m_xScale = 1.0f; m_yScale = 1.0f; m_idModel = ClsVision::CreateNccModel(m_hImage, -15, 15, 0, 2000); } return 0; }