美文网首页
3.2 控制参数

3.2 控制参数

作者: 厦门听涛 | 来源:发表于2016-08-31 22:54 被阅读0次

    HALCON/C++可以处理各种不同类型的字母数字混合的控制参数,如下:

    • 离散数字(long)
    • 浮点数字(double)
    • 字符串(char*)

    控制参数的一个特殊形式是句柄,提供了途径去访问复杂的数据类型,像windows,图像获取设备,用于形状匹配的模型。实际上,在内部,句柄总是以离散数字(long)表示。

    HALCON/C++使用tuple表示控制参数的容器类。另外,tuple是多态的,可以包含各种类型的参数。为了实现这个目的,HCtrlVal被介绍,请看下一节。

    The Basic Class for Control Parameters

    HCtrlVal是类HTuple的基类,并且一般对于用户隐藏。因为它仅仅用于临时的类型转换。核心点时它包含三种基本的控制参数类型,即离散数字(long),浮点类型(double),字符串类型(char*)。HCtrlVal提供了以下成员函数:

    typedef long long Hlong;
    
    HCtrlVal(void)
    Default constructor. 
    
    HCtrlVal(Hlong l)
    Constructing a value from long.
    
    HCtrlVal(int l)
    Constructing a value from int.
    
    HCtrlVal(double d)
    Constructing a value from double. 
    
    HCtrlVal(const char *s)
    Constructing a value from char *. 
    
    HCtrlVal(const HCtrlVal &v)
    Copy constructor. 
    
    ~HCtrlVal(void)
    Destructor. 
    
    HCtrlVal& operator = (const HCtrlVal &v)
    Assignment operator. 
    
    int ValType() const
    Type of a value (O: Hlong, int; 1: float, double; 2: string). 
    见:
    enum HCtrlType {
      LongVal   = LONG_PAR, 
      DoubleVal = DOUBLE_PAR,
      StringVal = STRING_PAR,
      UndefVal  = UNDEF_PAR
    };
      
    operator int(void) const
    Conversion to int. 
    
    operator Hlong(void) const
    Conversion to long. 
    
    operator double(void) const
    Conversion to double. 
    
    operator const char*(void) const
    Conversion to char *. 
    
    double D() const
    Accessing a value and conversion to double. 
    
    Hlong L() const
    Accessing a value and conversion to Hlong. 
    
    int I() const
    Accessing a value and conversion to int. 
    
    const char *S() const
    Accessing a value and conversion to char *. 
    
    HCtrlVal operator + (const HCtrlVal &val) const
    Adding two values. 
    
    HCtrlVal operator - (const HCtrlVal &val) const
    Subtracting two values. 
    
    HCtrlVal operator * (const HCtrlVal &val) const
    Multiplying two values. 
    
    HCtrlVal operator / (const HCtrlVal &val) const
    Division of two values. 
    

    这里面和我们前面介绍的HPixVal与int等各类型的转换相似,HCtrlVal也提供了与基本类型的相互转换和封装。

    另外有几个转换函数比较重要:

    • double D() const
      Accessing a value and conversion to double.

    • long L() const
      Accessing a value and conversion to long.

    • int I() const
      Accessing a value and conversion to int.

    • const char *S() const
      Accessing a value and conversion to char *.

    Tuples

    HTuple建立在HCtrlVal的基础上。它实现了动态长度的HCtrlVal对象的数组。默认的构造函数定义了一个空的数组(Num()==0)。并且数组可以通过赋值动态地扩展。内存管理,如重新分配、释放,也由类自身管理。访问数组的序号是0到Num()-1

    下面介绍几个重要的成员函数,更详细地请访问:%HALCONROOT%\include\cpp。

    • HTuple(int length, const HTuple &value)
      构造指定长度的常数组,同 tuple_gen_const.
    • HCtrlVal &operator [] (int i)
      设置第i个元素
    • HCtrlVal operator [] (int i) const
      读取第i个元素

    数组算术运算

    • HTuple operator + (const HTuple &val) const
      Adding two tuples element by element, similar to the operator tuple_add. The arrays have to be of the same size.

    • HTuple operator + (double &val) const
      HTuple operator + (int &val) const
      Adding a number to each element of the tuple, similar to the operator tuple_add.

    • HTuple operator - (const HTuple &val) const
      Subtracting two tuples element by element, similar to the operator tuple_sub. The arrays have to be of the same size.

    • HTuple operator - (double &val) const
      HTuple operator - (int &val) const
      Subtracting a number from each element of the tuple, similar to the operator tuple_sub.

    • HTuple operator * (const HTuple &val) const
      Multiplying two tuples element by element, similar to the operator tuple_mult. The arrays have to be of the same size.

    • HTuple operator * (double &val) const
      HTuple operator * (int &val) const
      Multiplying a number with each element of the tuple, similar to the operator tuple_mult.

    • HTuple operator / (const HTuple &val) const
      Division of two tuples element by element, similar to the operator tuple_div. The arrays have to be of the same size.

    • HTuple operator / (double &val) const
      HTuple operator / (int &val) const
      Division of each element of the tuple by a number, similar to the operator tuple_div.

    例1

    #include "HalconCpp.h"
    using namespace Halcon;
    #include "HIOStream.h"
    #if !defined(USE_IOSTREAM_H)
    using namespace std;
    #endif
    
    void main()
    {
        HTuple  t;
        cout << t.Num() << '\n';             // The length of the tuple is 0
        t[0] = 0.815;                        // Assigning values to the tuple
        t[1] = 42;
        t[2] = "HAL";
        cout << t.Num() << '\n';             // The length of the tuple is 3
        cout << "HTuple = " << t << '\n';    // Using the << operator 
        double d = t[0];                     // Accessing the tuple, if the
        int   l = t[1];                     // the types of the elements
        //Hlong l=t[1];
        const char  *s = t[2];               // are known
        // Accessing the tuple, if the types of the elements are known
        printf("Values: %g %ld %s\n", t[0].D(), t[1].L(), t[2].S());
    }
    

    句柄封装类

    最突出的类是HWindow.自从Halcon 6.1开始,HALCON/C++也提供了访问文件或者功能的句柄类,如图像获取装置,测量,或者基于形状的匹配。

    Windows

    HWindow以很方便的方式提供了Halcon窗口,halcon窗口的属性很容易改变。并且图像、区域、多边形等都可以显示在窗口上。下面列举常用的成员函数:


    创建窗口:

    • HWindow(int Row=0, int Column=0,
      int Width=-1, int Height=-1,
      int Father = 0, const char *Mode = "",
      const char *Host = "")
      Default constructor. The constructed window is opened.

    • ~HWindow(void)
      Destructor. This closes the window.

    • void Click(void) const
      等待用户在窗口点击鼠标

    • HDPoint2D GetMbutton(int *button) const
      HDPoint2D GetMbutton(void) const

    获取鼠标点击时的坐标,和鼠标的类型。见 get_mbutton.
    鼠标类型:
    1:
    Left button,
    2:
    Middle button,
    4:
    Right button.

    • HDPoint2D GetMposition(int *button) const
      HDPoint2D GetMposition(void) const
      获取鼠标的位置和鼠标的点击类型,不要求鼠标一定要点击。见 get_mposition.

    • HCircle DrawCircle(void) const
      Waiting for the user to draw a circle in the window, see the reference manual entry of draw_circle.

    • HEllipse DrawEllipse(void) const
      Waiting for the user to draw an ellipse in the window, see the reference manual entry of draw_ellipse.

    • HRectangle1 DrawRectangle1(void) const
      Waiting for the user to draw a rectangle parallel to the coordinate axis in the window, see the reference manual entry of draw_rectangle1.

    • HRectangle2 DrawRectangle2(void) const
      Waiting for the user to draw a rectangle with an arbitrary orientation and size in the window, see the reference manual entry of draw_rectangle2.

    例2

    #include "HalconCpp.h"
    using namespace Halcon;
    
    void main()
    {
        HImage  image("E:\\halcon\\images\\control_unit.png");     // Reading an image from a file
        HWindow w;                         // Opening an appropriate window
        image.Display(w);                  // Display the image
        w.SetLut("change2");               // Set a lookup table
        w.Click();                         // Waiting for a mouse click
        w.SetLut("default");               // Set the default lookup table
        w.SetPart(100, 100, 200, 200);        // Set a part of the window
        image.Display(w);
        w.Click();
        // Adapting the part to the image again
        w.SetPart(0, 0, image.Height() - 1, image.Width() - 1);
        image.Display(w);
        HRegionArray regs = image.Regiongrowing(1, 1, 4, 100);
        w.SetDraw("margin");
        w.SetColored(6);
        regs.Display(w);
        w.Click();
        image.Display(w);
        w.SetShape("rectangle1");
        regs.Display(w);
    }
    

    窗口在从文件中读取图像后打开,这意味着窗口被缩放到图像的大小。
    The lookup table is changed afterwards, and the program waits for a mouse click in the window. A part of the image is zoomed now, and the program waits again for a mouse click in the window. By applying a region growing algorithm from the HALCON library (Regiongrowing) regions are generated and displayed in the window. Only the margin of the regions is displayed. It is displayed in 6 different colors in the window. The example ends with another way of displaying the shape of regions. The smallest rectangle parallel to the coordinate axes surrounding each region is displayed.

    相关文章

      网友评论

          本文标题:3.2 控制参数

          本文链接:https://www.haomeiwen.com/subject/ccdcettx.html