交通标志识别之Halcon算法实现

* 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.

*这个例子展示了汽车行业的应用案例。在汽车中的监控系统检查人行道的路标,以支持司机在任何疏忽的情况下。这个例程主要关注两个路标,注意和前方无路路标。首先,生成两个标志的模型,然后在街道画面序列中检测。单帧检索时间最有7ms,最差25ms

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')

read_image (ImageInit, 'road_signs/street_01')

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

dev_update_off ()

dev_set_line_width (2)

dev_set_color ('green')

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 colour images.

*对后续匹配过程中的一些值进行初始化,注意标志有一个明显的红色部分,前方无路标志是蓝色的。因此,我们可以从彩色图像中提取各自的通道。

Channel := [3, 1]

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

在这个例子中,我们用到的路标的有意义的尺寸。

ScaleRMin := [0.5, 0.4]

ScaleRMax := [0.8, 2.0]

* One could add anisothropic scaling for the exhaustive search. However, this makes 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])取红色通道

zoom_image_factor (Image, ImageZoomed, 0.1, 0.1, 'weighted')

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

创建一个用来展示出基于形状的模型(这里分了三层)

这个操作用于显示出形状模型的大概情况,主要设定NumLevels的层数和Contrast参数的合理值,属于一个检查工作,为后续创建做准备

注意:如果输入Image是单通道,创建模型的时候metrics可以用‘use_polarity’,‘ignore_global_polarity’and‘ignore_local_polarity’,当是多通道时该参数应该用‘ignore_global_polarity’

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)

交通标志识别之Halcon算法实现_第1张图片

Models := ModelID

read_image (ImageDeadEnd, 'road_signs/dead_end_road_sign')

access_channel (ImageDeadEnd, Image, Channel[1])

gray_closing_shape (Image, ImageClosing, 5, 5, '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)

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.

*下面的程序,生成预期路标的ROI。即取得路标可能在的位置,舍弃不可能存在路标的区域,也就是车辆的正前方

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

* Search in image sequence

然后在序列中寻找路标,即寻找model

for Index := 1 to 16 by 1

    OutputString := []

    TotalTime := 0

    read_image (Image, 'road_signs/street_'+Index$'.02')

    * We are using colour images, hence the ROI of the search image can significantly be reduced based on the colour.

我们使用彩色图像,可以利用色彩区分来减少ROI的区域,提高效率

    determine_area_of_interest (Image, Rectangle, AreaOfInterest)

determine_area_of_interest是创建的一个函数封装,用于筛选交通标志可能存在的区域,先转化为3个单色图,在转化成HSV,然后在色差空间中进行灰度筛选

    reduce_domain (Image, AreaOfInterest, ImageReduced)

    dev_display (Image)

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

        根据被寻找的路标,我们使用不同的颜色通道来进行搜索

        access_channel (ImageReduced, ImageChannel, Channel[Index2])

        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)

        进行模板匹配,HomMat2D是输出的转换矩阵,Score匹配的可信度

        count_seconds (Time2)

        Time := Time2-Time1

        TotalTime := TotalTime + Time

        *

        * Display found models.

           展示寻找到的model,展示搜索结果

        if (|HomMat2D|)如果上面的搜索结果不为空

            get_deformable_model_contours (ModelContours, Models[Index2], 1)

            获得一个可变模型的边界

            projective_trans_contour_xld (ModelContours, ContoursProjTrans, HomMat2D)

            对一个边界进行投影转换,利用扇面找到的转换矩阵HomMat2D转换之后,在原图的相应位置就会产生一个转换后的边界

            gen_region_contour_xld (ContoursProjTrans, Region, 'filled')

            利用边界生成一个区域

            union1 (Region, RegionU)

            area_center (RegionU, Area, R, C)

            get_region_runs (RegionU, Row, ColumnBegin, ColumnEnd)

把region按照chord的形式存储在tuples中,chord就是按照列来一行一行的遍历region,那么在这一行中region就是一条或者多条线段,chord就是记录下这些线段的起始点以及行号加以保存

            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')

    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])

endfor

你可能感兴趣的:(交通标志识别之Halcon算法实现)