HALCON/C++可以處理各種不同類型的字母數(shù)字混合的控制參數(shù),如下:
- 離散數(shù)字(long)
- 浮點(diǎn)數(shù)字(double)
- 字符串(char*)
控制參數(shù)的一個(gè)特殊形式是句柄,提供了途徑去訪問復(fù)雜的數(shù)據(jù)類型,像windows,圖像獲取設(shè)備,用于形狀匹配的模型。實(shí)際上,在內(nèi)部,句柄總是以離散數(shù)字(long)表示。
HALCON/C++使用tuple表示控制參數(shù)的容器類。另外,tuple是多態(tài)的,可以包含各種類型的參數(shù)。為了實(shí)現(xiàn)這個(gè)目的,HCtrlVal被介紹,請(qǐng)看下一節(jié)。
The Basic Class for Control Parameters
HCtrlVal是類HTuple的基類,并且一般對(duì)于用戶隱藏。因?yàn)樗鼉H僅用于臨時(shí)的類型轉(zhuǎn)換。核心點(diǎn)時(shí)它包含三種基本的控制參數(shù)類型,即離散數(shù)字(long),浮點(diǎn)類型(double),字符串類型(char*)。HCtrlVal提供了以下成員函數(shù):
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等各類型的轉(zhuǎn)換相似,HCtrlVal也提供了與基本類型的相互轉(zhuǎn)換和封裝。
另外有幾個(gè)轉(zhuǎn)換函數(shù)比較重要:
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的基礎(chǔ)上。它實(shí)現(xiàn)了動(dòng)態(tài)長(zhǎng)度的HCtrlVal對(duì)象的數(shù)組。默認(rèn)的構(gòu)造函數(shù)定義了一個(gè)空的數(shù)組(Num()==0)。并且數(shù)組可以通過賦值動(dòng)態(tài)地?cái)U(kuò)展。內(nèi)存管理,如重新分配、釋放,也由類自身管理。訪問數(shù)組的序號(hào)是0到Num()-1
下面介紹幾個(gè)重要的成員函數(shù),更詳細(xì)地請(qǐng)?jiān)L問:%HALCONROOT%\include\cpp。
- HTuple(int length, const HTuple &value)
構(gòu)造指定長(zhǎng)度的常數(shù)組,同 tuple_gen_const. - HCtrlVal &operator [] (int i)
設(shè)置第i個(gè)元素 - HCtrlVal operator [] (int i) const
讀取第i個(gè)元素
數(shù)組算術(shù)運(yùn)算
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++也提供了訪問文件或者功能的句柄類,如圖像獲取裝置,測(cè)量,或者基于形狀的匹配。
Windows
HWindow以很方便的方式提供了Halcon窗口,halcon窗口的屬性很容易改變。并且圖像、區(qū)域、多邊形等都可以顯示在窗口上。下面列舉常用的成員函數(shù):
創(chuàng)建窗口:
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
等待用戶在窗口點(diǎn)擊鼠標(biāo)HDPoint2D GetMbutton(int *button) const
HDPoint2D GetMbutton(void) const
獲取鼠標(biāo)點(diǎn)擊時(shí)的坐標(biāo),和鼠標(biāo)的類型。見 get_mbutton.
鼠標(biāo)類型:
1:
Left button,
2:
Middle button,
4:
Right button.
HDPoint2D GetMposition(int *button) const
HDPoint2D GetMposition(void) const
獲取鼠標(biāo)的位置和鼠標(biāo)的點(diǎn)擊類型,不要求鼠標(biāo)一定要點(diǎn)擊。見 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.