HALCON
1. 語法范式 Syntax Style
1.1. 基本格式
1.1.1. 算子格式
算子(輸入圖像參數:輸出圖像參數:輸入控制參數:輸出控制參數)
其中四個參數任意一個可以為空
e.g.1.threshold(Image : Region : MinGray, MaxGray : )
** threshold算子,1 Image Para input : Image ; 2 Image Para output :Region ;
**3 Control Para input : MinGray , MaxGray 4 Control Para output :無
2.get_image_pointer1(Image : : : Pointer, Type, Width, Height)
** threshold算子,1 Image Para input : Image ; 2 Image Para output :無;
** 3 Control Para input : 無 4 Control Para output : Pointer, Type, Width, Height
1.1.2. 程序結構
HDEV即HALCON Develop,相當於VC中sln,solution。dev下面有很多窗口。
Switches the update of the PC during program execution on or off.
Switch time measurement for operators on or off.
Switches the update of the variable window during program execution on or off.
Switches the automatic output of iconic output objects into the graphics window during program execution on or off.
1.1.3. 符號
這里主要列出一些和C/C++含義不同的符號,以及HALCON中一些重要符號。
編號 |
符號 |
符號含義 |
1 |
* |
注釋符號 |
2 |
:= |
賦值符號,和C中不同 |
3 |
= |
邏輯符號,判斷相等,相對於C中的’==’ |
4 |
\ |
轉義符號,至少可以在用於換行的轉義,’\+Enter’就可以續行了 |
5 |
$ |
變量指示符號,e.g. +Distance$'.3'表示將變量Distance寫成3個字長的字符串形式 |
6 |
F1 |
Help文檔,直接看算子 |
7 |
F2 |
回滾到程序頭 |
8 |
F3/F4 |
激活/注銷此行代碼 |
9 |
F5 |
運行程序 |
10 |
F6 |
單步執行 |
11 |
F10 |
設置斷點 |
1.2. 讀寫文件I/O
1.2.1. 控制變量的I/O
HACLON的輸入,輸出寫法相當靈活。下面三個列子分別是常量字符串,格式化的浮點數,和一個tuple的輸出
*字符串和單個浮點數的輸出,甚至不需要fnew_line來寫下一行,直接用\n,在一般的文本編輯器里就能有比較好的格式。 注意不用寫百分號'%d''d',' 3d'表示始終為3位輸出,'10.3f'表示始終為10位輸出,其中小數位占3位
fwrite_string(FileHandle,'\tR1\tx1\ty1\tR2\tx2\ty2\td1\td2\td3\n')
fwrite_string(FileHandle, '\n'+i$'d'+' '+R1$'10.3f'+x1$'10.3f'+y1$'10.3f'+\
R2$'10.3f'+x2$'10.3f'+y2$'10.3f'+\
d1$'10.3f'+d2$'10.3f'+d3$'10.3f')
*在同一行中輸出按'7.3f'的格式輸出UpRows的所有元素,元素之間有個空格
fnew_line(FileHandle)
fwrite_string(FileHandle,UpRows$'7.3f'+' ') /
1.2.2. 圖像變量的I/O
Path:= 'I:/Work/ritz/Piece/data/data_0528/'
read_image (Image, Path+'hgtest1__'+i$'d')
dump_window (WindowHandle, 'png', 'C:/Documents and Settings/Administrator/桌面/sadaharu2_gray.png')
write_image (Amp, 'png', 0 , 'C:/Documents and Settings/Administrator/桌面/sadaharu2_canny2.png')
write_region(Margin,'C:/Documents and Settings/Administrator/桌面/sadaharu2_canny3.tif')
1.3. 圖像或形狀的生成與顯示
這條主要是為了區分三個系列的函數,gen_ , draw_ 和 disp_
l gen_ gen系列跨度很大,一般屬於Creation范疇,生成的可以是圖像image,匹配模板model,區域region,輪廓Contour,亞像素級輪廓Contour XLD,濾波器filter,HALCON內部描述文件descr(如標定板的表述caltab10mm.descr)等。下面展示的是一些容易混淆的gen_circle等,都是生成區域,屬於Table of Contents/Regions/Creation:
Create a circle.
Create an ellipse.
Create a rectangle of any orientation.
以畫圓為例,含有Row,Col,R,且這些都是輸入量:
gen_circle( : Circle : Row, Column, Radius : )
l draw_ 屬於Table of Contents/Graphics/Drawing和我們平常理解的不同,這里的draw是指交互式地在窗口上畫圖,而不是直接按參數畫圖
Interactive drawing of a circle.
Interactive drawing of an ellipse.
Draw a line.
以畫圓為例,雖然也有Row,Col,R,但這些都是輸出量:
draw_circle( : : WindowHandle : Row, Column, Radius)
l disp_ 屬於Table of Contents/Graphics/Output
Displays circular arcs in a window.輸出顯示一段弧
Displays arrows in a window.輸出顯示一個箭頭
Displays circles in a window.
Displays crosses in a window.
以畫弧為例,雖然也有CRow,CCol,而且這些都是輸入的控制參數:
disp_arc( : : WindowHandle, CenterRow, CenterCol, Angle, BeginRow, BeginCol : )
2. 數據結構 Data Structure
2.1. Tuple元組
HALCON中的元組是一個涵蓋范圍很廣的數據結構,可以表示各種整形浮點,也可以表示字符串。
2.1.1. tuple的函數
① Tuple的長度
tuple_length( : : Tuple : Length)
e.g.tuple_length( RowsEdges, LengthEdge),還有一種更簡單的方法,
② Tuple按index取元素
tuple_select( : : Tuple, Index : Selected)
e.g.tuple_select( RowsEdges, 0,RowSelectedPoint)
tuple_select_range( : : Tuple, Leftindex, Rightindex : Selected)
Select several elements of a tuple between the left index and the right index.
③ Tuple的初始化,包含分配內存需要的長度以及初始值
gen_tuple_const(NumHoles,0)
2.1.2. tuple的操作符
tuple的操作除了調用函數,還可以直接訪問元素,或者使用操作符
① Tuple的長度
e.g.LengthEdge1 := |RowsEdges|
② Tuple按index取元素
e.g.RowSelectedPoint := RowsEdges[0]
2.2. 常見圖像變量數據結構及相互轉換:
Image,Region, Contour ,XLD,幾何圖形
2.2.1. Image
Region
① Image Region
threshold(Image : Region : MinGray, MaxGray : )
Bin_threshold,char_threshold,dual_threshold,dyn_threshold,fast_threshold,hysteresis_threshold,threshold_sub_pix
var_threshold Threshold an image by local mean and standard deviation analysis
watersheds_threshold— Extract watershed basins from an image using a threshold.
② Image Region
reduce_domain(Image, Region : ImageReduced : : )
Reduce the domain of an image.提取ROI中的圖像部分
region_to_bin(Region : BinImage : ForegroundGray, BackgroundGray, Width, Height : )
Convert a region into a binary byte-image。將區域轉化為二值圖
region_to_label(Region : ImageLabel : Type, Width, Height : ) Convert regions to a label image.
region_to_mean(Regions, Image : ImageMean : : )
Paint regions with their average gray value. 結合原圖Image將區域內所有像素點轉換為它的中值。
2.2.2. Region
Contour
① Region Contour
Create XLD contours corresponding to circles or circular arcs.
Transforms a NURBS curve into an XLD contour.
gen_contour_polygon_rounded_xld
Generate a XLD contour with rounded corners from a polygon (given as tuples).
Generate an XLD contour from a polygon (given as tuples).
Generate XLD contours from regions.
Convert a skeleton into XLD contours.
Generate one XLD contour in the shape of a cross for each input point.
Creation of an XLD contour corresponding to an elliptic arc.
Create control data of a NURBS curve that interpolates given points.
Extract parallel XLD polygons.
Approximate XLD contours by polygons.
Create an XLD contour in the shape of a rectangle.
Extract parallel XLD polygons enclosing a homogeneous area.
3. 函數理解 Understanding the Basic but Essential Functions
3.1. edges_sub_pix
edges_sub_pix (Image, Edges, 'canny', 0.9, 20, 40)
edges_sub_pix(Image : Edges : Filter, Alpha, Low, High : )
主要介紹Canny邊緣檢測,同hysteresis_threshold,Canny邊緣檢測時不是單個閾值,而是有個“遲滯效應”設置了雙閾值。
hysteresis_threshold performs a hysteresis threshold operation (introduced by Canny) on an image. All points in the input image Image having a gray value larger than or equal to High are immediately accepted (“secure” points). Conversely, all points with gray values less than Low are immediately rejected. “Potential” points with gray values between both thresholds are accepted if they are connected to “secure” points by a path of “potential” points having a length of at most MaxLength points. This means that “secure” points influence their surroundings (hysteresis).
4. 經驗談 Special Tips
4.1. 多使用help 文檔中的Content Table:有問題何不往上翻一級
“不是路上真面目,只緣身在此山中”。解決一個問題,就和了解一個山峰所有真正的登山路一樣,當我們在山腰中時,自然是很難了解這條路是否能直通山頂的,而當我們處在一個比這座峰更高的一個位置時,很自然就可以觀察或者自己構造出一條比較好的登山路。這就是看往上一級,才看Content Table的作用,在這樣一個更高的視野我們就可以找到在這個范圍內的其他的路,也許剛好可以登上這座峰。如:
Table of Contents/Filter/smooth_image,
eliminate_min_max : Smooth an image in the spatial domain to suppress noise.