ROI区域对机器视觉而言尤为重要,可以大幅减少待处理的像素数,减少无谓的噪声影响,对图像匹配尤为有利,Halcon的ROIS是基于区域的处理,相对其他软件处理方法比较特殊且特别有效。本文记录学习halcon时与ROI相关的部分内容,既包括例程序又包括solution_guide说明的部分,后会编辑心得进入本文。
halcon创建区域的方式有传统的产生标准形状的方法;也可以从其他数据格式得到,如XLD;也可以从其他软件导入,如CAD的DXF文件或者OPENCV的一些文件格式。
创建ROI的步骤如下:分割图像---画区域或创建区域---处理区域(直接导入或者定义的区域经常不满足要求,需要处理如去洞,加壳等)---调整ROI或者图像(经常ROI依据图像序列上物体的位置而变化,需要进行坐标转换)---组合ROI或图像---显示ROI或者缩减的图像。
1.draw_region(
: Region : WindowHandle :
)
利用鼠标交互画一个闭合区域。点左键画图,右键结束,输出为鼠标画的闭合区域。控制输入为窗口句柄。
2.dev_display(Object
: : : )
在图形窗口显示输入的图形对象
(image,
region, or XLD)
3.reduce_domain(Image,
Region : ImageReduced : :
)
得到图像Image相对于Region区域的为定义域的图像。
亚像素精度提取驻点坐标(例如局部最大最小值点或者鞍点),返回这些点的坐标。
5.gen_cross_contour_xld(
: Cross : Row, Col, Size, Angle :
)
在输入的点坐标出产生交亚像素精度的交叉线,SIZE和Angle决定交叉线的长度和角度。
例程序1
* critical_points.hdev:
locate saddle point markers in an image
*
dev_update_window ('off')
* step: acquire image
read_image (Image, 'landmarks')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_set_part (0, 0, Height-1, Width-1)
dev_set_line_width (3)
dev_set_color ('blue')
dev_set_draw ('margin')
dev_display (Image)
set_display_font (WindowHandle, 20, 'mono', 'true',
'false')//内部程序,设置字体//
* step: draw region
* ****
disp_message (WindowHandle, 'Draw with the mouse the region of
interest', 'window', -1, -1, 'blue', 'false')
dev_set_color
('white') //设置输出颜色//
draw_region (Region, WindowHandle)
dev_display (Region)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* ****
* step: create ROI
* ****
reduce_domain (Image, Region, ImageReduced)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* ****
* step: process image within ROI
* ****
critical_points_sub_pix (ImageReduced, 'facet', 1.5, 8, RowMin,
ColumnMin, RowMax, ColumnMax, RowSaddle,
ColSaddle) //提取鞍点和局部最值点//
dev_clear_window ()
dev_display (ImageReduced)
dev_set_color ('yellow')
for i := 0 to |RowSaddle|-1 by
1 //循环在驻点处生成十字线,格式为XLD//
gen_cross_contour_xld (Cross, RowSaddle[i], ColSaddle[i], 25,
0.785398)
dev_display
(Cross)
endfor
stop ()
dev_update_window ('on')
设置HDVELOP内程序的参数。
7.set_mshape(
: : WindowHandle, Cursor :
)
设置当前鼠标点的形状
注意:程序里头的运算符:“#” t1 #
t2 不相等
tuple_not_equal
8.dev_set_check(
: : Mode :
)
指定HDeveop如何返回错误情况。
9.gen_circle(
: Circle : Row, Column, Radius :
)
输出一个圆形区域。输入控制为圆心坐标和半径。
利用同态算子计算区域的边界。边界类型可以是inner', 'inner_filled' and
'outer'. 分表表示区域内部边界,填充洞的区域内部边界,一个像素宽的外部边界。
利用方形或圆形模板对图像进行中值滤波处理,返回滤波后的图像。控制参数分别表示滤波掩膜的种类,半径,
以及是否对区域边缘进行延拓。返回中值滤波后的图像。
复制ImageSource图像的灰度值覆盖到ImageDestination图像上。仅仅定义域内的图像的灰度值被复制修改。
例程序2
*
median_interactive.hdev: react to mouse interaction
*dev_set_preferences ('graphics_window_context_menu',
'false')
dev_close_window ()
dev_update_off ()
* ****
* step: acquire image
* ****
read_image (Image, 'mreut')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, Width, Height,
WindowHandle)
set_display_font (WindowHandle, 12, 'Courier', 'true',
'false')
set_mshape (WindowHandle, 'arrow')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Image)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* ****
* step: react to mouse
* ****
Button := 0
while (Button # 4)
Row :=
-1
Column :=
-1
dev_set_check ('~give_error')
get_mposition (WindowHandle, Row, Column, Button)
dev_set_check ('give_error')
set_system
('flush_graphic', 'false')
dev_display
(Image)
set_tposition (WindowHandle, Height - 40, 4)
* check if
mouse cursor is over window
if (Row
>= 0 and Column >= 0)
gen_circle (Circle, Row, Column, 20)
boundary (Circle, RegionBorder, 'inner')
dev_display (RegionBorder)
set_system ('flush_graphic', 'true')
write_string (WindowHandle, 'Hold down mouse button to apply
filter')
if (Button = 1)
* apply filter to circle if left button is pressed
reduce_domain (Image, Circle, ImageReduced)
median_image (ImageReduced, ImageMedian, 'circle', 5,
'mirrored')
overpaint_gray (Image, ImageMedian)
endif
else
set_system ('flush_graphic', 'true')
write_string (WindowHandle, 'Please move mouse cursor over
window')
endif
endwhile
dev_display (Image)