Halcon 2D测量

时间:2021-10-31 10:57:49
* 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, , , -, -, WindowHandle)
set_display_font (WindowHandle, , '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 := [,]
LineColumn1 := [,]
LineRow2 := [,]
LineColumn2 := [,]
Tolerance :=
*
* Create two metrology line objects and set parameters
add_metrology_object_line_measure (MetrologyHandle, LineRow1, LineColumn1, LineRow2, LineColumn2, Tolerance, , , , [], [], Index1)
* Create region of interest for the alignment
gen_rectangle1 (Rectangle, LineRow1[] - , LineColumn1[] - , LineRow1[] + , LineColumn1[] + )
* 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, , )
get_region_points (Region, Rows, Columns)
set_metrology_model_param (MetrologyHandle, 'reference_system', [Rows[],Columns[],])
*
* Main loop
*
for I := to by
read_image (Image, 'diamond/diamond_' + I$'')
* Roughly segment the diamond's position
reduce_domain (Image, Rectangle, ImageReduced)
threshold (ImageReduced, Region, , )
* 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[], Columns[], )
*
*
* 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[], LineParameter[], LineParameter[], LineParameter[], LineParameter[], LineParameter[], LineParameter[], LineParameter[], Angle)
Angle := deg(Angle)
*
* Display results
*
* Create line contours
get_metrology_object_result_contour (ResultContour, MetrologyHandle, 'all', 'all', 1.5)
intersection_lines (LineParameter[], LineParameter[], LineParameter[], LineParameter[], LineParameter[], LineParameter[], LineParameter[], LineParameter[], Row, Column, IsOverlapping1)
* Calculate the orientation of the two lines
line_orientation (LineParameter[], LineParameter[], LineParameter[], LineParameter[], Orientation1)
if (Orientation1 > )
Orientation1 := Orientation1 - rad()
endif
line_orientation (LineParameter[], LineParameter[], LineParameter[], LineParameter[], Orientation2)
*
* Visualize the angle between the lines
gen_circle_contour_xld (ContCircle, Row, Column, , Orientation1, Orientation2, 'positive', )
* 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, , rad())
* Display everything
dev_display (Image)
dev_set_line_width ()
dev_set_color ('yellow')
dev_display (Contour)
dev_display (Cross)
dev_set_line_width ()
dev_set_color ('green')
dev_display (ResultContour)
dev_set_color ('blue')
dev_display (ContCircle)
disp_message (WindowHandle, 'Angle = ' + Angle$'.5' + '°', 'window', , , 'black', 'true')
if (I < )
disp_continue_message (WindowHandle, 'black', 'true')
endif
stop ()
endfor
* Clean up memory
clear_metrology_model (MetrologyHandle)