以下代码主要用于图像处理和机器视觉应用中的特定目标检测

dev_update_window (‘off’)

  • image acquisition and window size
    open_framegrabber (‘File’, 1, 1, 0, 0, 0, 0, ‘default’, -1, ‘default’, -1, ‘default’, ‘pendulum/pendulum.seq’, ‘default’, -1, 1, FGHandle)
    grab_image (ModelImage, FGHandle)
    get_image_pointer1 (ModelImage, Pointer, Type, Width, Height)
    dev_close_window ()
    dev_open_window (0, 0, Width, Height, ‘black’, WindowHandle)
    dev_set_part (0, 0, Height - 1, Width - 1)
    dev_display (ModelImage)
  • colors and other settings for the visualization
    dev_set_color (‘cyan’)
    dev_set_draw (‘margin’)
    dev_set_line_width (2)
    stop ()
  • ------------------- start of the application ----------------
  • step 1: extract the bright regions
    threshold (ModelImage, BrightRegions, 200, 255)
    connection (BrightRegions, ConnectedRegions)
    fill_up (ConnectedRegions, FilledRegions)
    dev_display (ModelImage)
    dev_display (FilledRegions)
    stop ()
  • step 2: select the region of the card
    select_shape (FilledRegions, Card, ‘area’, ‘and’, 1800, 1900)
    dev_set_part (round(0.2 * Height), round(0.1 * Width) - 1, round(0.7 * Height) - 1, round(0.6 * Width) - 1)
    dev_display (ModelImage)
    dev_display (Card)
    stop ()
  • step 3: use the card as an ROI for the next steps
    reduce_domain (ModelImage, Card, ImageCard)
    stop ()
  • step 4: extract the logo
    dev_set_color (‘blue’)
    threshold (ImageCard, DarkRegions, 0, 230)
    connection (DarkRegions, ConnectedRegions)
    select_shape (ConnectedRegions, Characters, ‘area’, ‘and’, 150, 450)
    union1 (Characters, CharacterRegion)
    dev_display (ModelImage)
    dev_display (CharacterRegion)
    stop ()
  • step 5: enlarge the region slightly
    dilation_circle (CharacterRegion, ROI, 1.5)
    dev_display (ModelImage)
    dev_display (ROI)
    stop ()
  • step 6: creating the model
    dev_set_part (0, 0, Height - 1, Width - 1)
    dev_display (ModelImage)
    reduce_domain (ModelImage, ROI, ImageROI)
    inspect_shape_model (ImageROI, ShapeModelImages, ShapeModelRegions, 5, 25)
    select_obj (ShapeModelRegions, ShapeModelRegion, 1)
    dev_display (ShapeModelRegion)
    create_shape_model (ImageROI, 3, 0, rad(360), ‘auto’, ‘none’, ‘use_polarity’, 30, 10, ModelID)
    get_shape_model_contours (ShapeModel, ModelID, 1)
    stop ()
  • step 7: searching the logo
    for i := 1 to 30 by 1
    grab_image (SearchImage, FGHandle)
    find_shape_model (SearchImage, ModelID, 0, rad(360), 0.7, 1, 0.5, ‘least_squares’, 0, 0.5, RowCheck, ColumnCheck, AngleCheck, Score)
    if (|Score| > 0)
    vector_angle_to_rigid (0, 0, 0, RowCheck, ColumnCheck, AngleCheck, MovementOfObject)
    affine_trans_contour_xld (ShapeModel, ModelAtNewPosition, MovementOfObject)
    dev_display (SearchImage)
    dev_display (ModelAtNewPosition)
    endif
    endfor
    stop ()
  • ------------------- end of the application -----------------
  • clean up
    dev_update_window (‘on’)
    clear_shape_model (ModelID)
    close_framegrabber (FGHandle)

以下是代码的主要功能和步骤:

图像获取和窗口设置:

open_framegrabber:打开文件帧抓取器,用于读取序列图像文件。
grab_image:从帧抓取器获取图像。
get_image_pointer1:获取图像的指针信息,包括指针、类型、宽度和高度。
dev_close_window:关闭当前窗口。
dev_open_window:根据图像大小打开一个新的窗口。
dev_set_part:设置窗口的显示区域。
图像可视化设置:

dev_set_color:设置绘制颜色为青色。
dev_set_draw:设置绘制模式为边缘。
dev_set_line_width:设置线条宽度。
stop:暂停,以便观察结果。
提取亮区域:

threshold:根据阈值提取亮区域。
connection:将提取的亮区域连接成 ** 的区域。
fill_up:填充区域以消除小孔。
dev_display:显示原始图像和提取的亮区域。
选择卡片区域:

select_shape:根据面积选择特定形状的区域。
dev_set_part:设置窗口的显示区域,以便更清晰地显示卡片区域。
dev_display:显示原始图像和选择的卡片区域。
使用卡片作为ROI:

reduce_domain:将图像域减少到卡片区域。
提取标志:

dev_set_color:设置绘制颜色为蓝色。
threshold:根据阈值提取暗区域。
connection:将提取的暗区域连接成 ** 的区域。
select_shape:根据面积选择特定形状的区域。
union1:合并区域以形成标志区域。
扩大区域:

dilation_circle:对标志区域进行膨胀操作,以稍微扩大区域。
创建模型:

inspect_shape_model:创建形状模型的图像和区域。
create_shape_model:创建形状模型。
get_shape_model_contours:获取形状模型的轮廓。
搜索标志:

循环抓取图像,并使用find_shape_model函数搜索标志。
如果找到标志,使用vector_angle_to_rigid和affine_trans_contour_xld计算标志的位置和方向变化。
dev_display:显示搜索图像和找到的标志。
清理:

dev_update_window:重新启用窗口更新。
clear_shape_model:清除形状模型。
close_framegrabber:关闭帧抓取器。
整体而言,这段代码是一个自动化的图像处理流程,用于从序列图像中提取、创建和搜索特定标志的形状模型。它通过图像获取、亮暗区域提取、形状选择、模型创建和搜索等步骤,展示了如何使用HALCON软件进行机器视觉中的目标检测和定位。这种技术在工业自动化、质量检测和机器人导航等领域非常有用。

Logo

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

更多推荐