随笔 - 272  文章 - 7  评论 - 27  阅读 - 83万

Halcon 2D测量

复制代码
* This program shows how to detect the edges of a diamond
* with subpixel accuracy and calculate the angle between them.
* 
* In contrast to the example measure_diamond.hdev,
* this example uses a metrology model to measure the edges.
* 
* First, the top of the diamond is roughly segmented
* to align the metrology objects.
* Then, the metrology model is applied and returns the
* parameters of the fitted lines.
* Finally, the angle between the two lines is computed.
* 
* Display initializations
dev_update_off ()
dev_close_window ()
read_image (Image, 'diamond/diamond_01')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
get_image_size (Image, Width, Height)
* 
* Create the metrology model data structure
create_metrology_model (MetrologyHandle)
* The image size is set in advance to speed up the
* first call of apply_metrology_model.
set_metrology_model_image_size (MetrologyHandle, Width, Height)
* Define the parameters of the metrology line objects
LineRow1 := [155,155]
LineColumn1 := [400,400]
LineRow2 := [290,290]
LineColumn2 := [230,570]
Tolerance := 20
* 
* Create two metrology line objects and set parameters
add_metrology_object_line_measure (MetrologyHandle, LineRow1, LineColumn1, LineRow2, LineColumn2, Tolerance, 10, 1, 20, [], [], Index1)
* Create region of interest for the alignment
gen_rectangle1 (Rectangle, LineRow1[0] - 40, LineColumn1[0] - 50, LineRow1[0] + 20, LineColumn1[0] + 50)
* Change the reference coordinate system in which the
* metrology model is given to be situated at the top of the diamond
reduce_domain (Image, Rectangle, ImageReduced)
dev_display(Image)
dev_display(Rectangle)
stop()
threshold (ImageReduced, Region, 128, 255)
get_region_points (Region, Rows, Columns)
set_metrology_model_param (MetrologyHandle, 'reference_system', [Rows[0],Columns[0],0])
* 
* Main loop
* 
for I := 1 to 5 by 1
    read_image (Image, 'diamond/diamond_' + I$'02')
    * Roughly segment the diamond's position
    reduce_domain (Image, Rectangle, ImageReduced)
    threshold (ImageReduced, Region, 128, 255)
    * Extract the top of the diamond
    get_region_points (Region, Rows, Columns)
    * 
    * Use the top of the diamond to align the metrology model in
    * the current image
    * 
    align_metrology_model (MetrologyHandle, Rows[0], Columns[0], 0)
    * 
    * 
    * Perform the measurement for both lines in one call
    * 
    apply_metrology_model (Image, MetrologyHandle)
    * 
    * Access results
    * 
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'all_param', LineParameter)
    angle_ll (LineParameter[0], LineParameter[1], LineParameter[2], LineParameter[3], LineParameter[4], LineParameter[5], LineParameter[6], LineParameter[7], Angle)
    Angle := deg(Angle)
    * 
    * Display results
    * 
    * Create line contours
    get_metrology_object_result_contour (ResultContour, MetrologyHandle, 'all', 'all', 1.5)
    intersection_lines (LineParameter[0], LineParameter[1], LineParameter[2], LineParameter[3], LineParameter[4], LineParameter[5], LineParameter[6], LineParameter[7], Row, Column, IsOverlapping1)
    * Calculate the orientation of the two lines
    line_orientation (LineParameter[0], LineParameter[1], LineParameter[2], LineParameter[3], Orientation1)
    if (Orientation1 > 0)
        Orientation1 := Orientation1 - rad(180)
    endif
    line_orientation (LineParameter[4], LineParameter[5], LineParameter[6], LineParameter[7], Orientation2)
    * 
    * Visualize the angle between the lines
    gen_circle_contour_xld (ContCircle, Row, Column, 100, Orientation1, Orientation2, 'positive', 1)
    * Get the used measure regions and the measured points
    * for visualization
    get_metrology_object_measures (Contour, MetrologyHandle, 'all', 'all', MRow, MColumn)
    gen_cross_contour_xld (Cross, MRow, MColumn, 6, rad(45))
    * Display everything
    dev_display (Image)
    dev_set_line_width (1)
    dev_set_color ('yellow')
    dev_display (Contour)
    dev_display (Cross)
    dev_set_line_width (2)
    dev_set_color ('green')
    dev_display (ResultContour)
    dev_set_color ('blue')
    dev_display (ContCircle)
    disp_message (WindowHandle, 'Angle = ' + Angle$'.5' + '°', 'window', 12, 12, 'black', 'true')
    if (I < 5)
        disp_continue_message (WindowHandle, 'black', 'true')
    endif
    stop ()
endfor
* Clean up memory
clear_metrology_model (MetrologyHandle)
复制代码

 

posted on   NLazyo  阅读(1723)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示