halcon之spoke拟合圆

主程序
read_image (Image, '2.bmp')
dev_display (Image)
dev_set_color ('red')
dev_get_window (WindowHandle)
* 卡尺个数
Elements:=10
* 检测高度
DetectHeight:=600
* 检测宽度
DetectWidth:=30
**************
* 一 ROI
**************
draw_spoke (Image, Regions, WindowHandle, Elements, DetectHeight, DetectWidth, \
ROIRows, ROICols, Direct)
**************
* 二 边缘查找
**************
* 极性
Transition:='negtive'
* 选择第几个
Select:='first'
* 平滑系数
Sigma:=2
* 阈值
Threshold:=20
spoke (Image, Regions, Elements, DetectHeight, DetectWidth, Sigma, Threshold, \
Transition, Select, ROIRows, ROICols, Direct, ResultRow, ResultColumn, ArcType)
**************
* 三 拟合圆
**************
* 最少拟合点数
ActiveNum:=5
pts_to_best_circle (Circle, ResultRow, ResultColumn, ActiveNum, ArcType, RowCenter, \
ColCenter, Radius, StartPhi, EndPhi, PointOrder)
dev_set_line_width (3)
dev_set_color ('blue')
dev_display (Circle)
draw_spoke
* disp_message (WindowHandle, '画圆,点击右键确认', 'window', 12, 12, 'black', 'true')
gen_empty_obj (Regions)
draw_circle (WindowHandle, Row, Column, Radius1)
gen_circle (Circle, Row, Column, Radius1)
gen_contour_region_xld (Circle, Contours, 'border')
get_contour_xld (Contours, Rows, Cols)
* tuple_length(Weights,Length1)
* if(Length1<4)
* disp_message(WindowHandle, '提示:点数太少,请重画', 'window', 32, 12, 'red', 'false')
* ROIRows:=[]
* ROICols:=[]
* return()
* endif
ROIRows:=Rows
ROICols:=Cols
* 拟合多边形
gen_contour_polygon_xld (Contour, ROIRows, ROICols)
* 拟合圆
fit_circle_contour_xld (Contour, 'algebraic', -1, 0, 0, 3, 2, RowC, ColumnC, Radius, StartPhi, EndPhi, PointOrder)
* 生成圆
gen_circle_contour_xld (ContCircle, RowC, ColumnC, Radius, StartPhi, EndPhi, PointOrder, 3)
* 计数
concat_obj (Regions, ContCircle, Regions)
* 获取圆的边缘轮廓
get_contour_xld (ContCircle, RowXLD, ColXLD)
dev_display (Image)
dev_display (ContCircle)
gen_cross_contour_xld (Cross, RowC, ColumnC, 60, 0.785398)
dev_display (Cross)
disp_message (WindowHandle, '2.远离圆心,画箭头确定边缘检测方向,点击右键确认', 'window', 12, 12, 'red', 'false')
* 画线
draw_line(WindowHandle, Row1, Column1, Row2, Column2)
* 计算圆到线开始的距离
distance_pp (RowC, ColumnC, Row1, Column1, DistanceStart)
* 计算圆到线结束的距离
distance_pp (RowC, ColumnC, Row2, Column2, DistanceEnd)
* 防呆
tuple_length (ColXLD, Length2)
if(Elements<3)
ROIRows :=[]
ROICols:=[]
disp_message (WindowHandle, '检测的边缘数量太少,请重新设置!', 'window', 52, 12, 'red', 'false')
return()
endif
* 卡尺个数
for i := 0 to Elements-1 by 1
if (RowXLD[0]==RowXLD[Length2-1])
tuple_int (1.0*Length2/(Elements)*(i), j)
else
tuple_int(1.0*Length2/(Elements-1)*(i), j)
endif
if(j>=Length2)
j:=Length2-1
continue
endif
RowE:=RowXLD[j]
ColE:=ColXLD[j]
if(DistanceStart>DistanceEnd)
tuple_atan2 (-RowE+RowC, ColE-ColumnC, ATan)
ATan:=rad(180)+ATan
Direct:='inner'
else
tuple_atan2 (-RowE+RowC, ColE-ColumnC, ATan)
Direct:='outer'
endif
gen_rectangle2_contour_xld (Rectangle1, RowE, ColE, ATan, DetectHeight/2, DetectWidth/2)
* 添加测量矩形卡尺
concat_obj(Regions, Rectangle1, Regions)
if(i==0)
RowL2:=RowE+DetectHeight/2*sin(-ATan)
RowL1:=RowE-DetectHeight/2*sin(-ATan)
ColL2:=ColE+DetectHeight/2*cos(-ATan)
ColL1:=ColE-DetectHeight/2*cos(-ATan)
gen_arrow_contour_xld (Arrow1, RowL1, ColL1, RowL2, ColL2, 25, 25)
* 添加测量矩形卡尺
concat_obj (Regions, Arrow1, Regions)
endif
endfor
return ()
spoke
* 选择
SelectOut := Select
* 极性
TransitionOut := Transition
* 获取图像大小
get_image_size (Image, Width, Height)
gen_empty_obj (Regions)
* 拟合y数组
ResultRow:=[]
* 拟合x数组
ResultColumn:=[]
* 生成多边形
gen_contour_polygon_xld (Contour, ROIRows, ROICols)
* 拟合多边形
fit_circle_contour_xld (Contour, 'algebraic', -1, 0, 0, 3, 2, RowC, ColumnC, Radius, StartPhi, EndPhi, PointOrder)
* 获取圆轮廓
gen_circle_contour_xld (ContCircle, RowC, ColumnC, Radius, StartPhi, EndPhi, PointOrder, 3)
* 联合对象
concat_obj (Regions, ContCircle, Regions)
* 获取轮廓点
get_contour_xld (ContCircle, RowXLD, ColXLD)
tuple_length(ColXLD, Length2)
if(Elements<3)
dev_disp_text ('检测的边缘数量太少,请重新设置!', 'window', RowXLD, ColXLD, 'black', [], [])
return ()
endif
* 遍历所有卡尺
for i := 0 to Elements-1 by 1
* 拟合圆
if (RowXLD[0]==RowXLD[Length2-1])
tuple_int (1.0*Length2/(Elements)*(i), j)
ArcType:='circle'
* 拟合圆弧
else
tuple_int (1.0*Length2/(Elements-1)*(i), j)
ArcType:='arc'
endif
if (j>=Length2)
j:=Length2-1
continue
endif
RowE:=RowXLD[j]
ColE:=ColXLD[j]
if(RowE>Height-1 or RowE<0 or ColE>Width or ColE<0)
continue
endif
* 方向
if(Direct=='inner')
tuple_atan2 (-RowE+RowC, ColE-ColumnC, ATan)
ATan:=rad(180)+ATan
else
tuple_atan2 (-RowE+RowC, ColE-ColumnC, ATan)
endif
* 生成矩形
gen_rectangle2_contour_xld (Rectangle1, RowE, ColE, ATan, DetectHeight/2, DetectWidth/2)
* 联合
concat_obj (Regions, Rectangle1, Regions)
* 显示方向
if(i==0)
RowL2:=RowE+DetectHeight/2*sin(-ATan)
RowL1:=RowE-DetectHeight/2*sin(-ATan)
ColL2:=ColE+DetectHeight/2*cos(-ATan)
ColL1:=ColE-DetectHeight/2*cos(-ATan)
gen_arrow_contour_xld (Arrow1, RowL1, ColL1, RowL2, ColL2, 25, 25)
concat_obj (Regions, Arrow1, Regions)
endif
* 生成测量矩形
gen_measure_rectangle2 (RowE, ColE, ATan, DetectHeight/2, DetectWidth/2, Width, Height, 'nearest_neighbor', MsrHandle_Measure)
* 确定查找参数
if (TransitionOut=='negtive')
TransitionOut := 'negtive'
elseif (TransitionOut=='positive')
TransitionOut := 'positive'
else
TransitionOut := 'all'
endif
if (SelectOut=='first')
SelectOut := 'first'
elseif (SelectOut=='last')
SelectOut := 'last'
else
SelectOut := 'all'
endif
* 卡尺测量
measure_pos (Image, MsrHandle_Measure, Sigma, Threshold, 'negative', SelectOut, RowEdge, ColEdge, Amplitude, Distance)
* 关闭测量句柄
close_measure (MsrHandle_Measure)
tRow:=0
tCol:=0
t:=0
* 计数是否卡出边缘点
tuple_length (RowEdge, Number)
* 卡出失败,则跳过
if (Number<1)
continue
endif
* 遍历所有边缘点,获取最大边缘点
for k := 0 to Number-1 by 1
if (abs(Amplitude[k])>t)
tRow:=RowEdge[k]
tCol:=ColEdge[k]
t:=abs(Amplitude[k])
endif
endfor
if (t>0)
ResultRow:=[ResultRow,tRow]
ResultColumn:=[ResultColumn,tCol]
endif
endfor
return ()
return ()
pts_to_best_circle
RowCenter:=0
ColCenter:=0
Radius:=0
gen_empty_obj (Circle)
tuple_length (Cols, Length)
if(Length>=ActiveNum and ActiveNum >2)
if (ArcType=='circle')
gen_contour_polygon_xld (Contour, [Rows,Rows[0]], [Cols,Cols[0]])
else
gen_contour_polygon_xld (Contour, Rows, Cols)
endif
fit_circle_contour_xld (Contour, 'algebraic', -1, 0, 0, 3, 2, RowCenter, ColCenter, Radius, StartPhi, EndPhi, PointOrder)
tuple_length (StartPhi, Length1)
if (Length1<1)
return ()
endif
if(ArcType=='arc')
gen_circle_contour_xld (Circle, RowCenter, ColCenter, Radius, StartPhi, EndPhi, PointOrder, 1)
else
StartPhi:=0
EndPhi:=rad(360)
gen_circle_contour_xld (Circle, RowCenter, ColCenter, Radius, StartPhi, EndPhi, PointOrder, 1)
endif
endif
return ()
return ()