测试效果
Guided Filter,即引导滤波,是一种图像滤波技术,它通过一张引导图(可以是输入图像本身或其他图像)对目标图像进行滤波处理,使得最后的输出图像在保持大体上与输入图像相似的同时,纹理部分与引导图相似。
Bilateral_filter(双边滤波器)是一种非线性的滤波方法,它结合了图像的空间邻近度和像素值相似度进行折中处理,同时考虑空域信息和灰度相似性,以达到保边去噪的目的。
各向异性扩散(Anisotropic Diffusion)是一种图像处理技术,旨在减少图像噪声的同时保持图像的边缘和细节。这种技术利用偏微分方程(PDE)来描述图像的扩散过程,并通过迭代计算来逐步平滑图像。
测试代码
* This example program shows how to use anisotropic_diffusion to smooth an
* image while preserving or enhancing the edge information to different degrees.
*
* Prepare visualization
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_set_part (0, 0, 511, 511)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_clear_window ()
Methods := ['\'guided_filter\'','\'bilateral_filter\'','\'anisotropic_diffusion\'']
Message := 'Compare different methods for'
Message[1] := 'edge-preserving smoothing:'
Message := [Message,' - ' + Methods]
dev_disp_text (Message, 'window', 12, 12, 'white', 'box', 'false')
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*
Loops := 5
Radius := 2
Amplitude := 15
SigmaSpatial := Radius
SigmaRange := Amplitude
Contrast := 3
Theta := 10
Iterations := 1
*
ImageFiles := ['mreut','claudia']
read_image (Images, ImageFiles)
for Index := 1 to |ImageFiles| by 1
select_obj (Images, Image, Index)
get_image_size (Image, ImageWidth, ImageHeight)
dev_resize_window_fit_size (0, 0, ImageWidth, ImageHeight, -1, -1)
dev_display (Image)
Message := 'Original image'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*
* Perform guided filtering.
*
* Execute once for caching to allow a more robust time measurement.
guided_filter (Image, Image, ImageGuided, Radius, Amplitude)
* Measure execution time
count_seconds (S1)
for L := 1 to Loops by 1
guided_filter (Image, Image, ImageGuided, Radius, Amplitude)
endfor
count_seconds (S2)
TimeGuided := S2 - S1
dev_display (ImageGuided)
Message := 'Guided filter'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*
* Perform bilateral filtering.
*
* Execute once for caching to allow a more robust time measurement.
bilateral_filter (Image, Image, ImageBilateral, SigmaSpatial, SigmaRange, [], [])
* Measure execution time
count_seconds (S3)
for L := 1 to Loops by 1
bilateral_filter (Image, Image, ImageBilateral, SigmaSpatial, SigmaRange, [], [])
endfor
count_seconds (S4)
TimeBilateral := S4 - S3
dev_display (ImageBilateral)
Message := 'Bilateral filter'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*
* Perform the anisotropic diffusion using the parabolic edge detection function.
* Note that this function still allows a slight diffusion (smoothing) across edges.
*
* Execute once for caching to allow a more robust time measurement.
anisotropic_diffusion (Image, ImagePeronaMalik, 'perona-malik', Contrast, Theta, Iterations)
* Measure execution time
count_seconds (S5)
for L := 1 to Loops by 1
anisotropic_diffusion (Image, ImagePeronaMalik, 'perona-malik', Contrast, Theta, Iterations)
endfor
count_seconds (S6)
TimeAnisoDiff := S6 - S5
dev_display (ImagePeronaMalik)
Message := 'Perona-Malik anisotropic diffusion'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*
* Display detail
if (ImageFiles[Index - 1] == 'mreut')
dev_set_part (111, 205, 181, 275)
else
dev_set_part (30, 80, 30 + 2 * 48, 80 + 2 * 37)
endif
dev_display (Image)
Message := 'Original image: ' + ImageFiles[Index - 1]
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
stop ()
dev_display (ImageGuided)
Message := 'Guided filter'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
dev_display (ImageBilateral)
Message := 'Bilateral filter'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
dev_display (ImagePeronaMalik)
Message := 'Perona-Malik anisotropic diffusion'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
*
* Compare execution times
Time := [TimeGuided,TimeBilateral,TimeAnisoDiff] * 1000 / Loops
*
tuple_sort_index (Time, Indices)
Colors := ['green','yellow','red']
TimeMax := Time[Indices[2]]
*
Width := 0.9 * ImageWidth * Time / TimeMax
Row := [50,150,250]
Column := gen_tuple_const(3,12)
gen_rectangle1 (Rectangle, Row, Column, Row + 50, Column + Width)
*
dev_clear_window ()
dev_set_part (0, 0, ImageHeight - 1, ImageWidth - 1)
dev_disp_text ('Execution times (' + ImageFiles[Index - 1] + ')', 'window', 12, 12, 'black', [], [])
dev_disp_text (Methods + ': ' + Time$'.1f' + ' ms', 'image', Row + 55, Column, 'white', 'box', 'false')
dev_set_color (Colors[Indices])
dev_display (Rectangle)
if (Index < |ImageFiles|)
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
endif
endfor