HALCON从入门到入门-高斯滤波的几种方式

测试效果

图像平滑(image smoothing),也称为图像模糊处理或图像滤波(image filtering),是一种图像处理技术,旨在弱化或消除图像中的细节、突变、边缘和噪声。

derivate_gauss 是HALCON图像处理库中的一个重要算子,它实现了图像与高斯函数导数的卷积操作。derivate_gauss算子主要用于计算图像的高斯导数,这是一种结合了高斯滤波和平滑导数的技术。通过计算图像的高斯导数,可以有效地提取图像的边缘、角点等特征,同时平滑图像中的噪声。

binomial_filter是Halcon图像处理库中的一个算子,用于对图像进行平滑处理。该算子通过使用一个掩膜(mask),其大小为指定的MaskWidth和MaskHeight像素,来对图像进行二项式滤波。二项式滤波器是一个非常好的高斯滤波器的近似值,且由于它只使用整数运算,因此可以实现非常高效的图像平滑。

上面几张图表示了三种不同的滤波方式在不同的sigma值下的运行时间对比。

测试代码

* This example illustrates the differences between the diverse
* implementations of Gaussian filters provided by HALCON.
* While their results are very similar, the way they approximate
* the Gaussian differs and with this some properties like their
* runtimes and the supported image types.
* Therefore, the decision which implementation should be used for
* a particular application depends on the conditions set by the
* application.
* 
* Briefly, the properties of the different implementations can be
* summarized as follows:
* 1. gauss_filter     offers the best compromise between runtime
*                     and accuracy.
* 2. binomial_filter  is a fast approximation to the true
*                     Gaussian, but also the most inaccurate.
* 3. derivative_gauss is the most exact implementation but it
*                     returns only real images.
* 4. smooth_image     is the slowest implementation.
*                     It has advantages when used with Deriche or
*                     Shen filters, which have runtimes independent
*                     of the mask size.
* 
* Note that the values for mask sizes and sigmas were chosen such that
* the smoothing results are as similar as possible.
* 
* Initialize display
dev_update_off ()
dev_close_window ()
Width := 1000
Height := 400
gen_lines_image (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'white', MainWindow)
set_display_font (MainWindow, 14, 'mono', 'true', 'false')
* 
* Initialize filter names and sizes
MaskSizeBinomial := [5,11,17,27]
MaskSizeGauss := [5,7,9,11]
Sigma := [1.075,1.55,2.025,2.55]
FilterNames := ['Operator','smooth_image','gauss_filter','derivate_gauss','binomial_filter']
Colors := ['blue','orange','lime green','magenta','cornflower blue']
* 
* 
ComputeDevice := 'GPU'
InputTypes := 'Allowed input image types'
OutputTypes := 'Output image types'
for I := 1 to |FilterNames| - 1 by 1
    get_operator_info (FilterNames[I], 'compute_device', Info)
    ComputeDevice[I] := Info
    get_operator_info (FilterNames[I], 'parameter', Param)
    get_param_info (FilterNames[I], Param[0], 'type_list', Types)
    InputTypes[I] := sum(Types + ' ')
    get_param_info (FilterNames[I], Param[1], 'type_list', Types)
    OutputTypes[I] := sum(Types + ' ')
endfor
* 
Color := ['blue',gen_tuple_const(|FilterNames| - 1,'black')]
ColorGPU := ['blue',gen_tuple_const(|FilterNames| - 1,'forest green')]
None := find(ComputeDevice,'none')
if (None != -1)
    ColorGPU[None] := 'red'
endif
* 
dev_clear_window ()
Message := 'HALCON offers multiple implementations of Gaussian or Gaussian-like filters.'
Message[1] := ' '
Message[2] := 'The table below shows which of the operators'
Message[3] := ' - runs on compute devices (GPUs)'
Message[4] := ' - supports which image types as input and output'
* 
disp_message (MainWindow, Message, 'window', 12, 12, 'black', 'false')
Column := 12
disp_message (MainWindow, FilterNames, 'window', 122, Column, Color, 'false')
calculate_next_column (FilterNames, MainWindow, Column, Column)
disp_message (MainWindow, ComputeDevice, 'window', 122, Column, ColorGPU, 'false')
calculate_next_column (ComputeDevice, MainWindow, Column, Column)
disp_message (MainWindow, InputTypes, 'window', 122, Column, Color, 'false')
calculate_next_column (InputTypes, MainWindow, Column, Column)
disp_message (MainWindow, OutputTypes, 'window', 122, Column, Color, 'false')
calculate_next_column (OutputTypes, MainWindow, Column, Column)
* 
Message := 'In the following, the results and execution times of the different operators are compared.'
Message[1] := ''
Message[2] := 'Note that the differences in the filter results are VERY small.'
Message[3] := 'This means that in most cases, the execution times, GPU support, and supported image types'
Message[4] := 'should be considered to choose the most suitable operator for your application.'
disp_message (MainWindow, Message, 'window', 252, 12, 'black', 'false')
disp_continue_message (MainWindow, 'black', 'true')
stop ()
* 
dev_clear_window ()
dev_set_draw ('margin')
dev_set_line_width (2)
Colors[0] := 'dim gray'
dev_display (Image)
Title := 'Compare Gaussian smoothing filter operators'
FilterNames[0] := 'No filter'
* 
* Initialize regions of interest
ColumnStart := 30
ColumnEnd := 470
gen_rectangle1 (GaussRectangle, 115, ColumnStart, 164, ColumnEnd)
gen_rectangle1 (BinomialRectangle, 165, ColumnStart, 214, ColumnEnd)
gen_rectangle1 (DerivateRectangle, 215, ColumnStart, 264, ColumnEnd)
gen_rectangle1 (SmoothRectangle, 265, ColumnStart, 314, ColumnEnd)
gen_rectangle1 (OrigRectangle, 315, ColumnStart, 364, ColumnEnd)
concat_obj (GaussRectangle, BinomialRectangle, Rectangles)
concat_obj (Rectangles, DerivateRectangle, Rectangles)
concat_obj (Rectangles, SmoothRectangle, Rectangles)
concat_obj (Rectangles, OrigRectangle, Rectangles)
VertProjections := []
* 
* Main loop:
* Compare results and execution times for different filter sizes
for I := 0 to |Sigma| - 1 by 1
    dev_clear_window ()
    dev_set_part (0, 0, Height - 1, Width - 1)
    dev_display (Image)
    * Prepare visualization
    Suffix := ''
    Suffix[1] := '(deriche2) (Alpha = ' + (0.885 / Sigma[I])$'.3f' + ')'
    Suffix[2] := ' (' + MaskSizeGauss[I] + 'x' + MaskSizeGauss[I] + ')'
    Suffix[3] := ' (Sigma = ' + Sigma[I] + ')'
    Suffix[4] := ' (' + MaskSizeBinomial[I] + 'x' + MaskSizeBinomial[I] + ')'
    Subtitle := ' Sigma = ' + Sigma[I] + ' (or corresponding mask size)'
    disp_message (MainWindow, Title, 'window', 12, 12, 'black', 'true')
    disp_message (MainWindow, Subtitle, 'window', 42, 12, 'black', 'false')
    * 
    * Inner loop:
    * Measure execution times for each filter
    for R := 1 to |FilterNames| by 1
        select_obj (Rectangles, Domain, R)
        perform_filter_benchmark (Image, Domain, ImageGauss, FilterNames[R - 1], MaskSizeGauss[I], MaskSizeBinomial[I], Sigma[I], Time)
        Runtime[(R - 1) * |Sigma| + I] := Time
        * 
        * Display result
        Color := Colors[R - 1]
        dev_set_color (Color)
        dev_display (ImageGauss)
        dev_display (Domain)
        area_center (Domain, Area, Row, Column)
        * 
        * Plot gray value profile in second window
        gray_projections (Domain, ImageGauss, 'simple', HorProjection, VertProjection)
        VertProjections := [VertProjections,VertProjection]
        gen_contour_polygon_xld (Line, 90 + [R,R] * 50, [470,500])
        Message := 'Gray value profiles of filtered regions.'
        Message[1] := 'Please zoom in to check the differences'
        Message[2] := 'in detail.'
        dev_set_color (Color)
        dev_display (Line)
        disp_message (MainWindow, FilterNames[R - 1] + Suffix[R - 1], 'window', Row, 35, Color, 'white')
        disp_message (MainWindow, Message, 'window', 12, 512, 'black', 'true')
        disp_message (MainWindow, [FilterNames[R - 1],Time$'.2' + ' ms'], 'window', 75 + R * 50, 512, Color, 'false')
    endfor
    GenericParamNames := ['axes_color','margin_top','margin_left','margin_bottom','margin_right']
    GenericParamValues := ['black',115,680,80,20]
    plot_tuple (MainWindow, [ColumnStart:ColumnEnd], VertProjections, 'column', '', Colors, GenericParamNames, GenericParamValues)
    disp_continue_message (MainWindow, 'black', 'true')
    stop ()
endfor
* 
* Plot average execution times
Max := max(Runtime)
dev_clear_window ()
plot_tuple (MainWindow, Sigma$'.3f', Runtime[|Sigma|:|Runtime| - 1], 'Sigma', 'Average execution times on a ' + Width + ' x ' + Height + ' image in ms', Colors[1:|FilterNames| - 1], 'axes_color', 'black')
disp_message (MainWindow, FilterNames[1:4], 'window', 70, 110, Colors[1:4], 'false')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄晓魚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值