halcon例程 — 逐字细究(五)

  • Post author:
  • Post category:其他


* This example shows an application case from the automobile

* industry. A monitoring system in a car  checks the sidewalk

* for roadsigns to support the driver in case of any inattention.

* To show the imaging process we focus on two road signs,

* the attention and the dead end road sign. First the models

* of both signs are generated and then detected in a street

* sequence.

*

dev_close_window ()

*关闭当前活动的窗口

* Read in model images.

* While the attention sign is from a synthetic source,

* the model for the dead end sign is from another sequence.

*read_image (ImageAttentionSign, ‘road_signs/attention_road_sign’)

* Image Acquisition 01: Code generated by Image Acquisition 01

read_image (ImageAttentionSign, ‘F:/halcon/road_signs/attention_road_sign.jpg’)

*读取图像

*read_image (ImageInit, ‘road_signs/street_01’)

* Image Acquisition 02: Code generated by Image Acquisition 02

read_image (ImageInit, ‘F:/halcon/road_signs/street_01.jpg’)

*读取图像

dev_open_window_fit_image (ImageInit, 0, 0, -1, -1, WindowHandle)

**按照指定图像的大小来打开一个窗口

*Image 输入图像名称,窗口的大小按该图像的实际大小来设定

*Row 输入窗口左上角的坐标y值,默认:0

*Column 输入窗口左上角的坐标x值,默认:0

*WidthLimit 输入宽度界限,默认: -1.该图像的最大宽度

*HeightLimit 输入高度界限,默认: -1.该图像的最大高度

*WindowHandle 输出新创建的窗口句柄

dev_update_off ()

*关闭dev_update_pc/dev_update_var/dev_update_window,停止刷新pc/变量/窗口

dev_set_line_width (2)

*设置区域,图形,XLD轮廓的线宽

dev_set_color (‘green’)

*设置区域,图形,XLD轮廓的颜色,直到下次该函数的调用更改

dev_set_draw (‘margin’)

*设置填充模式

set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)

*设置显示字体格式

*

* Some values for the later matching process are initialized

* The Attention sign has a significant red part, the

* dead end sign a blue one. Hence, we can extract the respective

* channels from the color images.

Channel := [3,1]

* In this example, we have significant scalings of the road signs.

ScaleRMin := [0.5,0.4]

ScaleRMax := [0.8,2.0]

* One could add an anisotropic scaling for the exhaustive search.

* However, this makes the detection slower and is not required here.

ScaleCMin := [1.0,1.0]

ScaleCMax := [1.0,1.0]

* Add names to the signs.

RoadSign := [‘Attention’,’Dead end’]

HFac := [47.0,50.0]

*

* Prepare the attention sign picture for the model

* creation process.

access_channel (ImageAttentionSign, Image, Channel[0])

*设置一个图像获取设备指定参数,通道数由access_channel()函数决定

*MUltiChannelImage 输入多通道图像

*Image 将多输入通道输出一个图像

*Channel 通道数量,默认:1

zoom_image_factor (Image, ImageZoomed, 0.1, 0.1, ‘weighted’)

**把一个图像缩放到指定比例大小

*Image 输入图像

*ImageZoomed 输出缩放后的图像

*ScaleWidth 宽度方向的比例 [0.001,10]

*ScaleHeight 高度方向的比例 [0.001,10]

*Interpolation 篡改类型

inspect_shape_model (ImageZoomed, ModelImages, ModelRegions, 3, 20)

**创建一个轮廓模型基于金字塔的影像

*Image 输入图像

*ModelImages 输出图像基于金字塔的影像

*ModelRegions 输出模型区域

*NumLevels 使用金字塔层数

*Contrast 设置对比度

create_planar_uncalib_deformable_model (ImageZoomed, 3, 0.0, 0.0, 0.1, ScaleRMin[0], ScaleRMax[0], 0.05, 1.0, 1.0, 0.5, ‘none’, ‘use_polarity’, ‘auto’, ‘auto’, [], [], ModelID)

**创建一个(不带校正透视的)可变形模型

*Template 输入的图像模板名称,用来产生模型的图像

*NumLevels 最大的金字塔(分析法)层数,默认为: ‘auto’

*AngleStart 输入匹配时的起始角度

*AngleExtent 输入匹配时的角度范围

*AngleStep 输入匹配旋转角度的步长,默认:’auto’

*ScaleRMin y值方向的最小缩放比例

*ScaleRMax y值方向的最大缩放比例

*ScaleRStep y值方向比例缩放的步长

*ScaleCMin x值方向的最小缩放比例

*ScaleCMax x值方向的最大缩放比例

*ScaleCStep x值方向比例缩放的步长

*Optimization 设置模板优化和模板创建方法,默认:’none’

*Metric 设置匹配方法 默认: ‘use_polarity’

*Contrast 设置对比度,默认: ‘auto’

*MinContrast 输入图像中匹配物体的最小差别(对比度),默认:5

*ParamName 输入一般参数的名称,默认: ‘[]’

*ParamValue 输入一般参数值,默认:'[]’

*ModelID 输出模型句柄

Models := ModelID

*

*

*

read_image (ImageDeadEnd, ‘road_signs/dead_end_road_sign’)

*读取图像

access_channel (ImageDeadEnd, Image, Channel[1])

*设置一个图像获取设备指定参数,通道数由access_channel()函数决定

gray_closing_shape (Image, ImageClosing, 5, 5, ‘octagon’)

**关闭带选择掩码的灰度值

*Image 输入图像

*ImageClosing 输出关闭矩形掩码灰度后的图像

*MaskHeight 输入矩形掩码(滤波器)的高度 [3,511]

*MaskWidth 输入矩形掩码(滤波器)的宽度 [3,511]

*MaskShape 输入掩码的外形,默认: ‘octagon’

zoom_image_factor (ImageClosing, ImageZoomed, 0.4, 0.4, ‘weighted’)

*把一个图像缩放到指定比例大小

gen_rectangle1 (Rectangle1, 28, 71, 67, 95)

*获取一个指定大小的矩形区域

reduce_domain (ImageZoomed, Rectangle1, ImageReduced)

*只显示指定大小区域部分的图像

create_planar_uncalib_deformable_model (ImageReduced, 3, 0.0, 0.0, 0.1, ScaleRMin[1], ScaleRMax[1], 0.05, ScaleRMin[1], ScaleRMax[1], 0.1, ‘none’, ‘use_polarity’, ‘auto’, ‘auto’, [], [], ModelID)

*创建一个(不带校正透视的)可变形模型

*

* The following three lines theoretically show how to

* query specific parameters of a model.

* Practically, the derived information is not needed

* within the program.

get_deformable_model_params (ModelID, ‘angle_step’, AngleStep)

**提取一个可变形模型的参数

*ModelID  输入可变形模型句柄

*GenParamNames 输入需要提取的参数名称 默认: ‘angle_start’

*GenParamValues 输出对应的参数值

get_deformable_model_params (ModelID, ‘scale_r_step’, ScaleRStep)

*提取一个可变形模型的参数

Models := [Models,ModelID]

*

* Generate ROI in which the road signs are expected.

* We can discard not significant parts of the image, in which

* no road sign can be located.

gen_rectangle1 (Rectangle, 115, 0, 360, 640)

*获取一个指定大小的矩形区域

*

* Search in image sequence

for Index := 1 to 16 by 1

OutputString := []

TotalTime := 0

*read_image (Image, ‘road_signs/street_’ + Index$’.02′)

read_image (Image, ‘F:/halcon/road_signs/street_’+Index$’.02’+’.jpg’)

*读取图像

* We are using color images, hence the ROI of the search image

* can significantly be reduced based on the color.

determine_area_of_interest (Image, Rectangle, AreaOfInterest)

**在图像中输出可能存在目标的所有区域(该函数为不能分类的本地函数,没有帮助文档)

*Image 输入图像

*Rectangle 输入指定大小的矩形region

*AreaOfInterest 输出可能存在路标的区域

reduce_domain (Image, AreaOfInterest, ImageReduced)

*只显示指定大小区域部分的图像

dev_display (Image)

*显示图像

*

for Index2 := 0 to |Models| – 1 by 1

*

* Depending on the street sign to be found, we use different color

* channels of the image and the operator find_planar_uncalib_deformable_model

* with different parameters because of the varying dimensions of the models.

access_channel (ImageReduced, ImageChannel, Channel[Index2])

*设置一个图像获取设备指定参数,通道数由access_channel()函数决定

count_seconds (Time1)

**通过的时间,即当前时间

find_planar_uncalib_deformable_model (ImageChannel, Models[Index2], 0, 0, ScaleRMin[Index2], ScaleRMax[Index2], ScaleCMin[Index2], ScaleCMax[Index2], 0.8, 1, 0, 2, 0.4, [], [], HomMat2D, Score)

**找出图像中一个不带校正的可变形模型的最佳匹配

*Image 输入需要匹配的图像

*ModelID 输入可变性模型的句柄

**AngleStart 输入匹配时的起始角度

*AngleExtent 输入匹配时的角度范围

*AngleStep 输入匹配旋转角度的步长,默认:’auto’

*ScaleRMin y值方向的最小缩放比例

*ScaleRMax y值方向的最大缩放比例

*ScaleRStep y值方向比例缩放的步长

*ScaleCMin x值方向的最小缩放比例

*ScaleCMax x值方向的最大缩放比例

*MinScore 输入最小的匹配值(匹配质量),考虑到模板的一半被遮挡,默认 0.5

*NumMatches 在图像上找到模板的最大个数

*MaxOverlap 定义了找到的两个目标区域最多重叠的系数如果MaxOverlap=0,找到的目标区域不能存在重叠,如果MaxOverlap=1,所有找到的目标区域都要返回。

*NumLevels 搜索时使用的金字塔层数,默认值:0

*Greediness 搜索时的”贪婪程度”(0:safe but slow 慢而安全,1:fast but matches may be missed 快而可能匹配失败)[0,1]

*ParamName 输入一般参数的名称,默认: ‘[]’

*ParamValue 输入一般参数值,默认:'[]’

*HomMat2D 输入匹配过程中所需要的变换矩阵,如倾斜、反相、镜像、平移、旋转等

*Score 输出目标模型的匹配值(匹配的可信度)

count_seconds (Time2)

**通过的时间,即当前时间,通常两个相同函数配套使用

Time := Time2 – Time1

TotalTime := TotalTime + Time

*

* Display found models.

if (|HomMat2D|)

get_deformable_model_contours (ModelContours, Models[Index2], 1)

**提取一个可变形模型中对应的轮廓

*ModelContours 输出可变形模型对应的轮廓

*ModelID 输入可变形模型句柄

*Level 输入等高线所在金字塔的层数,默认:1

projective_trans_contour_xld (ModelContours, ContoursProjTrans, HomMat2D)

**对一个XLD轮廓进行射影变换

*Contours 输入需要进行射影的等高线轮廓

*ContoursProjTrans 输出被射影的等高线

*HomMat2D 输入变换矩阵

gen_region_contour_xld (ContoursProjTrans, Region, ‘filled’)

**从XLD元组中创建一个区域,该函数使用前必须先创建一个XLD(轮廓),gen_contour_polygon_xld

*Coutour 输入一个XLD

*Region 输入一个xld转换的区域

*Mode 模式,默认为 ‘filled’

union1 (Region, RegionU)

**返回输入区域的并集

*Region 输入区域

*RegionUnion 输出并集区域

area_center (RegionU, Area, R, C)

*计算区域的面积及中心坐标

get_region_runs (RegionU, Row, ColumnBegin, ColumnEnd)

**查询一个区域的扫描宽度编码

*Region 输入区域

*Row 输出扫描线的y值编号

*ColumnBegin 输出扫描线的起始x值坐标 ColumnBegin == Row

*ColumnEnd 输出扫描线的终止x值坐标 ColumnEnd == Row

H := max(Row) – min(Row)

Fac := H / HFac[Index2]

gen_circle (Circle, R, C, 45 * Fac)

*获取一个指定大小的圆形区域

dev_display (Circle)

gen_circle (Circle, R, C, 50 * Fac)

*获取一个指定大小的圆形区域

dev_display (Circle)

dev_display (ContoursProjTrans)

if (Index2 == 0)

OutputString := ‘Attention sign found in : ‘ + (Time * 1000)$’.2f’ + ‘ ms \n’

else

OutputString := ‘Dead end sign found in  : ‘ + (Time * 1000)$’.2f’ + ‘ ms \n’

endif

endif

endfor

if (|OutputString| == 0)

OutputString := ‘No sign found in : ‘ + (Time * 1000)$’.2f’ + ‘ ms \n’

endif

OutputString := [‘Search for all models in: ‘ + (TotalTime * 1000)$’.2f’ + ‘ ms’,OutputString]

disp_message (WindowHandle, OutputString, ‘window’, 10, 10, ‘black’, ‘true’)

*在窗体上显示打印的信息

disp_continue_message (WindowHandle, ‘black’, ‘true’)

*在窗体上显示按F5继续运行的信息

stop ()

endfor

dev_display (Image)

disp_message (WindowHandle, ‘Program finished.\nPress \’Run\’ to clear all deformable models.’, ‘window’, 10, 10, ‘black’, ‘true’)

*在窗体上显示打印的信息

stop ()

* Clean the memory of the models.

for Index1 := 0 to 1 by 1

clear_deformable_model (Models[Index1])

**清除指定的可变形模型,释放内存空间

*ModelID 输入要清除的可变形模型句柄

endfor