?
1 、 Halcon 的自我描述
Program?Logic
??Each?program?consists?of?a?sequence?of?HALCON?operators
??The?program?can?be?structured?into?procedures
??The?sequence?can?be?extended?by?using?control?operators?like?if,?for,?repeat,?or?while
??The?results?of?the?operators?are?passed?via?variables
??No?implicit?data?passing?is?applied
??Input?parameters?of?operators?can?be?variables?or?expressions
??Output?parameters?are?always?variables
??HDevelop?has?no?features?to?design?a?graphical?user?interface
??An?HDevelop?program?is?considered?as?a?prototypic?solution?of?the?vision?part?of?an?application
??HDevelop?is?typically?not?used?for?the?final?application
由此可以看出, Halcon 的定位是一個類庫,有著完整、快速實現函數,同時提供了 HDevelop作為快速開發的圖形化( IDE )界面;但是, Halcon 程序并不是一個完整的最終應用軟件,它沒有用戶界面,也不提供顯示的數據(公用的數據格式)。
Halcon 的初學者也應當從參考 Halcon 的程序入手,熟悉 Halcon 類庫,也即 HDevelop-Based?Programming;在此基礎上,進入 OR Class-Oriented?Programming。這也是 Halcon 推薦的開發方式:
The?vision?part?is?solved?with?HDevelop, and?t he?application?is?developed?with?C++?or?Visual?Basic。
?
2 、 HDevelop 界面的學習
通過閱讀 Halcon 的 PPT ,學到了下面一些有用的信息:
??文件——瀏覽示例,可以看到很多有用的例子;
??程序窗體中,可以瀏覽與編輯 Procedues (過程),這個其實就是自定義函數咯~還可以自己修改這些過程,并添加說明文檔;
??F4 ——將函數語句注釋掉; F3 ——激活;
??本地過程( Local?Procedue )與外部過程( Externel?Procedue )
?
3 、基本語法結構
Halcon 的語法結構
類似于 Pascal? 與? Visual?Basic ,大部分的語句是 Halcon 提供的算子,此外也包含了少部分的控制語句;
不允許單獨聲明變量;
提供自動的內存管理(初始化、析構及 OverWrite ),但句柄則需要顯示釋放;
C++ (算子模式)
通過代碼導出,以 C++ 為例,默認導出為算子型的語法結構,而非面向對象的;在此模式下,全部函數聲明為全局類型,數據類型只需要用 Hobject 、 HTuple 兩類類型進行聲明;
C++ (面向對象)
可以以面向對象的方式重寫代碼,也即利用類及類的成員函數;
在這種模式下,控制變量的類型仍未 HTuple ,而圖形數據可以由多種類型,如 HImage 等;
其他語言(略)
?
4 、 Halcon 數據結構
兩類參數:圖形參數Iconic?(image,?region,?XLD)與控制參數Control?(string,?integer,?real,?handle),在 Halcon 算子的參數中,依次為:輸入圖形參數、輸出圖形參數、輸入控制參數、輸出控制參數;并且其輸入參數不會被算子改變。
圖形參數Iconic:
Images
??Multiple?channels
??Arbitrary?region?of?interest
??Multiple?pixel?types(byte,?(u)int1/2/4,real,?complex,?direction,?cyclic,?vector_field)
byte,?uint2?// 灰度圖像的標準編碼
int1,?int2?//Difference?of?two?images?or?derivates?with?integer?precision(??)int4?// 兩幅灰度圖的頻譜
direction?// 圖片邊緣的梯度方向
real?// 邊緣提取及特定灰度值的輪廓
complex?// 圖片頻率分布
cyclic?//Assigning?one?"gray"?value?to?each?color(??)
vector_field?// 連續圖形的光學流分布
Regions
??Efficient?data?structure?(runlength?encoding)
??Extensive?set?of?operators
??Fastest?morphology?on?the?market
圖形編碼中,需要了解? row? 和? run? 兩個術語;也是 Halcon?Region 存儲的方式
Extended?Line?Description?(XLD)
??Subpixel?accurate?line?and?edge?detection
??Generic?point?list?based?data?structure
??Handling?of?contours,?polygons,?lines,?parallels,?etc.
此外, Halcon 支持的類型還包括圖形元組、控制變量元組及句柄:
元組的概念,使得可以用一個變量傳遞數個對象,可以由重載后的函數來進行處理;圖形元組的下標從 1 開始,控制變量元組下標從 0 開始;句柄則可以用來描述窗體、文件等等,句柄不能是常量。
?
5 、 Halcon 語言
輸入控制參數可以是表達式,但圖形參數、輸出參數均應為變量;
String 類型變量由單引號 ’括起來;此外還有一些特殊字符;
Boolean 型變量包括? true?(?=?1?) 、? false?(?=?0?)? ;不為零的整數將被認為 true ;但絕大多數的 Halcon 函數接受字符串型的表達: ’true’‘false’,而非邏輯型表達;
函數返回常量用于標識錯誤:
??H_MSG_TRUE?no?error?2
??H_MSG_FALSE?logical?false?3
??H_MSG_FAIL?operator?did?not?succeed??5
可以放在 try …catch…endtry 塊中,也可以用 dev_error_var() 與 ?dev_set_check()? 來捕獲;
控制語句結構:(與一般語言略有不同,它們也有輸入輸出變量)
??if??...??endif?/?if?...?else??...?endif?/?if?...?elseif?...?else?...?endif?
??for??...??endfor
??while??...??endwhile
??repeat?...?until
此外,也有關鍵字? break 、 continue 、 return 、 exit 、 stop? 用來控制語句的執行;
賦值語句在 Halcon 中也被當作函數來使用:
標準賦值
??assign(Expression,?ResultVariable)?// 編輯形式,永遠都是輸入在前,輸出在后
??ResultVariable?:=?Expression?// 代碼形式
元組插入賦值
??insert(Tuple,?NewValue,?Index,?Tuple)?// 編輯形式
??Tuple[Index]?:=?NewValue?// 代碼形式
控制變量元組操作
??[t,t]?concatenation?of?tuples
??|t|??number?of?elements
??t[i]??selection?of?an?element
??t[i:j]??selection?of?a?part?of?a?tuple
??subset(t1,t2)?selection?from?t1?by?indices?in?t2?
圖形元組操作對應函數
??[]??gen_empty_obj?()
??|t|??count_obj?(p,?num)
??[t1,t2]??concat_obj?(p1,?p2,?q)
??t[i]??select_obj?(p,?q,?i+1)
??t[i:j]??copy_obj?(p,?q,?i+1,?j-i+1)
??subset(t1,t2)?select_obj?(p,?q,?t2+1)
元組的數學運算,如: A?*?B ,令? m?=?|A|,?n?=?|B| ;
若 m 、 n 不相等,且都大于 1 ,則錯誤;否則返回三種情況:
??m=n=1 ,返回一個值;
??m=n>1 ,返回一個包含 m 個數的元組,值為兩元組各對于值的操作結果;
??m>1,n=1 ,返回一個包含 m 個數的元組,值為第二個數與第一元組各值的操作結果;
Halcon? 的數學運算
算術運算
??a?/?a?division
??a?%?a?rest?of?the?integer?division
??a?*?a?multiplication
??v?+?v?addition?and?concatenation?of?strings
??a?-?a?subtraction
??-a??negation
位運算
??lsh(i,i)??left?shift
??rsh(i,i)??right?shift
??i?band?i??bit-wise?and
??i?bor?i??bit-wise?or
??i?bxor?i??bit-wise?xor
??bnot?i??bit-wise?complement
字符串操作
??v$s??conversion?to?string?// 字符串的格式化,有很豐富的參數
??v?+?v??concatenation?of?strings?and?addition
??strchr(s,s)??search?character?in?string
??strstr(s,s)??search?substring
??strrchr(s,s)??search?character?in?string?(reverse)
??strrstr(s,s)??search?substring?(reverse)
??strlen(s)??length?of?string
??s{i}?selection?of?one?character
??s{i:i}??selection?of?substring
??split(s,s)??splitting?to?substrings
比較操作符
??t?<?t??less?than
??t?>?t??greater?than
??t?<=?t???less?or?equal
??t?>=?t??greater?or?equal
??t?=?t??equal
??t?#?t??not?equal
邏輯操作符
??not?l??negation
??l?and?l??logical?’and’
??l?or?l?logical?’or’
??l?xor?l??logical?’xor’
數學函數
??sin(a)??sine?of?a
??cos(a)??cosine?of?a
??tan(a)??tangent?of?a
??asin(a)??arc?sine?of?a?in?the?interval?[-p/2,?p/?2],?a???[-1,?1]
??acos(a)??arc?cosine?a?in?the?interval?[-p/2,?p/2],?a???[-1,?1]
??atan(a)??arc?tangent?a?in?the?interval?[-p/2,?p/2],?a???[-1,?1]
??atan2(a,b)??arc?tangent?a/b?in?the?interval?[-p,?p]
??sinh(a)??hyperbolic?sine?of?a
??cosh(a)??hyperbolic?cosine?of?a
??tanh(a)??hyperbolic?tangent?of?a
??exp(a)??exponential?function
??log(a)??natural?logarithm,?a>?0
??log10(a)??decade?logarithm,?a>?0
??pow(a1,a2)??power
??ldexp(a1,a2)??a1?pow(2,a2)
其他操作(統計、隨機數、符號函數等)
??min(t)??minimum?value?of?the?tuple
??max(t)??maximum?value?of?the?tuple
??min2(t1,t2)??element-wise?minimum?of?two?tuples?
??max2(t1,t2)??element-wise?maximum?of?two?tuples?
??find(t1,t2)?indices?of?all?occurrences?of?t1?within?t2?
??rand(i)?create?random?values?from?0..1?(number?specified?by?i)?
??sgn(a)?element-wise?sign?of?a?tuple?
??sum(t)??sum?of?all?elements?or?string?concatenation
??cumul(t)?cumulative?histogram?of?a?tuple
??mean(a)??mean?value
??deviation(a)??standard?deviation
??sqrt(a)??square?root?of?a
??deg(a)??convert?radians?to?degrees
??rad(a)??convert?degrees?to?radians
??real(a)??convert?integer?to?real
??int(a)?convert?a?real?to?integer
??round(a)??convert?real?to?integer
??number(v)??convert?string?to?a?number
??is_number(v)??test?if?value?is?a?number
??abs(a)??absolute?value?of?a?(integer?or?real)
??fabs(a)??absolute?value?of?a?(always?real)
??ceil(a)??smallest?integer?value?not?smaller?than?a
??floor(a)??largest?integer?value?not?greater?than?a
??fmod(a1,a2)?fractional?part?of?a1/a2,?with?the?same?sign?as?a1
??sort(t)??sorting?in?increasing?order
??uniq(t)?eliminate?duplicates?of?neighboring?values(typically?used?in?combination?with?sort)
??sort_index(t)??return?index?instead?of?values
??median(t)?Median?value?of?a?tuple?(numbers)
??select_rank(t,v)?Select?the?element?(number)?with?the?given?rank
??inverse(t)??reverse?the?order?of?the?values
??subset(t1,t2)?selection?from?t1?by?indices?in?t2
??remove(t1,t2)?Remove?of?values?with?the?given?indices
??environment(s)??value?of?an?environment?variable
??ord(a)??ASCII?number?of?a?character
??chr(a)??convert?an?ASCII?number?to?a?character
??ords(s)??ASCII?number?of?a?tuple?of?strings
??chrt(i)??convert?a?tuple?of?integers?into?a?string
?
6 、 Halcon 名稱解釋
??Operator:?A?procedure?of??the?HALCON?library?used?in?HDevelop?or?one?of?the?language?interfaces.
??Procedure?(of?HDevelop):?A?subroutine?defined?for?the?use?inside?HDevelop.
??Region:?Result?of?a?segmentation?like?threshold.?In?other?systems?called?blob,?area,?binary?image,?or?island.?Implemented?using?runlength?encoding.
??XLD:?Extended?Line?Description.?Universal?data?structure?used?to?handle?contour?based?data.?Mainly?used?in?the?context?of?subpixel?precise?measurement.
??Domain:?Part?of?the?image?which?is?used?for?processing.?In?other?systems?called?ROI?(region?of?interest).
??Channel:?One?image?matrix?of?a?multi-spectral?image.?One?example?is?the?red?channel?of?an?RGB?image.
??Iconic?data:?Overall?term?for?images,?regions,?and?XLD?data.?In?object?oriented?languages?(C++?and?COM)?and?in?HDevelop?iconic?data?is?represented?by?a?polymorphic?data?type.?In?object?oriented?languages?iconic?data?is?also?called?iconic?object.
??Control?data:?All?non?iconic?data.?Examples?are?single?values?(integer,?real,?and?string),?coordinates,?arrays?of?values.
??Tuple:?an?array?of?values?where?each?element?can?be?of?a?different?type.?One?can?have?both?iconic?and?control?tuples.
??HALCON?object:?Synonym?for?Iconic?object?/?data
??Image?acquisition?interface:?Interface?between?the?frame?grabber?/camera?driver?(SDK)?and?the?HALCON?library.?The?Image?acquisition?interface?is?a?DLL?which?is?dynamically?loaded?when?calling?open_framegrabber.
??Language?interface:?Software?that?enables?the?programmer?to?use?the?HALCON?library?in?a?given?language?(e.g.,?C++).
??Extension?Package:?A?mechanism?that?enables?the?user?to?fully?integrate?user-defined?procedures?into?the?HALCON?environment.?The?extension?package?concept?gives?full?access?to?the?internal?data?structures?of?HALCON.
??License?file:?File?“license.dat“?in?the?directory?“license“.??This?file?is?used?together?with?hardware?components?(dongle?or?Ethernet?card)?to?check?if?a?correct?license?is?available.
??Help?files:?Files?in?the?directory?“help“?which?are?used?to?get?online?information?about?all?HALCON?operators.?This?is?extensively?used?by?HDevelop.
??Shape-Based?Matching:?Finding?of?an?object?in?an?image?based?on?a?predefined?model.?The?shape?based?matching?uses?features?to?quickly?locate?objects?very?precisely.
??Variation?Model:?A?method?to?do?print?checking?by?presenting?multiple?good?patterns?to?the?system.?The?variation?model?learns?the?normal?variation?a?good?pattern?and?based?on?this?information?can?detect?real?defects.
??Measure?Tool:?A?set?of?operators?to?find?the?exact?location?of?edges?along?lines?or?circular?arcs.?Other?systems?call?the?similar?tool,?e.g.,?caliper.
??Accuracy:?The?deviation?from?the?true?value
??Precision:?The?standard?deviation?of?the?measurement
?
7 、 Halcon 函數
典型函數
??Filtering?(noise,?smoothing,?edge,?bit,?arithmetic,?enhancement)
??Segmentation?(thresholding,?topology,?region?growing,?classification,?comparison)
??Region?processing
??Morphology
??Feature?extraction
??Edge?detection
??Color?processing?and?classification
??OCR?/?OCV
??Bar?code?/?data?code
??Measurement
??Rectification
??Gray?value?matching
?
8 、 Halcon? HDevEngine
HDevEngine允許用戶在應用程序中直接調用 Halcon 程序( *.hdvp ),適用范圍包括 C++ 、 COM 、 .NET 語言。具體功能為:
??載入并執行 Halcon 程序( HDevelop?programs)
??載入、刪除、執行HDevelop過程(HDevelop?procedures )
??查詢以載入的HDevelop過程的接口
??將正確的參數值傳遞給HDevelop過程,執行并獲得結果
??重新實現了HDevelop的某些內部算子( operator ),例如 dev_display
??HDevEngine錯誤處理
在 C++ 中,使用 HDevEngine需要包括頭文件#include?"HDevEngineCpp.h",并包含附加的可執行文件hdevenginecpp.lib,具體見示例。
利用HDevEngine,可以很方便得實現多線程。
?
9 、 Halcon 數據結構( 2 )
Halcon 中, Image?=?Channel?+?Domain?,? 像素點存放在 Channel 矩陣中,根據 ROI 來描述 Image 。
Image 相關操作:
??輸入:從文件、從設備
??生成:外部圖像數據、空內存區域;
??顯示:disp_image() 圖像首通道灰度圖; disp_color()? 彩色圖; disp_channel() 某特定通道; disp_obj()? 自動判別類別;
??縮放: set_part()? 設置顯示區域; set_part_style()? 設置顯示參數;
??3D 顯示:(略)
Rules?to?Display?Gray?Images沒特別懂
邊界點的處理:鏡像、常數、延續、周期(略):
域的局限性:一些算子總是要處理周圍的矩形區域,比如 mean_image() ,并且總是先處理小的區域;
測量工具中的 ROIs 比較特殊,這種 ROIs 并不依附于 Image 上,而是在算子 gen_measure_*() 后產生,并且也只能是任意朝向的矩形、圓弧形區域;
處理多通道圖像:
??分割:Gray?operators僅適用第一通道,Color?operators:?使用前三個通道,Multi?channel?operator會使用全部通道;
// 對“ Gray?operators僅適用第一通道”的解釋:實際上,灰度圖,就是用第一通道( Red )像素點值所構建出來的那幅圖。
??過濾:所有通道被處理時均使用相同的參數,并且結果的通道數與處理的圖片相同;
??可以將域的處理結果,與原圖像結合在一起作為輸入圖像;
??通道處理:count_channels(),decompose*(),?compose*(),?access_channel(),?append_channel(),?channels_to_image(),?image_to_channels()
圖像金字塔中,第一個圖像為最大的圖像,各圖像有各自的區域
標準圖形( Shape ): circle 、 ellipse 、 rectangle1 、 rectangle2 、 line 、 polygon
特殊區域圖形: gen_grid_region():?grid 、 lines 、 points,?gen_checker_region()
圖像處理:
??修改:set_grayval()?:?Modify?one?or?more?pixels?of?the?input?image;paint_gray():?Paint?part?of?an?image?into?another?image;?overpaint_gray():?Paint?part?of?an?image?into?the?input?image;?paint_region():?Paint?the?region?with?a?constant?gray?value?into?an?image;?overpaint_region()?:?Paint?the?region?with?a?constant?gray?value?into?the?input?image
??復制:crop_part():?Crop?a?rectangle,?given?by?upper?left?corner?and?size;?crop_rectangle1():?Crop?a?rectangle,?given?by?upper?left?and?lower?right?corner;?crop_domain():?Crop?area?of?the?smallest?rectangle1?of?the?domain;?crop_domain_rel():?Like?crop_domain?but?with?the?possibility?to?change?the?size?of?bounding?box;?change_format():?Limit?the?size?of?the?image?at?the?lower?or?the?right?part;?get_grayval():?Access?one?or?multiple?pixel?values
??Tile:?tile_images(),?tile_images_offset(),?tile_channels()
?
10 、 Halcon 數據結構( 3 )
區域運算:
??并: union1() 、 union2() ;
??交: intersection();
??差: difference();
??補: complement() ;
圖形顯示參數設置:
??顯示模式: s et_draw();? 參數: margin 、 filled
??線寬線形: set_line_width();?set_line_style();
??顏色: set_color();?set_colored();?set_rgb();?set_gray();
??顯示圖形: set_shape();? 參數: original 、 outer?circle 、 inner?circle 、 rectangle1 、 rectangle2 、 ellipse 、 icon
??set_icon
?
11 、 Halcon 數據結構( 4 )
關于 XLD ,簡要寫一下:
圖像均用像素點保存,而像素點是整型的,不連續的; Halcon 做了拓展,定義了亞像素( subpixel )的對象: xld ;其實 xld 已經用過了,比如提取邊緣、構建輪廓等等, xld 在模板匹配、圖形校準等多方面有重要的用途。
?
12 、色彩? color
在視網膜底部,有三類感光細胞,它們分別探測不同頻率的光,產生 RGB 神經沖動,并把這些神經沖動傳遞下去;經過另外的細胞的處理,轉換成 1 個亮度通道、兩個色彩通道。
??The?RGB?stimulus?is?converted?to?a?luminance?and?2?chrominance?channels
所以, RGB 圖是原始的感光圖,而人眼的感覺,卻不是 RGB 三通道的疊加;更直觀地描述人的感覺,需要用到其他的色彩空間,比如 hsv 空間。
不同頻率的光,會產生不同的顏色;而光只有三種顏色,這是因為人眼只有三種光感受器。
所以有,任何光產生的顏色,都能夠由這三種純色來合成,這就是光的三元色。
?
?
對于相機來說,能夠檢測到的光譜范圍比人眼要寬泛,比如紅外相機等;為了獲得人眼類似的圖像,可以加上過濾裝置,濾去超出 400-700nm 范圍的光線。
?
13 、色彩空間及? Halcon 顏色處理
CCD 彩色相機有 R 、 G 、 B 三種感光芯片,捕捉不同顏色,然后轉換為 RGB 三通道。
顏色空間:
??RGB?Red 、 Green 、 Blue 三色通道,對光來說,猶如在黑暗中點亮各分色。
??CMY?Cyan 、 Magenta 、 Yellow? 三顏色通道,猶如在白紙上圖顏料
??YUV 、 YIQ ?Y 描述亮度、其余兩通道描述顏色(的差值),用于電視轉播
??ARgYb?與上類似, A 描述亮度,其余兩者描述顏色差值
??I1i2i3?與上類似, i1 描述亮度
??HSI?Hue 、 Saturation 、 Intensity? 分別描述顏色、飽和度、亮度
??HSV?與上類似,這里的 V 描述亮度,方法與上不同
??HSL?與 HSI 類似, L 描述亮度,但 Hue 與之不同
??HIS?與HIS類似
??Uniform?Color?Space 、 CIE?uv? 用二維圖描述色彩
??CIE?Lab? 高級色彩空間,較少使用
?
顏色空間的轉換,依靠 GPU 進行運算: trans_from_rgb() ,速度快
Scale_image()? 可以對單通道( RGB 、或 HSV 中的)進行重新渲染;
顏色的選取,通過對 Hue 通道進行 threshold()
2D?Histogram? 可用來描述兩通道( RGB 、 HSV 等中的)相應值對應關系,可用來選取顏色相近區域: histo_2dim()
N維像素分類: learn_ndim_norm() 、 learn_ndim_box()?
LUT : MLP 、 SVM 、 GMM
彩色過濾器:用于彩色圖像的分割等: edges_color() 、 edges_color_sub_pix() 、 lines_color()
?
14 、 Halcon? 窗體
Halcon 窗體的坐標系統: (Row,?Column)?(Width,?Height)
圖形:可以顯示灰度圖、彩色圖、 3D ;定義要顯示的區域,插值
區域:繪圖模式 (Fill 、 Margin) ,邊界、線寬,定義色彩模式,自動圖形轉換
繪圖:點、線、 xld 等
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
