参考例程
一维测量:fuse.hdev、measure_pin.hdev、pm_measure_board.hdev、measure_arc.hdev 测量弧形、measure_fill_level.hdev
边缘提取(亚像素):circles.hdev、measure_metal_part.hdev
close_contour_gaps.hdev、union_cotangential_contours_xld.hdev 联合过程、fit_ellipse_contour_xld.hdev
模板匹配(形状):matching_multi_channel_yogurt.hdev
一般流程
1.采集
2.预处理
去燥:
中值滤波:Media_Image 、均值滤波·:mean_image 、高斯滤波:guss_image
增强:
scale_image() 黑的地方更黑,亮的地方更亮。
gray_dilation ()
gray_erosion ()
gray_opening() 先腐后膨 灰度值最高的值作为新值,有使图像变亮的作用。
gray_closing() 先腐后膨 灰度值最低的值作为新值,有使图像变暗的作用。
emphasize() res := round((orig - mean) * Factor) + orig mean代表先对原图进行mean_image后的图像对应的灰度值,orig 代表每幅图对应的灰阶值 ,res代表输出图像的灰阶值。
3.边缘提取
edges_sub_pix ()
threshold_sub_pix ()
4.联合分割
union_adjacent_contours_xld () 邻近
union_cocircular_contours_xld () 共圆
union_collinear_contours_ext_xld () 共线
5.拟合
fit_circle_contour_xld ()
fit_line_contour_xld ()
6.计算显示
主要算子
边缘 像素变化最大处(一阶导数最大/二阶导数为0处)由亮到暗为负,由暗到亮为正
XLD: eXtended Line Descriptions 亚像素轮廓
threshold_sub_pix(Image:Border:Threshold)提取图像的XLD
select_shape_xld()选择特定形状特征要求的XLD轮廓或多边形
select_contours_xld()
area_center_xld()
shape_trans_xld()
segment_contours_xld()
gen_measure_rectangle2 ()
measure_pairs ()
close_measure (MeasureHandle)
measure_pos ()
图像边界 edges_image() 图像边缘提取
区域边界 boundary()
区域放大 坐标不变
dev_set_part(row1,colum1,row2,colum2)
dev_displauy(image)
亚像素边缘提取 得到亚像素轮廓
edges_sub_pix (ImageReduced, Edges, ‘canny’, 2, 20, 60)
threshold_sub_pix (ImageReduced, Edges, ‘canny’, 2, 20, 60)
segment_contours_xld (Edges, ContoursSplit, ‘lines_circles’, 5, 4, 3)
region_to_bin (Rectangle, BinImage, 130, 100, 120, 130)
fit_ellipse_contour_xld ()
*利用测量工具实现
*1采集图像
dev_close_window ()
read_image (Image, 'D:/Documents/Desktop/halcon/9 实战项目图片/测量工件.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')
AmplitudeThreshold := 40
RoiWidthLen2 := 24.5
set_system ('int_zooming', 'true')
* Measure 05: Coordinates for line Measure 05 [0]
LineRowStart_Measure_05_0 := 259.288
LineColumnStart_Measure_05_0 := 95.6157
LineRowEnd_Measure_05_0 := 285.212
LineColumnEnd_Measure_05_0 := 953.096
* Measure 05: Convert coordinates to rectangle2 type
TmpCtrl_Row := 0.5*(LineRowStart_Measure_05_0+LineRowEnd_Measure_05_0)
TmpCtrl_Column := 0.5*(LineColumnStart_Measure_05_0+LineColumnEnd_Measure_05_0)
TmpCtrl_Dr := LineRowStart_Measure_05_0-LineRowEnd_Measure_05_0
TmpCtrl_Dc := LineColumnEnd_Measure_05_0-LineColumnStart_Measure_05_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 05: Create measure for line Measure 05 [0]
* Measure 05: 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_05_0)
* Measure 05: Coordinates for line Measure 05 [1]
LineRowStart_Measure_05_1 := 165.564
LineColumnStart_Measure_05_1 := 458.549
LineRowEnd_Measure_05_1 := 578.351
LineColumnEnd_Measure_05_1 := 436.614
* Measure 05: Convert coordinates to rectangle2 type
TmpCtrl_Row := 0.5*(LineRowStart_Measure_05_1+LineRowEnd_Measure_05_1)
TmpCtrl_Column := 0.5*(LineColumnStart_Measure_05_1+LineColumnEnd_Measure_05_1)
TmpCtrl_Dr := LineRowStart_Measure_05_1-LineRowEnd_Measure_05_1
TmpCtrl_Dc := LineColumnEnd_Measure_05_1-LineColumnStart_Measure_05_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 05: Create measure for line Measure 05 [1]
* Measure 05: 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_05_1)
* Measure 05: Coordinates for line Measure 05 [2]
LineRowStart_Measure_05_2 := 384.919
LineColumnStart_Measure_05_2 := 693.858
LineRowEnd_Measure_05_2 := 374.949
LineColumnEnd_Measure_05_2 := 861.366
* Measure 05: Convert coordinates to rectangle2 type
TmpCtrl_Row := 0.5*(LineRowStart_Measure_05_2+LineRowEnd_Measure_05_2)
TmpCtrl_Column := 0.5*(LineColumnStart_Measure_05_2+LineColumnEnd_Measure_05_2)
TmpCtrl_Dr := LineRowStart_Measure_05_2-LineRowEnd_Measure_05_2
TmpCtrl_Dc := LineColumnEnd_Measure_05_2-LineColumnStart_Measure_05_2
TmpCtrl_Phi := atan2(TmpCtrl_Dr, TmpCtrl_Dc)
TmpCtrl_Len1 := 0.5*sqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc)
TmpCtrl_Len2 := RoiWidthLen2
gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 1280, 960, 'nearest_neighbor', MsrHandle_Measure_05_2)
*测量边缘对
measure_pairs (Image, MsrHandle_Measure_05_0, 1, AmplitudeThreshold, 'all', 'all', Row1_Measure_05_0, Column1_Measure_05_0, Amplitude1_Measure_05_0, Row2_Measure_05_0, Column2_Measure_05_0, Amplitude2_Measure_05_0, Width_Measure_05_0, Distance_Measure_05_0)
measure_pairs (Image, MsrHandle_Measure_05_1, 1, AmplitudeThreshold, 'all', 'all', Row1_Measure_05_1, Column1_Measure_05_1, Amplitude1_Measure_05_1, Row2_Measure_05_1, Column2_Measure_05_1, Amplitude2_Measure_05_1, Width_Measure_05_1, Distance_Measure_05_1)
measure_pairs (Image, MsrHandle_Measure_05_2, 1, AmplitudeThreshold, 'all', 'all', Row1_Measure_05_2, Column1_Measure_05_2, Amplitude1_Measure_05_2, Row2_Measure_05_2, Column2_Measure_05_2, Amplitude2_Measure_05_2, Width_Measure_05_2, Distance_Measure_05_2)
*显示直线及文字数据
disp_line (WindowHandle, Row1_Measure_05_0, Column1_Measure_05_0, Row2_Measure_05_0, Column2_Measure_05_0)
disp_line (WindowHandle, Row1_Measure_05_1, Column1_Measure_05_1, Row2_Measure_05_1, Column2_Measure_05_1)
disp_line (WindowHandle, Row1_Measure_05_2, Column1_Measure_05_2, Row2_Measure_05_2, Column2_Measure_05_2)
m_width1:=Column2_Measure_05_0-Column1_Measure_05_0
m_width2:=Row2_Measure_05_1-Row1_Measure_05_1
m_width3:=Column2_Measure_05_2-Column1_Measure_05_2
disp_message (WindowHandle, '长 ' + m_width1, 'image', 0, 0, 'red', 'false')
disp_message (WindowHandle, ' 宽 ' + m_width2, 'image', 100, 0, 'red', 'false')
disp_message (WindowHandle, '直径 ' + m_width3, 'image', 200, 0, 'red', 'false')
close_measure (MsrHandle_Measure_05_0)
close_measure (MsrHandle_Measure_05_1)
close_measure (MsrHandle_Measure_05_2)
*
*识别拟合求距离
rgb1_to_gray (Image, Imagegray)
*2预处理(定位)
dev_set_draw ('fill')
threshold (Imagegray, Regions, 0, 114)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 243981, 296759)
area_center (SelectedRegions, AreaRegion, RowCenterRegion, ColumnCenterRegion)
orientation_region (SelectedRegions, OrientationRegion)
dev_display (SelectedRegions)
dev_display (Image)
disp_message (WindowHandle, 'Center Row: ' + RowCenterRegion$'.5', 'window', 20, 10, 'blue', 'false')
disp_message (WindowHandle, 'Area: ' + AreaRegion + ' pixel', 'window', 20, 300, 'blue', 'false')
disp_message (WindowHandle, 'Center Column: ' + ColumnCenterRegion$'.5', 'window', 60, 10, 'blue', 'false')
disp_message (WindowHandle, 'Orientation: ' + OrientationRegion$'.3' + ' rad', 'window', 60, 300, 'blue', 'false')
disp_cross (WindowHandle, RowCenterRegion, ColumnCenterRegion, 15, 0)
disp_arrow (WindowHandle, RowCenterRegion, ColumnCenterRegion, RowCenterRegion - 60 * sin(OrientationRegion), ColumnCenterRegion + 60 * cos(OrientationRegion), 2)
*intersection_lines
*3提取边缘
edges_sub_pix (Imagegray, Edges, 'canny', 0.6, 20, 40)
select_shape_xld (Edges, SelectedXLD, ['height','width'], 'and', [100,600], [500,1000])
*分割轮廓
segment_contours_xld(SelectedXLD, ContoursSplit, 'lines_circles', 5, 4, 2)
dev_set_colored (12)
select_shape_xld (ContoursSplit, SelectedXLD1, ['height','width'], 'and', [200,0], [400,30])
sort_contours_xld(SelectedXLD1, SortedContour1, 'character', 'true', 'row')
fit_line_contour_xld(SortedContour1, 'tukey', -1, 0, 5, 2, RowBegin0, ColBegin0, RowEnd0, ColEnd0, Nr, Nc, Dist)
gen_contour_polygon_xld(line1, [RowBegin0[0],RowEnd0[0]], [ColBegin0[0],ColEnd0[0]])
gen_contour_polygon_xld(line2, [RowBegin0[1],RowEnd0[1]], [ColBegin0[1],ColEnd0[1]])
select_shape_xld (ContoursSplit, SelectedXLD1_2, ['height','width'], 'and', [0,600], [50,1000])
sort_contours_xld(SelectedXLD1_2, SortedContour2, 'character', 'true', 'row')
fit_line_contour_xld(SortedContour2, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr1, Nc1, Dist1)
gen_contour_polygon_xld(line3, [RowBegin1[0],RowEnd1[0]], [ColBegin1[0],ColEnd1[0]])
gen_contour_polygon_xld(line4, [RowBegin1[1],RowEnd1[1]], [ColBegin1[1],ColEnd1[1]])
*显示
distance_pl(RowBegin0[0], ColBegin0[0], RowBegin0[1], ColBegin0[1], RowEnd0[1], ColEnd0[1], Distance1)
disp_message (WindowHandle, '竖直边的距离 ' +Distance1, 'window',300, 10, 'blue', 'false')
distance_pl(RowBegin1[0], ColBegin1[0], RowBegin1[1], ColBegin1[1], RowEnd1[1], ColEnd1[1], Distance1)
disp_message (WindowHandle, '水平边的距离 ' +Distance1, 'window',360, 10, 'blue', 'false')
*拟合圆
edges_sub_pix(Imagegray, Edges3, 'canny', 1, 20, 30)
select_shape_xld (Edges3, SelectedXLD2, ['width','circularity'], 'and', [34.86,0.12752], [640.37,1])
union_cocircular_contours_xld (SelectedXLD2, UnionContours, 1.6, 0.9, 0.9, 30, 40, 40, 'true', 1)
fit_circle_contour_xld(UnionContours, 'algebraic', -1, 0, 0, 3, 2, Row1, Column1, Radius1, StartPhi1, EndPhi1, PointOrder1)
gen_circle_contour_xld(ContCircle1, Row1, Column1, Radius1, 0, 6.28318, 'positive', 1)
*显示
distance_pl(Row1, Column1, RowBegin0[0], ColBegin0[0], RowEnd0[0], ColEnd0[0], DistanceR_left)
disp_message (WindowHandle, '圆心到左边直线的距离 ' +DistanceR_left+'半径'+Radius1, 'window',400, 10, 'blue', 'false')
distance_pp(Row1, Column1, RowEnd1[0], ColEnd1[0],DistanceR_upendpoint)
angle_ll(RowBegin0[1],ColBegin0[1],RowEnd0[1],ColEnd0[1],RowBegin1[0],ColBegin1[0],RowEnd1[0],ColEnd1[0],m_angle)
disp_message (WindowHandle, '圆心到上边直线末尾点(左边点)的距离 ' +DistanceR_upendpoint+'半径'+Radius1, 'window',460, 10, 'blue', 'false')
read_image (Image, 'D:/Documents/Desktop/halcon/9 实战项目图片/测量同轴度.jpg')
*图像增强
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
emphasize (Image, ImageEmphasize, 7, 7,2)
rgb1_to_gray (ImageEmphasize, GrayImage)
threshold (GrayImage, Regions, 61, 255)
fill_up (Regions, RegionFillUp)
connection (RegionFillUp, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 642589, 772045)
*膨胀的到外圈圆
dilation_circle (SelectedRegions, RegionDilation, 30)
shape_trans (RegionDilation, RegionTrans, 'convex')
dev_display (Image)
dev_display (RegionDilation)
*区域转为轮廓 拟合 (画圆) 显示
gen_contour_region_xld (RegionTrans, Contours, 'border')
fit_circle_contour_xld (Contours, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.0)
dev_display (GrayImage)
*求内圆
threshold (GrayImage, Regions1, 0, 29)
connection (Regions1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 61428.6, 84285.7)
fill_up (SelectedRegions1, RegionFillUp1)
shape_trans (RegionFillUp1, RegionTrans1, 'outer_circle')
gen_contour_region_xld (RegionTrans1, Contours1, 'border')
fit_circle_contour_xld (Contours1, 'algebraic', -1, 0, 0, 3, 2, Row1, Column1, Radius1, StartPhi1, EndPhi1, PointOrder1)
gen_circle_contour_xld (ContCircle1, Row1, Column1, Radius1, 0, rad(360), 'positive', 1)
dev_display (Image)
dev_display (ContCircle)
dev_display (ContCircle1)
*m_yianxinpp := sqrt(pow(Row1-Row,2)+pow(Column1-Column,2))
distance_pp (Row1, Column1, Row, Column, Distance)