【Halcon】提取直线及其宽度

1.思路

  1. 先利用calculate_lines_gauss_parameters()根据最大线宽和灰度值对比度计算lines_gauss()的三个参数:Sigma,LowThresh, HeighThresh
  2. 根据上面得到的3个参数,利用lines_gauss()获取直线和线宽
  3. 按照一定规则,对线进行排序并显示出来

2. 代码

* 关闭窗口更新
dev_update_off ()
* 读取图像
read_image (Angio, 'angio-part')
get_image_size (Angio, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_display (Angio)
disp_message (WindowHandle, 'Original image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 根据最大线宽和对比度计算lines_gauss的三个参数
*1个参数:最大线宽
*2个参数:对比度,若有2个值,第2个值为最小对比度且一定小于第1个值;若
* 仅有1个值,则最小对比度为最大对比度的1/3
calculate_lines_gauss_parameters (8, [12, 0], Sigma, Low, High)
* 计算高斯直线
lines_gauss (Angio, Lines, Sigma, Low, High, 'dark', 'true', 'parabolic', 'true')
select_contours_xld (Lines, RelLines, 'length', 5.0, 999, 0, 0)
dev_set_colored (12)
dev_display (Angio)
dev_display (RelLines)
disp_message (WindowHandle, 'Extacted lines', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
count_obj (RelLines, Number)
* 轮廓按照一定规则排序
sort_contours_xld (RelLines, RelLines, 'lower_left', 'true', 'row')
gen_empty_obj (PrintedLines)
for Index := 1 to Number by 1
    dev_display (Angio)
    dev_set_color ('white')
    dev_display (PrintedLines)
    select_obj (RelLines, Line, Index)
    dev_set_color ('green')
    dev_display (Line)
    concat_obj (PrintedLines, Line, PrintedLines)
    * 获取轮廓点
    get_contour_xld (Line, Row, Col)
    meanRow := sum(Row) / |Row|
    meanCol := sum(Col) / |Col|
    * 获取轮廓属性
    get_contour_attrib_xld (Line, 'width_left', WidthL)
    get_contour_attrib_xld (Line, 'width_right', WidthR)
    get_contour_attrib_xld (Line, 'contrast', Contrast)
    meanContrast := sum(Contrast) / |Contrast|
    * 为了显示线宽,只有线和背景在该点的灰度差达到25%才会显示,
    * 该点是通过抛物线乘以sqrt(3/4)给出
    Diameter := (WidthL + WidthR) * sqrt(0.75)
    Diam := sum(Diameter) / |Diameter|
    if (meanRow > Height - 50)
        disp_message (WindowHandle, 'Diam: ' + Diam, 'image', Height - 70,\
                      meanCol, 'yellow', 'false')
    else
        disp_message (WindowHandle, 'Diam: ' + Diam, 'image', meanRow, meanCol, \
                      'yellow', 'false')
    endif
    disp_message (WindowHandle, 'Diameters of line segments', 'window', 135, 60,\
                  'black', 'true')
    if (Index < 5)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    else
        wait_seconds (0.2)
    endif
endfor
dev_update_on ()

3.结果

【Halcon】提取直线及其宽度_第1张图片

4.算子介绍

  1. calculate_lines_gauss_parameters( : : MaxLineWidth, Contrast : Sigma, Low, High)

【Halcon】提取直线及其宽度_第2张图片

  1. lines_gauss(Image : Lines : Sigma, Low, High, LightDark, ExtractWidth, LineModel, CompleteJunctions : )

    [https://www.mvtec.com/doc/halcon/13/en/lines_gauss.html]:

你可能感兴趣的:(Halcon)