3.2 控制參數(shù)

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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,835評(píng)論 6 534
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,676評(píng)論 3 419
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,730評(píng)論 0 380
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,118評(píng)論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,873評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,266評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,330評(píng)論 3 443
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,482評(píng)論 0 289
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,036評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,846評(píng)論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,025評(píng)論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,575評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,279評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,684評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,953評(píng)論 1 289
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,751評(píng)論 3 394
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,016評(píng)論 2 375

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,762評(píng)論 0 33
  • 文/夢(mèng)鷺 第一章:結(jié)束,是時(shí)候結(jié)束了…… “木樰霜,我會(huì)一直一直愛著你,不放手。”一位俊美的美少年對(duì)我說。 “人...
    夢(mèng)鷺閱讀 252評(píng)論 0 0
  • 踩著小雨回家,到家洗櫻桃,泡酒,做飯,吃飯,沖澡。。。 生活平淡又規(guī)律,心如止水。
    七月紫蘇閱讀 195評(píng)論 0 0
  • 今天早晨醒來,感覺很悶與有點(diǎn)累;就沒有出去晨跑,這幾天的天氣真是太熱啦。 回到工作室,開會(huì)足足三個(gè)多小時(shí);總結(jié)與反...
    UsanaCrystal閱讀 78評(píng)論 0 0
  • “飛行員特訓(xùn)營(yíng)”結(jié)營(yíng)了 三天時(shí)間說短不短,說長(zhǎng)也不長(zhǎng) 我用三天時(shí)間 記住了14位小朋友的名字 他們用三天時(shí)間,給我...
    D2董閱讀 194評(píng)論 0 0