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
Ø 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();
图形显示参数设置:
Ø 显示模式: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等