Editing the Source Code (Part 2)
In the source file <Project name>Dlg.cpp:
-
Add the following lines after the #include section at the top of the file:
Copy#include <vision/PMRedLine/PMRedLineTool.hpp>
using namespace Cognex; -
Add the following to the OnBnClickedRunTool method:
Copy// Always use Object::Create<class>() instead of std::make_shared<class>()
//
// Static train rectangle, to persist between tool runs.
static Vision::RectanglePtr trainRect{};
if (!trainRect)
{
trainRect = Vision::Object::Create<Vision::Rectangle>();
trainRect->setInteractive(true);
trainRect->setGraphicDOFEnable(Vision::RectangleDOFConstants::All);
trainRect->SetXYWidthHeight(40, 85, 100, 100);
trainRect->setSelectedSpaceName("#");
trainRect->setTipText("Train Region");
}
// Static train origin coordinate axes, to persist between tool runs.
static Vision::CoordinateAxesPtr trainAxes{};
if (!trainAxes)
{
trainAxes = Vision::Object::Create<Vision::CoordinateAxes>();
trainAxes->setInteractive(true);
trainAxes->setGraphicDOFEnable(Vision::CoordinateAxesDOFConstants::All);
trainAxes->setSelectedSpaceName("#");
trainAxes->setTipText("Train Origin");
trainAxes->Transform() = Vision::Object::Create<Vision::Transform2DLinear>();
trainAxes->Transform()->setTranslationX(85);
trainAxes->Transform()->setTranslationY(135);
}
// Static top graphics record, to persist between tool runs.
static auto topRecord = Vision::Object::Create<Vision::Implementation::Record>();
// Load image -- used for both input and training.
auto imageFileTool = Vision::Object::Create<Vision::ImageFile::ImageFileTool>();
imageFileTool->Operator()->Open("images\\coins.idb", Vision::ImageFile::ImageFileModeConstants::Read;
imageFileTool->Run();
auto image = imageFileTool->OutputImage();
// Create PMRedLine tool.
auto redLineTool = Vision::Object::Create<Vision::PMRedLine::PMRedLineTool>();
redLineTool->SuspendChangedEvent();
// Set input image.
redLineTool->setInputImage(image);
// Configure training pattern.
redLineTool->Pattern()->setTrainImage(image);
redLineTool->Pattern()->setTrainRegion(trainRect);
redLineTool->Pattern()->setOrigin(trainAxes->Transform());
// Configure run parameters.
redLineTool->RunParams()->setNumberToFind(6);
redLineTool->RunParams()->ZoneScale()->setConfiguration(
Vision::PMRedLine::PMRedLineZoneConstants::LowHigh);
redLineTool->RunParams()->ZoneScale()->setHigh(1.1);
redLineTool->RunParams()->ZoneScale()->setLow(0.9);
// Configure last-run record.
auto lastRunRecordEnable = (int)Vision::PMRedLine::PMRedLineLastRunRecordConstants::All;
lastRunRecordEnable &= ~(int)Vision::PMRedLine::PMRedLineLastRunRecordConstants::ResultsCoordinateAxes;
redLineTool->setLastRunRecordEnable(
(Vision::PMRedLine::PMRedLineLastRunRecordConstants)lastRunRecordEnable);
// Train and run PMRedLine tool.
redLineTool->Pattern()->Train();
redLineTool->Run();
redLineTool->ResumeAndRaiseChangedEvent();
topRecord->SubRecords()->Clear();
topRecord->SubRecords()->Add(redLineTool->CreateCurrentRecord());
topRecord->SubRecords()->Add(redLineTool->CreateLastRunRecord());
m_display.setAutoFitWithGraphics(true);
m_display.setSubject(topRecord); -
Add the following line to the OnSize method:
Copym_display.OnSize(nType, cx, cy);