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

Halcon 中实现中值滤波主要依赖于 `median_image` 算子,该算子可以对图像进行中值滤波处理,有效去除椒盐噪声并保留图像边缘特征。以下是一个详细的实现方法: ### 使用 `median_image` 算子实现中值滤波 #### 算子语法 ```hdevelop median_image(Image : ImageMedian : MaskType, MaskWidth, MaskHeight, Border : ) ``` - **Image**:输入图像。 - **ImageMedian**:输出的中值滤波图像。 - **MaskType**:滤波窗口的形状,可选 `'square'`(正方形)、`'circle'`(圆形)等。 - **MaskWidth / MaskHeight**:窗口的宽度和高度,通常设置为相同值以获得对称的滤波窗口。 - **Border**:边界处理方式,例如 `'mirrored'` 表示镜像填充边界。 #### 示例代码 以下是一个完整的 Halcon 示例代码,演示如何对图像添加椒盐噪声并使用中值滤波进行去噪处理: ```hdevelop * 读取图像 read_image (Image, 'claudia') * 获取图像尺寸并显示原始图像 get_image_size (Image, Width, Height) dev_close_window () dev_open_window_fit_size (0, 0, Width, Width, -1, -1, WindowHandle) dev_display (Image) * 生成椒盐噪声分布 sp_distribution (5, 5, Distribution) * 对图像添加椒盐噪声 add_noise_distribution (Image, ImageNoise, Distribution) * 显示并保存噪声图像 dev_display (ImageNoise) dump_window (WindowHandle, 'bmp', 'D:/sp_image') * 使用 3x3 窗口进行中值滤波 median_image (ImageNoise, ImageMedian3, 'square', 3, 3, 'mirrored') dump_window (WindowHandle, 'bmp', 'D:/3_image') * 使用 5x5 窗口进行中值滤波 median_image (ImageNoise, ImageMedian5, 'square', 5, 5, 'mirrored') dump_window (WindowHandle, 'bmp', 'D:/5_image') * 使用 7x7 窗口进行中值滤波 median_image (ImageNoise, ImageMedian7, 'square', 7, 7, 'mirrored') dump_window (WindowHandle, 'bmp', 'D:/7_image') ``` ### 实现要点 - **窗口大小**:窗口越大,去噪能力越强,但可能导致图像细节模糊。通常选择 3x3、5x5 等奇数窗口尺寸。 - **边界处理**:`'mirrored'` 是一种常用的边界处理方式,通过镜像扩展图像边界来避免边缘信息丢失。 - **窗口形状**:根据应用场景选择 `'square'` 或 `'circle'`,正方形窗口更常见且计算效率较高。 ### 性能优化 Halcon 中的 `median_image` 算子已经经过高度优化,其执行速度远高于自定义实现。如果使用 OpenCV 等库自行实现类似功能,虽然可以达到相同效果,但处理速度会明显慢于 Halcon [^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黄晓魚

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

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

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

打赏作者

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

抵扣说明:

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

余额充值