以下代码是使用HALCON软件的脚本语言编写的,主要功能是演示如何使用相位相关性(phase correlation)通过快速傅里叶变换(FFT)来计算两幅图像之间的平移量,具体实施如下:

  • This example shows how phase_correlation_fft can be used to compute the
  • translation between two images.
    dev_update_off ()
    read_image (Image, ‘wafer/wafer_dies.png’)
    get_image_size (Image, Width, Height)
    dev_close_window ()
    dev_open_window (0, 0, Width, Height, ‘black’, WindowHandle)
    set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)
    dev_display (Image)
    disp_message (WindowHandle, ‘Original image’, ‘window’, 12, 12, ‘black’, ‘true’)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
  • Translate the image.
    RowTrans := 92.4
    ColumnTrans := 58.2
    optimize_rft_speed (Width, Height, ‘standard’)
    hom_mat2d_identity (HomMat2D)
    hom_mat2d_translate (HomMat2D, RowTrans, ColumnTrans, HomMat2D)
  • We set ‘init_new_image’ to ‘true’ to ensure the translated image has
  • defined values of 0 in the part that lies outside the original image.
    get_system (‘init_new_image’, InitNewImage)
    set_system (‘init_new_image’, ‘true’)
    affine_trans_image (Image, ImageTrans, HomMat2D, ‘constant’, ‘false’)
    full_domain (ImageTrans, ImageTranslated)
    set_system (‘init_new_image’, InitNewImage)
  • Simulate a degradation of the image by an uneven illumination.
    gen_image_surface_second_order (ImageSurface, ‘byte’, 0.0005, -0.0008, 0, 0, 0, 128, Height / 2, Width / 2, Width, Height)
    add_image (ImageTranslated, ImageSurface, ImageDegraded, 1, -128)
    dev_display (ImageDegraded)
    disp_message (WindowHandle, [‘Image translated by (’ + RowTrans′4.1f′+′,′+ColumnTrans'4.1f' + ',' + ColumnTrans4.1f+,+ColumnTrans’4.1f’ + ‘)’,‘and degraded by uneven illumination’], ‘window’, 12, 12, ‘black’, ‘true’)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
  • Compute the phase correlation.
    rft_generic (Image, ImageFFT, ‘to_freq’, ‘none’, ‘complex’, Width)
    rft_generic (ImageDegraded, ImageTransFFT, ‘to_freq’, ‘none’, ‘complex’, Width)
    phase_correlation_fft (ImageFFT, ImageTransFFT, ImagePhaseCorrelationFFT)
    rft_generic (ImagePhaseCorrelationFFT, ImagePhaseCorrelation, ‘from_freq’, ‘n’, ‘real’, Width)
    dev_display (ImagePhaseCorrelation)
    gen_circle_contour_xld (Circle, RowTrans, ColumnTrans, 20, 0, 6.28318, ‘positive’, 1)
    dev_set_color (‘green’)
    dev_display (Circle)
    disp_message (WindowHandle, [‘Phase correlation image:’,‘note the peak in the upper left corner’], ‘window’, 12, 12, ‘black’, ‘true’)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
  • Since the phase correlation is cyclic, negative translations result in
  • peaks in lower or right part of the image. If the translation is close
  • 0 in one or two directions, the interpolation in local_max_sub_pix would
  • therefore access wrong values because of its border treatment (which is
  • not cyclic). To obtain a translation that is correct in all cases, we
  • shift the phase correlation cyclically so that a zero translation
  • corresponds to the center of the image. We then correct the coordinates
  • returned by local_max_sub_pix.
    RowOffset := Height / 2
    ColumnOffset := Width / 2
    cyclic_shift_image (ImagePhaseCorrelation, ImageCyclicShift, RowOffset, ColumnOffset)
    local_max_sub_pix (ImageCyclicShift, ‘facet’, 1, 0.02, RowShifted, ColumnShifted)
    Row := RowShifted - RowOffset
    Column := ColumnShifted - ColumnOffset
    dev_set_part (0, 0, Height - 1, Width - 1)
    dev_display (ImageDegraded)
    disp_message (WindowHandle, ‘Translation computed by the phase correlation:\n (’ + Row′5.2f′+′,′+Column'5.2f' + ',' + Column5.2f+,+Column’5.2f’ + ‘)’, ‘window’, 12, 12, ‘black’, ‘true’)
    gen_cross_contour_xld (Cross, Row, Column, 20, 0.0)
    dev_display (Cross)

在这里插入图片描述
以下是代码解释:

dev_update_off():关闭设备更新,以提高性能。
read_image():读取名为 ‘wafer/wafer_dies.png’ 的图像。
get_image_size():获取图像的宽度和高度。
dev_close_window() 和 dev_open_window():关闭当前窗口并打开一个新的窗口来显示图像。
set_display_font():设置窗口中文本的显示字体。
dev_display() 和 disp_message():显示原始图像,并在窗口中显示相关信息。
stop():暂停执行,直到用户继续。
定义图像的平移量 RowTrans 和 ColumnTrans。
optimize_rft_speed():优化快速傅里叶变换(FFT)的速度。
hom_mat2d_identity() 和 hom_mat2d_translate():创建一个2D仿射变换矩阵,并应用平移。
get_system() 和 set_system():获取和设置系统参数,以确保平移后的图像在原始图像外部的部分被填充为。
affine_trans_image() 和 full_domain():对图像进行仿射变换,并确保变换后的图像域是完整的。
gen_image_surface_second_order() 和 add_image():生成一个用于模拟不均匀照明的图像表面,并将其添加到变换后的图像中。
dev_display() 和相关 disp_message():显示退化后的图像,并在窗口中显示相关信息。
rft_generic():对原始图像和退化后的图像进行快速傅里叶变换(FFT)。
phase_correlation_fft():计算两幅图像的相位相关性。
rft_generic():将相位相关性的结果从频率域转换回空间域。
gen_circle_contour_xld() 和 dev_display():生成一个圆轮廓,并在相位相关性图像上显示。
disp_message():在窗口中显示相位相关性图像的信息。
cyclic_shift_image() 和 local_max_sub_pix():对相位相关性图像进行循环移位,并找到局部最大值的子像素位置。
Row 和 Column:计算平移量,并在窗口中显示。
gen_cross_contour_xld() 和 dev_display():生成一个十字轮廓,并在退化后的图像上显示平移量。
总之,这段代码演示了如何使用相位相关性通过FFT来计算图像之间的平移量,并展示了如何通过模拟不均匀照明来退化图像,以及如何对相位相关性图像进行处理以找到正确的平移量。这对于图像配准、目标跟踪和机器视觉等领域非常有用。

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐