Rectangle Fitting¶
This code snippet shows how to create a rectangle measurement tool, adjust the transition parameters, set the nominal gauge position, perform the measurement and retrieve the result.
C++¶
#include <eVision/EasyGauge/ERectangleGauge.h>
using namespace eVision;
// Image constructor
EImageBW8 srcImage;
// ERectangleGauge constructor
ERectangleGauge rectangleGauge;
// Adjust the transition parameters
rectangleGauge.SetTransitionType(ETransitionType_Bw);
rectangleGauge.SetTransitionChoice(ETransitionChoice_LargestAmplitude);
// Set the rectangle fitting gauge position,
// size (50x30 units) and orientation (15)
EPoint center(256.f, 256.f);
ERectangle rectangle(center, 50.f, 30.f, Easy::FromDegrees(15.f));
rectangleGauge.SetRectangle(rectangle);
try
{
// Measure
ERectangle measured = rectangleGauge.Measure(srcImage);
// Get the size and the rotation angle of the fitted rectangle
float sizeX = measured.GetSizeX();
float sizeY = measured.GetSizeY();
float angle = measured.GetAngle();
}
catch (const EException& e)
{
// The rectangle was not found.
}
// Save the point gauge measurement context
rectangleGauge.Save("myRectangleGauge.gge");
C#¶
using eVision;
// Image constructor
EImageBW8 srcImage = new EImageBW8();
// ERectangleGauge constructor
ERectangleGauge rectangleGauge = new ERectangleGauge();
// Adjust the transition parameters
rectangleGauge.TransitionType = ETransitionType.Bw;
rectangleGauge.TransitionChoice = ETransitionChoice.LargestAmplitude;
// Set the rectangle fitting gauge position,
// size (50x30 units) and orientation (15°)
rectangleGauge.Rectangle = new ERectangle(new EPoint(256.0f, 256.0f), 50.0f, 30.0f, Easy.FromDegrees(15.0f));
try
{
// Measure
ERectangle measured = rectangleGauge.Measure(srcImage);
// Get the size and the rotation angle of the fitted rectangle
float sizeX = measured.SizeX;
float sizeY = measured.SizeY;
float angle = measured.Angle;
}
catch (EException e)
{
// The rectangle was not found
}
// Save the rectangle gauge measurement context
rectangleGauge.Save("myRectangleGauge.gge");
Python¶
import eVision
# Image constructor
src_image = eVision.EImageBW8()
# ERectangleGauge constructor
rectangle_gauge = eVision.ERectangleGauge()
# Adjust the transition parameters
rectangle_gauge.TransitionType = eVision.ETransitionType.Bw
rectangle_gauge.TransitionChoice = eVision.ETransitionChoice.LargestAmplitude
# Set the rectangle fitting gauge position,
# size (50x30 units) and orientation (15°)
rectangle_gauge.Rectangle = eVision.ERectangle(eVision.EPoint(256.0, 256.0), 50.0, 30.0, eVision.Easy.FromDegrees(15.0))
try:
# Measure
measured = rectangle_gauge.Measure(src_image)
# Get the size and the rotation angle of the fitted rectangle
sizeX = measured.SizeX
sizeY = measured.SizeY
angle = measured.Angle
except eVision.EException:
# The rectangle was not found.
pass
# Save the rectangle gauge measurement context
rectangle_gauge.Save("myRectangleGauge.gge")