Halcon学习笔记
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;在此基础上,进入ORClass-Oriented Programming。这也是Halcon推荐的开发方式:
The vision part is solved with HDevelop,and the 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//選擇一個元組的一部分(i和J是index?)
- subset(t1,t2) selection from t1 by indices in t2 //選擇從T1在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.// 算子:在HDevelop或语言接口之一使用HALCON库的程序。
- 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
// 过滤(噪声,平滑,边缘,位运算,增强)
分割(阈值,拓扑结构,区域生长,分类,比较)
加工区
形态
特征提取
边缘检测
色彩处理和分类
OCR/ OCV
条形码/数据码
测量
整改
灰度值匹配
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();
图形显示参数设置:
- 显示模式:set_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等