Halcon几何定位

H a l c o n 几 何 定 位 Halcon几何定位 Halcon


建模经验

  1. 知道什么是关键轮廓】什么是干扰轮廓
  2. 边缘数量越少越好,边缘数量多了,速度会变慢
  3. 边缘越长越好
  4. 干扰边越少越好
  5. 关键边缘越多越好,占模板轮廓的比例越多越好
  6. 可以用PhotoShop制造理想的模板图像
  7. 灵活运用区域操作技术,获取关键边缘区域
    difference和union:特别对于:回型区域,需要挖洞区域

定位经验

  1. 频繁调节的参数:角度、缩放、分数、极性
  2. 多次搜索策略(分类):匹配分数逐步降低:for循环
  3. 合理设置旋转角度范围
  4. 合理设置贪婪度:定位要求高、贪婪度越小越好,但是时间会变长
  5. 有效提高匹配分数措施:缩放、会增加时间。对分数提高的效果:异步>同步>不缩放。合理设置缩放。

灰度定位与轮廓定位的对比

几何定位 = 轮廓定位

运行时间对缩放的适应性对灰度的适应性(光照的变化,物体本身材质等造成的灰度的线性和非线性变化)对旋转的适应性对物体遮挡和缺失的适应性
灰度定位
轮廓定位

灰度定位,一般指的就是Blob分析
几何定位:支持单模板定位和多模板定位
几何定位分三种:无缩放、xy同步缩放、xy异步缩放
几何定位工具:匹配助手


模板匹配的重要参数
1.对比度
2.最小组件尺寸
3.金字塔等级(等级越高,模板越模糊,匹配越容易)
4.角度范围
5.缩放参数
6.极性(度量)
7.最小分数(相似度、匹配分数)
8.匹配最大数
9.贪心算法
10.最大重叠比
11.形状模板可能与图像边缘交叉


对比度
Contrast不仅仅是对比度,根据数组元素数量不同,其意义不同
1个元素时:10,对比度,直接提取边缘
2个元素时:[10,20],表示10提取不出边缘,则使用20作为参数提取边缘
3个元素时:[10,30,5],前两个参数同上,最后一个参数表示所提取边缘的长度最小为5


几何定位的流程

  1. 图像预处理,创建ROI
  2. 准备模板图像
  3. 创建模板(参数设置)
  4. 输入需要模板匹配的输入图像
  5. 使用模板进行定位(参数设置)
  6. 得到定位结果:位置、角度、缩放比、分值等
  7. 释放模板资源

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

read_image (Image, '1.BMP')

* 
* Matching 02: ************************************************
* Matching 02: BEGIN of generated code for model initialization
* Matching 02: ************************************************
set_system ('border_shape_models', 'false')
* 
* Matching 02: Obtain the model image
* Matching 02: *************
* Matching 02: * ATTENTION *
* Matching 02: *************
* Matching 02: The image from the graphics window is not available. At this point,
* Matching 02: it is necessary to ensure an image is stored in the variable 'Image'
* 
* Matching 02: Build the ROI from basic regions
gen_rectangle1 (ModelRegion, 198.514, 448.541, 267.264, 514.813)
* 
* Matching 02: Reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage)
* 
* Matching 02: Create the shape model
create_scaled_shape_model (TemplateImage, 3, rad(0), rad(360), rad(3.5798), 0.89, 1.1, 0.0312, ['none','no_pregeneration'], 'use_polarity', [10,11,4], 4, ModelID)
* 
* Matching 02: Get the model contour for transforming it later into the image
get_shape_model_contours (ModelContours, ModelID, 1)
* 
* Matching 02: Get the reference position
area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)
vector_angle_to_rigid (0, 0, 0, RefRow, RefColumn, 0, HomMat2D)
affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
* 
* Matching 02: Display the model contours
dev_display (Image)
dev_set_color ('green')
dev_set_draw ('margin')
dev_display (ModelRegion)
dev_display (TransContours)
stop ()
* 
* Matching 02: END of generated code for model initialization
* Matching 02:  * * * * * * * * * * * * * * * * * * * * * * *
* Matching 02: BEGIN of generated code for model application
* 
* Matching 02: Loop over all specified test images
TestImages := ['1.BMP']
for T := 0 to 0 by 1
    * 
    * Matching 02: Obtain the test image
    read_image (Image, TestImages[T])
    * 
    * Matching 02: Find the model
    find_scaled_shape_model (Image, ModelID, rad(0), rad(360), 0.89, 1.1, 0.8, 27, 0.5, 'least_squares', [3,1], 0.4, Row, Column, Angle, Scale, Score)
    * 
    * Matching 02: Transform the model contours into the detected positions
    dev_display (Image)
    for I := 0 to |Score| - 1 by 1
        hom_mat2d_identity (HomMat2D)
        hom_mat2d_scale (HomMat2D, Scale[I], Scale[I], 0, 0, HomMat2D)
        hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
        hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
        affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
        dev_set_color ('green')
        dev_display (TransContours)
        stop ()
    endfor
endfor
* 
* Matching 02: *******************************************
* Matching 02: END of generated code for model application
* Matching 02: *******************************************
* 


实例

* 
* online shape-based matching with adaptation to illumination changes
* rotational invariant
* rotation/translation to "normalize" the input image
* 
* set online to true if you are using an image acquisition device
* if online is set to false, a virtual image acquisition device is opened which reads an image sequence from hard disk
* 
online := false
VisUndefPixelBlack := 1
dev_update_window ('off')
dev_update_pc ('off')
dev_update_var ('off')
dev_update_time ('off')
if (online)
    * open the 'real' image acquisition device
    info_framegrabber ('1394IIDC', 'revision', RevisionInfo, RevisionInfoValues)
    open_framegrabber ('1394IIDC', 1, 1, 0, 0, 0, 0, 'default', -1, 'default', -1, 'default', 'default', 'default', -1, -1, AcqHandle)
else
    * open the virtual image acquisition device
    info_framegrabber ('File', 'general', Information, ValueList)
    open_framegrabber ('File', 1, 1, 0, 0, 0, 0, 'default', -1, 'default', -1, 'default', 'pendulum/pendulum', 'default', -1, 1, AcqHandle)
endif
grab_image (Image, AcqHandle)
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, Width + 30, Width, Height, 'black', WindowRot)
dev_open_window (0, 0, Width, Height, 'black', WindowID)
dev_set_window (WindowID)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_set_window (WindowRot)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_display (Image)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
hom_mat2d_identity (HomMat2DIdentity)
* disp_continue_message (WindowID, 'black', 'true')
* stop ()
* ------------------------
* select the model object
* ------------------------
if (online)
    * 
    * define template by drawing a rectangle
    * 
    dev_update_pc ('off')
    dev_set_window (WindowID)
    dev_set_draw ('margin')
    dev_set_line_width (5)
    grab_image (ImageTempl, AcqHandle)
    dev_set_window (WindowID)
    dev_display (ImageTempl)
    disp_message (WindowID, 'define the template', 'window', 12, 12, 'black', 'true')
    dev_set_color ('green')
    draw_rectangle2 (WindowID, RowTempl, ColumnTempl, PhiTempl, Length1, Length2)
    gen_rectangle2 (Rectangle, RowTempl, ColumnTempl, PhiTempl, Length1, Length2)
    dev_display (Rectangle)
    reduce_domain (ImageTempl, Rectangle, ImageReduced)
    create_shape_model (ImageReduced, 4, 0, rad(360), 'auto', 'none', 'use_polarity', 30, 10, ModelID)
    intensity (ImageReduced, Image, TemplMean, Deviation)
    dev_update_pc ('on')
else
    * 
    * load default object for virtual framegrabber
    * 
    Length1 := 32.0
    Length2 := 12.5
    PhiTempl := -0.315
    RowTempl := 112.5
    ColumnTempl := 92.0
    copy_obj (Image, ImageTempl, 1, 1)
    gen_rectangle2 (Rectangle, RowTempl, ColumnTempl, PhiTempl, Length1, Length2)
    reduce_domain (Image, Rectangle, ImageReduced)
    create_shape_model (ImageReduced, 4, 0, rad(360), 'auto', 'none', 'use_polarity', 30, 10, ModelID)
    intensity (ImageReduced, Image, TemplMean, Deviation)
endif
* RowRot := RowTempl
* ColumnRot := ColumnTempl
RowRot := Height / 2
ColumnRot := Width / 2
* ------------------------------------------
* visualize normalized input image
* ------------------------------------------
* Compensate for rotation and translation
vector_angle_to_rigid (RowTempl, ColumnTempl, PhiTempl, RowRot, ColumnRot, 0, HomMat2DRotate)
affine_trans_image (ImageTempl, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')
if (VisUndefPixelBlack == 1)
    full_domain (ImageAffineTrans, ImageFull)
    dev_set_window (WindowRot)
    dev_display (ImageFull)
else
    dev_set_window (WindowRot)
    dev_display (ImageAffineTrans)
endif
dev_set_window (WindowID)
dev_display (ImageTempl)
disp_message (WindowID, 'run the template matching', 'image', 20, 20, 'yellow', 'false')
disp_message (WindowID, 'run the template matching', 'image', 20, 20, 'yellow', 'false')
disp_message (WindowID, '(click left to start,', 'image', 50, 20, 'yellow', 'false')
disp_message (WindowID, 'right to stop)', 'image', 80, 20, 'yellow', 'false')
* disp_continue_message (WindowID, 'black', 'true')
* stop ()
Button := 0
while (Button != 1)
    get_mbutton (WindowID, dummy, dummy, Button)
endwhile
dev_update_pc ('off')
* ------------------------
* shape-based matching
* ------------------------
Button := 0
Exp := 1
while (Button != 4)
    if (online)
        grab_image_async (Image, AcqHandle, -1)
    else
        grab_image (Image, AcqHandle)
    endif
    count_seconds (Seconds1)
    find_shape_model (Image, ModelID, 0, rad(360), 0.7, 1, 0.5, 'least_squares', 0, 0.7, Row, Column, Angle, Score)
    if (Score > 0.5)
        gen_rectangle2 (Rectangle, Row, Column, PhiTempl + Angle, Length1, Length2)
        reduce_domain (Image, Rectangle, ImageReduced)
        intensity (ImageReduced, Image, Mean, Deviation)
        * Compensate for rotation and translation
        vector_angle_to_rigid (Row, Column, Angle, RowRot, ColumnRot, 0, HomMat2DRotate)
        affine_trans_image (Image, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')
        count_seconds (Seconds2)
        Time := round(1000 * (Seconds2 - Seconds1))
        if (VisUndefPixelBlack == 1)
            full_domain (ImageAffineTrans, ImageFull)
            dev_set_window (WindowRot)
            dev_display (ImageFull)
        else
            dev_set_window (WindowRot)
            dev_display (ImageAffineTrans)
        endif
        dev_set_window (WindowID)
        dev_display (Image)
        disp_message (WindowID, Time + ' ms', 'window', 12, 12, 'black', 'true')
        dev_set_color ('green')
        dev_display (Rectangle)
    else
        dev_display (Image)
    endif
    * --------------------------
    * Exit with mouse click
    * --------------------------
    dev_error_var (Error, 1)
    dev_set_check ('~give_error')
    get_mposition (WindowID, R, C, Button)
    dev_error_var (Error, 0)
    dev_set_check ('give_error')
    if (Error != H_MSG_TRUE)
        Button := 0
    endif
endwhile
clear_shape_model (ModelID)
disp_continue_message (WindowID, 'black', 'true')
stop ()
close_framegrabber (AcqHandle)
dev_set_window (WindowRot)
dev_close_window ()
dev_update_pc ('on')
dev_update_var ('on')
dev_update_time ('on')
dev_update_window ('on')
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值