此例子是通过测量助手测出工件的长、宽以及孔洞的直径
*关闭窗口
dev_close_window ()
*读取图片
read_image (Image, 'C:/Users/gwd/Desktop/测量工件.BMP')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
*显示提示信息
disp_continue_message (WindowHandle, 'black', 'true')
*测量助手
* Measure 01: Code generated by Measure 01
* Measure 01: Prepare measurement
AmplitudeThreshold := 40
RoiWidthLen2 := 27
set_system ('int_zooming', 'true')
* Measure 01: Coordinates for line Measure 01 [0]
LineRowStart_Measure_01_0 := 262.938
LineColumnStart_Measure_01_0 := 85.75
LineRowEnd_Measure_01_0 := 289.188
LineColumnEnd_Measure_01_0 := 978.25
* Measure 01: Convert coordinates to rectangle2 type
TmpCtrl_Row := 0.5*(LineRowStart_Measure_01_0+LineRowEnd_Measure_01_0)
TmpCtrl_Column := 0.5*(LineColumnStart_Measure_01_0+LineColumnEnd_Measure_01_0)
TmpCtrl_Dr := LineRowStart_Measure_01_0-LineRowEnd_Measure_01_0
TmpCtrl_Dc := LineColumnEnd_Measure_01_0-LineColumnStart_Measure_01_0
TmpCtrl_Phi := atan2(TmpCtrl_Dr, TmpCtrl_Dc)
TmpCtrl_Len1 := 0.5*sqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc)
TmpCtrl_Len2 := RoiWidthLen2
* Measure 01: Create measure for line Measure 01 [0]
* Measure 01: Attention: This assumes all images have the same size!
gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 1280, 960, 'nearest_neighbor', MsrHandle_Measure_01_0)
* Measure 01: Coordinates for line Measure 01 [1]
LineRowStart_Measure_01_1 := 172.938
LineColumnStart_Measure_01_1 := 550.75
LineRowEnd_Measure_01_1 := 581.688
LineColumnEnd_Measure_01_1 := 535.75
* Measure 01: Convert coordinates to rectangle2 type
TmpCtrl_Row := 0.5*(LineRowStart_Measure_01_1+LineRowEnd_Measure_01_1)
TmpCtrl_Column := 0.5*(LineColumnStart_Measure_01_1+LineColumnEnd_Measure_01_1)
TmpCtrl_Dr := LineRowStart_Measure_01_1-LineRowEnd_Measure_01_1
TmpCtrl_Dc := LineColumnEnd_Measure_01_1-LineColumnStart_Measure_01_1
TmpCtrl_Phi := atan2(TmpCtrl_Dr, TmpCtrl_Dc)
TmpCtrl_Len1 := 0.5*sqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc)
TmpCtrl_Len2 := RoiWidthLen2
* Measure 01: Create measure for line Measure 01 [1]
* Measure 01: Attention: This assumes all images have the same size!
gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 1280, 960, 'nearest_neighbor', MsrHandle_Measure_01_1)
* Measure 01: Coordinates for line Measure 01 [2]
LineRowStart_Measure_01_2 := 375.438
LineColumnStart_Measure_01_2 := 715.75
LineRowEnd_Measure_01_2 := 381.063
LineColumnEnd_Measure_01_2 := 855.75
* Measure 01: Convert coordinates to rectangle2 type
TmpCtrl_Row := 0.5*(LineRowStart_Measure_01_2+LineRowEnd_Measure_01_2)
TmpCtrl_Column := 0.5*(LineColumnStart_Measure_01_2+LineColumnEnd_Measure_01_2)
TmpCtrl_Dr := LineRowStart_Measure_01_2-LineRowEnd_Measure_01_2
TmpCtrl_Dc := LineColumnEnd_Measure_01_2-LineColumnStart_Measure_01_2
TmpCtrl_Phi := atan2(TmpCtrl_Dr, TmpCtrl_Dc)
TmpCtrl_Len1 := 0.5*sqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc)
TmpCtrl_Len2 := RoiWidthLen2
* Measure 01: Create measure for line Measure 01 [2]
* Measure 01: Attention: This assumes all images have the same size!
gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 1280, 960, 'nearest_neighbor', MsrHandle_Measure_01_2)
* Measure 01: ***************************************************************
* Measure 01: * The code which follows is to be executed once / measurement *
* Measure 01: ***************************************************************
* Measure 01: The image is assumed to be made available in the
* Measure 01: variable last displayed in the graphics window
copy_obj (Image, Image, 1, 1)
* Measure 01: Execute measurements
measure_pairs (Image, MsrHandle_Measure_01_0, 1, AmplitudeThreshold, 'all', 'all', Row1_Measure_01_0, Column1_Measure_01_0, Amplitude1_Measure_01_0, Row2_Measure_01_0, Column2_Measure_01_0, Amplitude2_Measure_01_0, Width_Measure_01_0, Distance_Measure_01_0)
measure_pairs (Image, MsrHandle_Measure_01_1, 1, AmplitudeThreshold, 'all', 'all', Row1_Measure_01_1, Column1_Measure_01_1, Amplitude1_Measure_01_1, Row2_Measure_01_1, Column2_Measure_01_1, Amplitude2_Measure_01_1, Width_Measure_01_1, Distance_Measure_01_1)
measure_pairs (Image, MsrHandle_Measure_01_2, 1, AmplitudeThreshold, 'all', 'all', Row1_Measure_01_2, Column1_Measure_01_2, Amplitude1_Measure_01_2, Row2_Measure_01_2, Column2_Measure_01_2, Amplitude2_Measure_01_2, Width_Measure_01_2, Distance_Measure_01_2)
*显示直线
disp_line (WindowHandle, Row1_Measure_01_0, Column1_Measure_01_0, Row2_Measure_01_0, Column2_Measure_01_0)
disp_line (WindowHandle, Row1_Measure_01_1, Column1_Measure_01_1, Row2_Measure_01_1, Column2_Measure_01_1)
disp_line (WindowHandle, Row1_Measure_01_2, Column1_Measure_01_2, Row2_Measure_01_2, Column2_Measure_01_2)
*显示测量的结果
*长---第二条边缘直线的列减去第一条边缘直线的列
*【以下以此类推】
m_width1:=Column2_Measure_01_0-Column1_Measure_01_0
m_width2:=Row2_Measure_01_1-Row1_Measure_01_1
m_width3:=Column2_Measure_01_2-Column1_Measure_01_2
disp_message (WindowHandle, '长 '+m_width1, 'image', 0, 0, 'red', 'true')
disp_message (WindowHandle, '宽 '+m_width2, 'image', 100, 0, 'red', 'true')
disp_message (WindowHandle, '直径 '+m_width3, 'image', 200, 0, 'red', 'true')
stop ()
*关闭句柄
close_measure (MsrHandle_Measure_01_0)
close_measure (MsrHandle_Measure_01_1)
close_measure (MsrHandle_Measure_01_2)
基于Halcon学习的测量拟合----实战【三】_BoomBiuBiu的博客-CSDN博客此例子是通过代码测出工件的长、宽总代码:*采用拟合的方法rgb1_to_gray (Image, GrayImage)threshold (GrayImage, Regions, 0, 174)connection (Regions, ConnectedRegions)select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 218807, 326147)*获得行列坐标area_center (Selecte..https://blog.csdn.net/BoomBiuBiu/article/details/123007758