Halcon学习之2DMetrology

#include"halconcpp\HalconCpp.h"
#include<iostream>
using namespace HalconCpp;
using namespace std;

1.测量矩形

void test2DMetrologyRect()
{
    //创建一个需要用来测量几何图形的数据结构
    HMetrologyModel metrologyModel;
    metrologyModel.CreateMetrologyModel();

    HImage image("fabrik.png");
    Hlong width, height;
    image.GetImageSize(&width,&height);

    HWindow w(0, 0,width, height);
    w.DispImage(image);
    w.Click();
    //设置被测量图像的尺寸大小
    metrologyModel.SetMetrologyModelImageSize(width, height);

    //增加一个矩形到测量模型
    //参数说明: MetrologyHandle指测量模型的句柄, Row为矩形中心的y坐标值, Column为矩形中心的x坐标值, Phi为矩形的长半轴的方向,
    //Length1为矩形长半轴的长度, Length2为矩形短半轴的长度,MeasureLength1相对于长半轴垂直边界的检测区域一半长度, 
    //MeasureLength2相对于短半轴垂直边界的检测区域一半长度, MeasureSigma高斯平滑用到的参数, MeasureThreshold最小边界的振幅, 
    // GenParamName指定通用参数的名称, GenParamValue指定通用参数的值,Index指定创建测量对象的索引
    HTuple Row=270, Column=230, Phi=0, Length1=30, Length2=25, MeasureLength1=10, MeasureLength2=2;
    HTuple MeasureSigma = 1, MeasureThreshold = 30, GenParamName = HTuple(), GenParamValue = HTuple(), Index;
    Index = metrologyModel.AddMetrologyObjectRectangle2Measure(Row, Column, Phi, Length1, Length2, MeasureLength1, MeasureLength2, MeasureSigma, MeasureThreshold, GenParamName, GenParamValue);

    //对一个图像进行测量并且匹配指定的测量对象
    metrologyModel.ApplyMetrologyModel(image);

    //获得测量模型获得的测量结果
    //参数说明: MetrologyHandle测量模型的句柄, Index指定测量对象的下标, Instance指定返回的测量结果实例的类型,
    //GenParamName指定通用参数的名称, GenParamValue指定通用参数的值,Parameter返回的结果
    Hlong Index1=0, Instance1=0;
    HString GenParamName1="result_type",  GenParamValue1="all_param";
    HTuple  Parameter1;
    Parameter1 = metrologyModel.GetMetrologyObjectResult(Index1, Instance1, GenParamName1 , GenParamValue1);
    cout << "返回的测量参数一共个数: " << Parameter1.Length() << endl;
    cout << "矩形中心的y坐标值: " << (double)Parameter1[0] << " 矩形中心的x坐标值: " << (double)Parameter1[1] << endl;
    cout << "矩形的长半轴的方向角: " << (double)Parameter1[2] << " 矩形长半轴的长度: " << (double)Parameter1[3] << endl;
    cout << "矩形短半轴的长度: " << (double)Parameter1[4] << endl<<endl;

    //查询测量对象的结果轮廓
    //参数说明:Contour返回的结果轮廓对象, MetrologyHandle测量模型的句柄, Index指定测量对象的下标, Instance指定返回的测量结果实例的类型, Resolution指定相邻轮廓点的距离
    HTuple Index2=0, Instance2=0;
    double Resolution2=1.5;
    HXLDCont cont2 = metrologyModel.GetMetrologyObjectResultContour(Index2, Instance2, Resolution2);
    w.SetColor("red");
    w.DispXld(cont2);
    w.Click();

    //测量模型序列化与逆序列化
    HSerializedItem item = metrologyModel.SerializeMetrologyModel();
    HMetrologyModel serializeModel;
    serializeModel.CreateMetrologyModel();
    serializeModel.DeserializeMetrologyModel(item);

    //测量模型写入文件与从文件中读出
    metrologyModel.WriteMetrologyModel("metrologyModel.mtr");
    HMetrologyModel fileModel;
    fileModel.CreateMetrologyModel();
    fileModel.ReadMetrologyModel("metrologyModel.mtr");
}

2.测量圆或者圆弧

void test2DMetrologyCircle()
{
    //创建一个需要用来测量几何图形的数据结构
    HMetrologyModel metrologyModel;
    metrologyModel.CreateMetrologyModel();

    HImage image;
    image.ReadImage("rings_and_nuts.png");//载入一幅图片
    Hlong width, height;
    image.GetImageSize(&width, &height);
    HWindow w(0, 0, width, height);

    w.DispImage(image);
    w.Click();

    //设置被测量图像的尺寸大小
    metrologyModel.SetMetrologyModelImageSize(width, height);
    //增加一个圆或圆弧到测量模型
    //参数说明:Row指定圆心的y坐标, Column指定圆心的x坐标, Radius指定圆的半径, MeasureLength1指定垂直于边界的测量区域一半的宽度,
    // MeasureLength2指定垂直于边界的测量区域一半的宽度,MeasureSigma指定高斯平滑的参数值, MeasureThreshold指定最小边界的振幅值,
    //GenParamName指定通用参数的名称, GenParamValue指定通用参数的值, Index返回创建的测量对象在测量模型中的下标
    HTuple Index, Row=125, Column=130, Radius=35, MeasureLength1=10;
    HTuple MeasureLength2=2, MeasureSigma=1, MeasureThreshold=30, GenParamName="measure_distance", GenParamValue=40;
    Index = metrologyModel.AddMetrologyObjectCircleMeasure(Row, Column, Radius, MeasureLength1, MeasureLength2, MeasureSigma, MeasureThreshold, GenParamName, GenParamValue);

    //查询指定的模型轮廓的位置
    //参数说明:Contour为返回的轮廓, Index指定测量模型的下标, Resolution指定轮廓点之间的距离
    HTuple Index1=0;
    double Resolution1 = 1.5;
    HXLDCont cont1 = metrologyModel.GetMetrologyObjectModelContour(Index1, Resolution1);
    w.SetColor("yellow");
    w.DispXld(cont1);
    w.Click();

    //对一个图像进行测量并且匹配指定的测量对象
    metrologyModel.ApplyMetrologyModel(image);

    //获得测量对象的测量区域(对应xld轮廓中)和边界定位结果(对应存储的连续点中)
    //参数说明:Contours为返回的轮廓, Index指定测量模型的下标, Transition指定边界模式, Row返回测量后的边界对应的点的y值, Column返回测量后的边界对应的点的x值
    HXLDCont cont2;
    HString Index2 = "all", Transition2 = "all";
    HTuple Row2, Column2;
    cont2=metrologyModel.GetMetrologyObjectMeasures(Index2, Transition2, &Row2, &Column2);
    HRegion region = cont2.GenRegionContourXld("filled");//从xld轮廓转换到region类型
    w.SetColor("blue");
    w.DispRegion(region);
    w.Click();

    //显示测量后在图像上分布的点 
    w.SetColor("red");
    for (int i = 0; i < Row2.Length(); i++)
    {
        w.DispCircle((double)Row2[i], (double)Column2[i], 1);
    }
    w.Click();

}

你可能感兴趣的:(halcon,二维测量)