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;
#endifvoid main()
{HTuple t;cout << t.Num() << '\n'; // The length of the tuple is 0t[0] = 0.815; // Assigning values to the tuplet[1] = 42;t[2] = "HAL";cout << t.Num() << '\n'; // The length of the tuple is 3cout << "HTuple = " << t << '\n'; // Using the << operator double d = t[0]; // Accessing the tuple, if theint 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 knownprintf("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 fileHWindow w; // Opening an appropriate windowimage.Display(w); // Display the imagew.SetLut("change2"); // Set a lookup tablew.Click(); // Waiting for a mouse clickw.SetLut("default"); // Set the default lookup tablew.SetPart(100, 100, 200, 200); // Set a part of the windowimage.Display(w);w.Click();// Adapting the part to the image againw.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.
