Halcon取直线

原图,类似这样的图就行Halcon取直线_第1张图片

Halcon取直线_第2张图片

代码

read_image (Image, 'E:/gongsi/Project/BatterySun/test8.png')
rgb1_to_gray (Image, GrayImage)
threshold_sub_pix (GrayImage, Regions,200)
select_shape_xld (Regions, SelectedXLD, 'contlength', 'and', 1, 99999)
count_obj (SelectedXLD, Number)
segment_contours_xld (SelectedXLD, ContoursSplit, 'lines', 5, 4, 2)
count_obj (ContoursSplit, Number1)
union_collinear_contours_xld (ContoursSplit, UnionContours1, 20, 1, 2, 0.1, 'attr_keep')
select_contours_xld (UnionContours1, SelectedContours1, 'contour_length', 50, 9999, -0.5, 0.5)
area_center_points_xld(SelectedContours1, Area, Row, Column)//获取线的中心与长度
fit_line_contour_xld (SelectedContours1, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr1, Nc1, Dist1)//百度该算子

for i := 0 to Number1-1 by 1 //判断横竖
  if(Nr1[i]>0.5)
      Area[i]:=-1
  else 
      j:=i
 endif   
endfor

效果
Halcon取直线_第3张图片

你可能感兴趣的:(图像)