Halcon 2D measurement

* 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: = you (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)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324782106&siteId=291194637