3.2 控制参数

2016-08-31  本文已影响0人  厦门听涛

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

控制参数的一个特殊形式是句柄,提供了途径去访问复杂的数据类型,像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也提供了与基本类型的相互转换和封装。

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

Tuples

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

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


数组算术运算

例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窗口的属性很容易改变。并且图像、区域、多边形等都可以显示在窗口上。下面列举常用的成员函数:


创建窗口:

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

例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.

上一篇下一篇

猜你喜欢

热点阅读