圖像預處理
消除噪聲 | mean_image/binomial_filter |
抑制小斑點或細線 | median_image |
平滑圖像 | smooth_image |
平滑圖像的同時保存邊緣 | anisotropic_diffusion |
遍歷文件夾與文件選擇
1)遍歷文件夾:
list_files( : : Directory, Options : Files)
Directory:目錄(文件夾路徑)
Options:選項
'files' | 指定搜索的格式為文件 |
'directories' | 指定搜索的格式為文件夾 |
'recursive' | 指定可以遍歷子文件夾下的文件 |
'follow_links' | |
'max_depth 5' | 指定遍歷的深度 |
'max_files 1000' | 指定遍歷的最大文件數目 |
Files:文件(文件的路徑)
2)文件格式篩選
tuple_regexp_select( : : Data, Expression : Selection)
Data:被選擇的文件路徑數組
Expression:文件格式的篩選規則
//. | 轉義 . |
(bmp|JPG) | 篩選的文件格式 |
'ignore_case' | 忽略大小寫 |
Selection:選擇出的文件路徑數組
示例:
1: * 遍歷文件夾D:/資料庫/Downloads
2: list_files ('D:/資料庫/Downloads', ['files','follow_links'], ImageFiles)
3:
4: * 篩選bmp或jpg格式的文件
5: tuple_regexp_select (ImageFiles, ['\\.(bmp|jpg)$','ignore_case'], ImageFiles)
6:
7: * 依次讀取圖片
8: for Index := 0 to |ImageFiles| - 1 by 1
9: read_image (Image, ImageFiles[Index])
10:
11: endfor
(一)Halcon的語法結構特點
類似於Pascal 與 Visual Basic,大部分的語句是Halcon提供的算子,此外也包含了少部分的控制語句;
不允許單獨聲明變量;
提供自動的內存管理(初始化、析構及OverWrite),但句柄則需要顯示釋放;
C++(算子模式)
通過代碼導出,以C++為例,默認導出為算子型的語法結構,而非面向對象的;在此模式下,全部函數聲明為全局類型,數據類型只需要用Hobject、HTuple兩類類型進行聲明;
C++(面向對象)
可以以面向對象的方式重寫代碼,也即利用類及類的成員函數;
在這種模式下,控制變量的類型仍未HTuple,而圖形數據可以由多種類型,如HImage等;
其他語言(略)
HImage 可以查看halcon中類相關的內容
(二)Halcon的數據類型
兩類參數:
1、圖形參數Iconic (image, region, XLD)
2、與控制參數Control (string, integer, real, handle),
在Halcon算子的參數中,依次為:輸入圖形參數、輸出圖形參數、輸入控制參數、輸出控制參數;並且其輸入參數不會被算子改變。
1、圖形參數Iconic:
(1)Images
在Halcon中,Image = Channel + Domain , 像素點存放在Channel矩陣中,根據ROI來描述Image。
Image相關操作:
輸入:從文件、從設備
生成:外部圖像數據、空內存區域;
顯示:disp_image()圖像首通道灰度圖;disp_color() 彩色圖;disp_channel()某特定通道;disp_obj() 自動判別類別;
縮放:set_part() 設置顯示區域;set_part_style() 設置顯示參數;
說明:
Ø Multiple channels //多通道圖像,可以是灰度圖像或RGB圖像
Ø Arbitrary region of interest //ROI區域圖像
Ø 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 //連續圖形的光學流分布
(2)Regions
以行列坐標形式儲存,有廣泛的應用,特點是高效,可利用同態算子。比如用閾值對圖像分割的結果,其他系統中稱為BOLB,AREA等。
(3)Extended Line Description (XLD)
圖像均用像素點保存,而像素點是整型的,不連續的,Halcon做了拓展,定義了亞像素(subpixel)的描述幾何輪廓的對象:xld,主要用在亞像素測量的背景下,可用於如提取邊緣、構建輪廓等等,xld在模板匹配、圖形校准等多方面有重要的用途。
說明:
Subpixel accurate line and edge detection(亞像素精度的線和邊緣檢測)
Generic point list based data structure(依據數據結構產生點的表)
Handling of contours, polygons, lines, parallels, etc.(對輪廓,多邊形,線等進行操作)
2、控制參數Control:
String類型變量由單引號’括起來;此外還有一些特殊字符;
Boolean型變量包括 true ( = 1 )、 false ( = 0 ) ;不為零的整數將被認為true;但絕大多數的Halcon函數接受字符串型的表達:’true’‘false’,而非邏輯型表達;
此外,Halcon支持的類型還包括圖形元組、控制變量元組及句柄:
元組的概念,使得可以用一個變量傳遞數個對象,可以由重載后的函數來進行處理;圖形元組的下標從1開始,控制變量元組下標從0開始;句柄則可以用來描述窗體、文件等等,句柄不能是常量。
(三)Halcon的基本語句
1、標准賦值
Ø assign(Input, Result) //編輯形式,永遠都是輸入在前,輸出在后
1: assign(sin(x) + cos(y), u)
Ø Result := Input //代碼形式
1: u := sin(x) + cos(y) //與之前的assign(sin(x) + cos(y), u)是等價的
2、元組插入賦值
Ø insert(Tuple, NewValue, Index, Tuple) //編輯形式
1: Tuple := [1,2,3,4,5,6,7,8,9]
2: insert(Tuple,0,3,Tuple)
Ø Tuple[Index] := NewValue //代碼形式
1: Tuple := [1,2,3,4,5,6,7,8,9]
2: Tuple[3]:=0
例程:
1: read_image (Mreut, 'mreut') //讀入圖像
2: threshold (Mreut, Region, 190, 255) //閾值化,輸出閾值在190-255的Regions
3: Areas := [] //定義數組Areas
4: for Radius := 1 to 50 by 1 //循環
5: dilation_circle (Region, RegionDilation, Radius) //利用半徑為Radius的圓對Region進行膨脹運算,輸出
6: //RegionDilation,輸出形式仍然為Region。
7: area_center (RegionDilation, Area, Row, Column) //輸出區域的面積和中心像素坐標
8: Areas[Radius-1] := Area //對數組Areas的第Radius-1個元素進行賦值
9: endfor
3、基本數組操作極其對應的算子
數組操作 | 說明 | 對應的算子 |
t := [t1,t2] | t1,t2連接成新的數組 | tuple_concat |
i := |t| | 得到數組長度 | tuple_length |
v := t[i] | 選取第i個元素0<= i < |t| | tuple_select |
t := t[i1:i2] | 選取i1到i2的元素 | tuple_select_range |
t := subset(t,i) | 選取數組t中的第i個元素 | tuple_select |
t := remove(t,i) | 去除數組t中的第i個元素 | tuple_remove |
i := find(t1,t2) | 找到t2數組在t1數組中出現位置索引(or -1 if no match) | tuple_find |
t := uniq(t) | 在t數組中把連續相同的值只保留一個 | tuple_uniq |
4、創建數組
(1)gen_tuple_const函數
1: tuple_old := gen_tuple_const(100,666) //創建一個具有100個元素的,每個元素都為666的數組
2: tuple_new := gen_tuple_const(|tuple_old|,4711) //創建一個和原來數據長度一樣的,每個元素為4711的數組
(2)當數組中的元素不同時,需要用循環語句對數組中的每一個元素賦值
例如:
1: tuple := [] //創建空數組
2: for i := 1 to 100 by 1 //建立步長為1的循環
3: tuple := [tuple,i*i] //將i方的值賦給數組的第i個元素
4: endfor //循環結束
算術運算
Ø 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
邏輯操作符
Ø lnot 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
(四)HDevelop language(結構語句)
1) if ... endif / if ... else ... endif / if ... elseif ... else ... endif
2) for ... endfor
3) while ... endwhile
4) repeat ... until(循環體至少被執行一次,直到滿足條件時退出。等同於C語言的do...while語句)
此外,也有關鍵字 break、continue、return、exit、stop 用來控制語句的執行;
stop:終止后面的循環,點擊Step Over or Run button繼續。
exit:終止Hdevelop程序段。
(五)異常處理
異常處理:
try ... catch ... endtry:異常算子處理句柄
throw:允許處理用戶定義的意外情況。
用MFC寫的,我在捕獲異常提時候,都需要在前面使用HException::InstallHHandler(&CPPExpDefaultExceptionHandler);才能全try{..}catch(HException &except){..} 生效
在VC中其實是靠不住的。例如下面的代碼:
- try
- {
- BYTE * pch ;
- pch = ( BYTE * ) 00001234 ; // 給予一個非法地址
- * pch = 6 ; // 對非法地址賦值,會造成Access Violation 異常
- }
-
//...是捕捉任意類型的異常.
- catch ( ... )
- {
- AfxMessageBox ( " catched " ) ;
- }
這段代碼在debug下沒有問題,異常會被捕獲,會彈出”catched”的消息框。但在Release方式下如果選擇了編譯器代碼優化選項,則 VC編譯器會去搜索try塊中的代碼, 如果沒有找到throw代碼,他就會認為try catch結構是多余的, 給優化掉。這樣造成在Release模式下,上述代碼中的異常不能被捕獲,從而迫使程序彈出錯誤提示框退出。
那么能否在release代碼優化狀態下捕獲這個異常呢, 答案是有的。 就是__try, __except結構,上述代碼如果改成如下代碼異常即可捕獲。
- __try
- {
- BYTE * pch ;
- pch = ( BYTE * ) 00001234 ; // 給予一個非法地址
- * pch = 6 ; // 對非法地址賦值,會造成Access Violation 異常
- }
- __except ( EXCEPTION_EXECUTE_HANDLER )
- {
- AfxMessageBox ( " catched " ) ;
- }
但是用__try, __except塊還有問題, 就是這個不是C++標准, 而是Windows平台特有的擴展。而且如果在使用過程中涉及局部對象析構函數的調用,則會出現C2712 的編譯錯誤。 那么還有沒有別的辦法呢?
當然有, 就是仍然使用C++標准的try{}catch(..){}, 但在編譯命令行中加入 /EHa 的參數。這樣VC編譯器不會把try catch模塊給優化掉了。
(6)單攝像機標定
In the reference manual,operator signatures are visualized in the following way:
operator ( iconic input : iconic output : control input : control output )
在HALCON所有算子中,變量皆是如上格式,即:圖像輸入:圖像輸出:控制輸入:控制輸出。
其中四個參數任意一個可以為空。
控制輸入可以是變量、常量、表達式;
控制輸出以及圖像輸入和輸出必須是變量。
1.caltab_points:從標定板中讀取marks中心坐標,該坐標值是標定板坐標系統里的坐標值,該坐標系統以標定板為參照,向右為X正,下為Y正,垂直標定板向下為Z正。該算子控制輸出為標定板中心3D坐標。
2.create_calib_data:創建Halcon標定數據模型。輸出一個輸出數據模型句柄。
3.set_calib_data_cam_param:設定相機標定數據模型中設置相機參數的原始值和類型。設置索引,類型,以及相機的原始內參數等。
4.set_calib_data_calib_object:在標定模型中設定標定對象。設定標定對象句柄索引,標定板坐標點儲存地址。
5.find_caltab:分割出圖像中的標准標定板區域。輸出為標准的標定區域,控制
6.find_marks_and_pose:抽取標定點並計算相機的內參數。輸出MARKS坐標數組,以及估算的相機外參數。
即標定板在相機坐標系中的位姿,由3個平移量和3個旋轉量構成。
7.set_calib_data_observ_points( : : CalibDataID, CameraIdx, CalibObjIdx,CalibObjPoseIdx, Row, Column, Index, Pose : )
收集算子6的標定數據,將標定數據儲存在標定數據模型中。輸入控制分別為標定數據模型句柄,相機索引,標定板索引,位姿索引,行列坐標,位姿。
8.calibrate_cameras( : : CalibDataID : Error) 標定一台或多台相機,依據CalibDataID中的數據。控制輸出平均誤差。
9.get_calib_data( : : CalibDataID, ItemType, ItemIdx, DataName : DataValue) 獲得標定數據。
依靠索引號和數據名稱來返回輸出的數據值。可查詢與模型相關的數據,與相機相關的數據(包括相機的內外參數等),與標定對象相關的數據,與標定對象的姿態相關的數據。控制輸出是要查詢的標定數據。
如:
get_calib_data (CalibDataID, 'camera', 0, 'params', CamParam) //查詢相機的位姿
get_calib_data (CalibDataID, 'calib_obj_pose', [0, NumImage], 'pose', Pose) //查詢標定板位姿
10.write_cam_par( : : CameraParam, CamParFile : ) 記錄相機的內參數,輸入控制為內參數,輸出控制為
存取相機內參數的文件名。
11.set_origin_pose( : : PoseIn, DX, DY, DZ : PoseNewOrigin)
設置新的坐標原點,控制輸入為原始的位姿和沿着世界坐標系的三個坐標軸的平移量,控制輸出為新的位姿。