#include "stdafx.h" #include "NccDotProcess.h" #include "VisionRecipe.h" #include "VisionBufferPro.h" #include "FileRecipe.h" CNccDotProcess::CNccDotProcess(){ /* code */ m_id = 0; m_pos.x = 0; m_pos.y = 0; m_strName = _T("P0"); HalconCpp::GenEmptyObj(&m_hImage); m_idModel = -1; m_eDir = DIMENSION_NONE; m_score = 0.5f; m_ptResult.x = 0.0f; m_ptResult.y = 0.0f; m_xScale = 1.0f; m_yScale = 1.0f; m_isScale = false; m_idModel = HalconCpp::HTuple(); } CNccDotProcess::~CNccDotProcess(){ /* code */ if (!ClsVision::TupleNil(m_idModel)) { ClsVision::ClearNccModel(m_idModel); m_idModel = HalconCpp::HTuple(); } } CNccDotProcess::CNccDotProcess(DimensionDir eDir, int id) { /* code */ m_id = id; m_eDir = eDir; switch (eDir) { case GLOBAL_DEFINE::DIMENSION_A: m_strName.Format(_T("TopA%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B: m_strName.Format(_T("TopB%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C: m_strName.Format(_T("TopC%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D: m_strName.Format(_T("TopD%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_A_DN: m_strName.Format(_T("BotA%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B_DN: m_strName.Format(_T("BotB%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C_DN: m_strName.Format(_T("BotC%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D_DN: m_strName.Format(_T("BotD%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_NONE: m_strName.Format(_T("P%d"), m_id); break; default: m_strName.Format(_T("P%d"), m_id); break; } m_pos.x = 0; m_pos.y = 0; HalconCpp::GenEmptyObj(&m_hImage); m_idModel = HalconCpp::HTuple(); m_score = 0.5f; m_ptResult.x = 0.0f; m_ptResult.y = 0.0f; m_xScale = 1.0f; m_yScale = 1.0f; m_isScale = false; } int CNccDotProcess::execute(DimensionDir eDir, Point2I offset) { /* code */ m_ptResult.x = 0.0f; m_ptResult.y = 0.0f; if (ClsVision::TupleNil(m_idModel)) { CreateMatchModel(); } if (ClsVision::TupleNil(m_idModel)) return -1; findTargetPoint(eDir, offset); return 0; } void CNccDotProcess::findTargetPoint(DimensionDir eDir, Point2I offset) { /* code */ if (ClsVision::TupleNil(m_idModel)) return; int width = 0; int height = 0; ClsVision::GetImageSize(m_hImage, width, height); int x1 = (int)(m_pos.x - 0.5 * width - 200); int y1 = (int)(m_pos.y - 0.5 * height - 200); int x2 = (int)(m_pos.x + 0.5 * width + 200); int y2 = (int)(m_pos.y + 0.5 * height + 200); x1 += offset.x; y1 += offset.y; x2 += offset.x; y2 += offset.y; HalconCpp::HObject hSearchImage; if (!CVisionBufferPro::getImageROI(m_eDir, hSearchImage, x1, y1, x2, y2)) return; 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; m_ptResult = vResult[0].pt; m_ptResult.x += x1; m_ptResult.y += y1; CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir); if (nullptr != pSideData) { //ÇøÓòµÄÏÔʾ½á¹û DispVisionResult inf; inf.nType = 1; inf.eDir = (int)(m_eDir); inf.eVision = (int)(VISION_LOC); inf.rectX1 = (int)(m_ptResult.x - 0.5 * width - 1); inf.rectY1 = (int)(m_ptResult.y - 0.5 * height - 1); inf.rectX2 = (int)(m_ptResult.x + 0.5 * width + 1); inf.rectY2 = (int)(m_ptResult.y + 0.5 * height + 1); pSideData->m_vDispVisionResult.push_back(inf); } //if (nullptr != pSideData) { //Êä³öµãµÄÏÔʾ½á¹û // DispVisionResult inf; // inf.nType = 0; // inf.eDir = (int)(m_eDir); // inf.eVision = (int)(VISION_LOC); // inf.pointX = (int)(m_ptResult.x); // inf.pointY = (int)(m_ptResult.y); // pSideData->m_vDispVisionResult.push_back(inf); //} KeyDebugInfo inf = CVisionRecipe::getInstance()->getKeyInfo(); if (inf.isDebugTool && inf.isLocKey) { Log_GetDebug()->TraceInfo("Trace Ncc Result %d: %d, (%.0f,%.0f), %d", (int)m_eDir, m_id, m_ptResult.x, m_ptResult.y, __LINE__); } } void CNccDotProcess::CreateMatchModel(void) { /* code */ if (!ClsVision::TupleNil(m_idModel)) { ClsVision::ClearNccModel(m_idModel); m_idModel = HalconCpp::HTuple(); } if (!ClsVision::ObjectExist(m_hImage)) return; int width = 0; int height = 0; ClsVision::GetImageSize(m_hImage, width, height); if (width < 1 || height < 1) return; 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); } } void CNccDotProcess::setPose(Point2I pose) { /* code */ m_pos = pose; } Point2I CNccDotProcess::getPose(void) { /* code */ return m_pos; } void CNccDotProcess::setImage(HalconCpp::HObject &hImage) { /* code */ m_hImage = hImage; int width = 0; int height = 0; ClsVision::GetImageSize(hImage, width, height); if (width < 1 || height < 1) { m_pos.x = 0; m_pos.y = 0; if (!ClsVision::TupleNil(m_idModel)) { ClsVision::ClearNccModel(m_idModel); m_idModel = HalconCpp::HTuple(); } } else { CreateMatchModel(); } } HalconCpp::HObject& CNccDotProcess::getImage(void) { /* code */ return m_hImage; } int CNccDotProcess::getID(void) { /* code */ return m_id; } Json::Value CNccDotProcess::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_loc", m_eDir, m_id); } std::string strName = CFileRecipe::toString(m_strName); Json::Value jsValue; jsValue["alg type"] = LOC_VISION_TYPE; jsValue["side"] = (int)(m_eDir); jsValue["id"] = m_id; jsValue["name"] = strName; jsValue["score"] = m_score; jsValue["pos x"] = m_pos.x; jsValue["pos y"] = m_pos.y; 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 CNccDotProcess::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. score strName = "score"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isDouble()) { m_score = jsValue[strName.c_str()].asDouble(); } //5. pos x strName = "pos x"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_pos.x = jsValue[strName.c_str()].asInt(); } //6. pos y strName = "pos y"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_pos.y = jsValue[strName.c_str()].asInt(); } //7. 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(); } } }