计算内圆、外圆的半径、圆心
read_image (Image, 'F:/CYG/VirtualProject/halcon/Detect/01.bmp')
get_image_size (Image, Width, Height)
read_image (Image2, 'F:/CYG/VirtualProject/halcon/Coordinates.jpg')
cal_checkerboard (Image2, O, RM1, TM, Ratio)
dev_close_window ()
dev_open_window (0, 0, Width/3, Height/3, 'black', WindowHandle)
stop ()
*图像测量
threshold (Image, Regions, 100, 255)
connection (Regions, ConnectedRegions)
fill_up (ConnectedRegions, RegionFillUp)
select_shape (RegionFillUp, SelectedRegions, ['area','circularity'], 'and',\
[5000,0.9], [6500,1])
count_obj (SelectedRegions, Number)
if (Number # 19)
disp_message (WindowHandle, '检测目标不完整', 'window', 12, 12, 'black', 'true')
stop ()
endif
stop ()
*求外圆参数,
area_center (RegionFillUp, AreaW, RowW, ColumnW)//外圆圆心
tuple_sqrt (AreaW/3.1415, WR)//外圆半径
*像素坐标转实际坐标
create_matrix (2, 19, [RowW,ColumnW], MatrixIDW)
TrueWR := WR*Ratio//实际外圆半径
mult_matrix (RM1, MatrixIDW, 'AB', MatrixMultIDW)
get_full_matrix (MatrixMultIDW, ValuesW)
X := (ValuesW[0:18]+O[0])*Ratio
Y := (ValuesW[19:37]+O[1])*Ratio
*求内圆参数
difference (RegionFillUp, ConnectedRegions, RegionDifference)
area_center (RegionDifference, AreaN, RowN, ColumnN)//内圆圆心
tuple_sqrt (AreaN/3.1415, NR)//内圆半径
*像素坐标转实际坐标
create_matrix (2, 19, [RowN,ColumnN], MatrixIDN)
TrueNR := NR*Ratio//实际内圆半径
mult_matrix (RM1, MatrixIDN, 'AB', MatrixMultIDN)
get_full_matrix (MatrixMultIDN, ValuesN)
x := ((ValuesN[0:18]+O[0])*Ratio)$'.3f'
y := ((ValuesN[19:37]+O[1])*Ratio)$'.3f'
*最大最小值、平均厚度
smallest_circle (RegionFillUp, Row, Column, Radius)
inner_circle (RegionDifference, Row1, Column1, Radius1)
Max := (Radius - Radius1)* Ratio //圆环最大值
tuple_max (Max, Max1)
inner_circle (RegionFillUp, Row2, Column2, Radius2)
smallest_circle (RegionDifference, Row3, Column3, Radius3)
Min := (Radius2 - Radius3)*Ratio //圆环最小值
tuple_min (Min, Min1)
AverageR := TrueWR-TrueNR //平均厚度
dev_display (Image)
dev_disp_text ('圆环测量结果/mm', 'window', 0, 0, 'black', [], [])
disp_message (WindowHandle, [1:19], 'image', RowN-20, ColumnN-20, 'green', 'false')
dev_open_window (0, Width/3, 400, Height/3, 'black', WindowHandle2)
dev_disp_text ('序号', 'window', 0, 0, 'black', [], [])
disp_message (WindowHandle2, [1:19], 'window', 20, 5, 'black', 'true')
dev_disp_text ('外圆半径', 'window', 0, 40, 'black', [], [])
disp_message (WindowHandle2, TrueWR, 'window', 20, 40, 'black', 'true')
dev_disp_text ('内圆半径', 'window', 0, 100, 'black', [], [])
disp_message (WindowHandle2, TrueNR, 'window', 20, 100, 'black', 'true')
dev_disp_text ('圆环平均厚度', 'window', 0, 160, 'black', [], [])
disp_message (WindowHandle2, AverageR, 'window', 20, 170, 'black', 'true')
dev_disp_text ('圆环最大最小值', 'window', 330, 10, 'black', [], [])
disp_message (WindowHandle2, ['Max=','Min=']+[Max1,Min1], 'window', 330, 110, 'black', 'true')
dev_disp_text ('圆环X坐标', 'window', 0, 240, 'black', [], [])
disp_message (WindowHandle2, x, 'window', 20, 250, 'black', 'true')
dev_disp_text ('圆环Y坐标', 'window', 0, 300, 'black', [], [])
disp_message (WindowHandle2, y, 'window', 20, 310, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* dev_clear_window ()
set_display_font (WindowHandle, 20, 'mono', 'true', 'false')
disp_message (WindowHandle, '测量完成!', 'window', 0, 0, 'black', 'true')
stop ()
标定
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width/3, Height/3, 'black', WindowHandle)
dev_display (Image)
*提取坐标系
decompose3 (Image, R, G, B)
trans_from_rgb (R, G, B, H, S, V, 'hsv')
threshold (H, Regions, 240, 255)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'outer_radius', 'and', 50, 99999)
*select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 8000, 9999)//考虑到如果XY轴长度短
*//即坐标面积很小就不适用
*生成线、原点坐标
skeleton (SelectedRegions, Skeleton)
gen_contour_region_xld (Skeleton, Contours, 'border')
segment_contours_xld (Contours, ContoursSplit, 'lines_circles', 5, 4, 2)
select_contours_xld (ContoursSplit, SelectedContours, 'contour_length', 100, 200, -0.5, 0.5)
count_obj (SelectedContours, Number1)
if (Number1 < 4)
disp_message (WindowHandle, '筛选线段有误,请重新调整参数!', 'window', 12, 12, 'black', 'true')
endif
union_collinear_contours_xld (SelectedContours, UnionContours, 200, 1, 2, 0.1, 'attr_keep')
fit_line_contour_xld (UnionContours, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
intersection_lines (RowBegin[0], ColBegin[0], RowEnd[0], ColEnd[0], \
RowBegin[1], ColBegin[1], RowEnd[1], ColEnd[1], Row, Column, IsOverlapping)
O := [Row, Column]//原点坐标
*旋转矫正、计算变换矩阵
emphasize (G, ImageEmphasize, Width, Height, 1)
median_image (ImageEmphasize, ImageMedian, 'square', 5, 'mirrored')
threshold (ImageMedian, Regions1, 140, 255)
erosion_rectangle1 (Regions1, RegionErosion, 11, 11)//腐蚀处理,后面记得膨胀复原
connection (RegionErosion, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, ['area','rectangularity'], 'and', \
[6500,0.9], [7000,1])
count_obj (SelectedRegions1, Number)
if (Number = 0)
disp_message (WindowHandle, '标定板误差,请检查标定板类型!', 'window', 12, 12, 'black', 'true')
endif
select_obj (SelectedRegions1, ObjectSelected, Number/2)
smallest_rectangle2 (ObjectSelected, Row1, Column1, Phi, Length1, Length2)
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(360), HomMat2D)
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
dev_display (Image)
*求坐标转换矩阵
M := [HomMat2D[0],HomMat2D[1],HomMat2D[3],HomMat2D[4]]//旋转矩阵
create_matrix (2, 2, M, RM)//创建矩阵
invert_matrix (RM, 'general', 0, RM1)//矩阵求逆,旋转矩阵
create_matrix (2, 1, O, TM)//平移矩阵
*计算格子像素
dilation_rectangle1 (SelectedRegions1, RegionDilation, 11, 11)
area_center (RegionDilation, Area, Row2, Column2)
tuple_sqrt (Area, L)
tuple_mean (L, Mean)
*像素转实际物理尺寸
Ratio := 1/Mean
return ()
return ()