3.2 控制参数

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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,660评论 0 33
  • 文/梦鹭 第一章:结束,是时候结束了…… “木樰霜,我会一直一直爱着你,不放手。”一位俊美的美少年对我说。 “人...
    梦鹭阅读 232评论 0 0
  • 踩着小雨回家,到家洗樱桃,泡酒,做饭,吃饭,冲澡。。。 生活平淡又规律,心如止水。
    七月紫苏阅读 177评论 0 0
  • 今天早晨醒来,感觉很闷与有点累;就没有出去晨跑,这几天的天气真是太热啦。 回到工作室,开会足足三个多小时;总结与反...
    UsanaCrystal阅读 67评论 0 0
  • “飞行员特训营”结营了 三天时间说短不短,说长也不长 我用三天时间 记住了14位小朋友的名字 他们用三天时间,给我...
    D2董阅读 170评论 0 0