Halcon视觉正确找到角点位置-@龙熙视觉培训李杰

连续图像采集*****
*Image Acquisition 01: Code generated by Image Acquisition 01
R:=[]
C:=[]
ImageFiles := []
ImageFiles[0] := ‘./MPic/pic02.png’
* Reopen the window appropriately
read_image (halconImage, ImageFiles[0])
get_image_size (halconImage, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, ‘black’, WindowHandle)
dev_display (halconImage)
dev_set_draw (‘margin’)

Halcon视觉正确找到角点位置-@龙熙视觉培训李杰_第1张图片

****测试窗口跟随被测物体对齐
for Index := 1 to 3 by 1

  • Index := 3
    read_image (Image, ‘./MPic/pic’+Index$‘02d’+’.png’)
    rotate_image(Image,halconImage, 10, ‘constant’)
    *生成矩形
  • gen_rectangle2 (Rectangle, Rowq, Columnq, Phiq, Length1q, Length2q)
    *剪切矩形区域图像 ROI找出感兴趣区域
  • reduce_domain (halconImage, Rectangle, ImageReduced1)
    *二值化找出目标区域
    binary_threshold ( halconImage, Region, ‘max_separability’, ‘light’, UsedThreshold)
    *开运算
    opening_circle (Region, RegionOpening, 9)

*连通域
connection (RegionOpening, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions2, ‘area’, ‘and’, 20491, 1613949)

Halcon视觉正确找到角点位置-@龙熙视觉培训李杰_第2张图片

*根据面积和矩形度 筛选
select_shape (ConnectedRegions, SelectedRegions, [‘area’,‘rectangularity’], ‘and’, [20000,0.1], [50000000,1])
select_shape (SelectedRegions, SelectedRegions1, ‘area’, ‘and’, 0, 1)
*找出面积最大区域
select_shape_std(SelectedRegions,SelectedImage,‘max_area’,30)

  • 判断下是否找到区域
    dilation_circle(SelectedImage,RegionDilation,10)
    erosion_circle(SelectedImage,RegionErosion,10)
    Halcon视觉正确找到角点位置-@龙熙视觉培训李杰_第3张图片

difference(RegionDilation,RegionErosion,RegionDifference)
*从原图里面把相交的矩形区域扣出来
reduce_domain (halconImage, RegionDifference, ImageReduced)
smallest_rectangle2(RegionDifference, Row16, Column16, Phi3, Length1, Length2)
gen_rectangle2(Rectangle1, Row16, Column16, Phi3, Length1, Length2)
dev_clear_window()
dev_display(halconImage)
dev_set_color(‘blue’)
dev_display(Rectangle1 )

*获取4个顶点
tuple_cos (Phi3, Cos)
tuple_sin (Phi3, Sin)
dev_set_color(‘red’)

Rows:=[]
Columns:=[]
右上顶点
a:= -Length1
Cos - Length2Sin
b := -Length1
Sin + Length2Cos
gen_cross_contour_xld(Cross, Row16-b, Column16+a, 6, Phi3)
右下顶点
c := Length1
Cos - Length2
Sin
d := Length1Sin + Length2Cos
gen_cross_contour_xld(Cross, Row16-d, Column16+c, 6, Phi3)
左下顶点
e:= Length1
Cos + Length2Sin
f := Length1
Sin - Length2Cos
gen_cross_contour_xld(Cross, Row16-f, Column16+e, 6, Phi3)
左上顶点
g := -Length1
Cos + Length2
Sin
h := -Length1Sin - Length2Cos
gen_cross_contour_xld(Cross, Row16-h, Column16+g, 6, Phi3)

Rows:=[Rows,Row16-b]
Rows:=[Rows,Row16-d]
Rows:=[Rows,Row16-f]
Rows:=[Rows,Row16-h]
Columns:=[Columns,Column16+a]
Columns:=[Columns,Column16+c]
Columns:=[Columns,Column16+e]
Columns:=[Columns,Column16+g]

distance_pp(Rows, Columns, [1024,1024,1024,1024], [0,0,0,0], Distance)

  • tuple_sort(Distance,Sorted)
    tuple_sort_index (Distance, Indices)
    smallD:=Distance[Indices[0]]
    newRow:=Rows[Indices[0]]
    newColumn:=Columns[Indices[0]]

*获得矩形和感兴趣区域
gen_rectangle2(Rectangle2,newRow, newColumn, Phi3, 150, 150)
*从原图里面把矩形区域扣出来
reduce_domain (ImageReduced, Rectangle2, ImageReduced1)

*边缘检测 edgessubpix
edges_sub_pix (ImageReduced1, Edges, ‘canny’, 1, 1, 30)
select_shape_xld (Edges, SelectedXLD, ‘contlength’, ‘and’,20 , 2000)
*分割线

Halcon视觉正确找到角点位置-@龙熙视觉培训李杰_第4张图片

segment_contours_xld (SelectedXLD, ContoursSplit, ‘lines’, 5, 1, 0.5)
select_shape_xld (ContoursSplit, SelectedXLD1, ‘contlength’, ‘and’,20 , 1000)
union_collinear_contours_xld (SelectedXLD1, UnionContours,100, 20, 2, 0.1, ‘attr_keep’)
count_obj (UnionContours, NumberLine)
*清空,写入数组
gen_empty_obj (Lines)

  • 第1 部分 选择垂直线
    select_contours_xld (UnionContours, LinesVerticalS, ‘direction’, rad(70), rad(110), 0, 0)
  • Get_self_contour (LinesVerticalS, ObjectSelected, Contour1)
    fit_line_contour_xld (LinesVerticalS, ‘tukey’, -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr, Nc, Dist)
    gen_contour_polygon_xld (Contour1, [RowBegin1,RowEnd1],
    [ColBegin1,ColEnd1])

Halcon视觉正确找到角点位置-@龙熙视觉培训李杰_第5张图片

dev_clear_window()
dev_display(halconImage)

dev_set_color(‘red’)
dev_set_line_width(1)
dev_display(Contour1)

  • tuple_max(Length,Max)
  • select_shape_xld(LinesVertical,oneLineV,‘contlength’,‘and’,Max,Max)

dev_set_color(‘blue’)

  • 第2 部分 选择水平线

select_contours_xld (UnionContours, LinesHorizontalS, ‘direction’, rad(-30), rad(30), 0, 0)

  • Get_self_contour (LinesHorizontalS, ObjectSelected, Contour2)
    fit_line_contour_xld (LinesHorizontalS, ‘tukey’, -1, 0, 5, 2, RowBegin2, ColBegin2, RowEnd2, ColEnd2, Nr, Nc, Dist)
    gen_contour_polygon_xld (Contour2, [RowBegin2,RowEnd2], [ColBegin2,ColEnd2])
    dev_open_window (0, 0, Width/2, Height/2, ‘black’, WindowHandle1)
    dev_display (halconImage)
    dev_set_color(‘green’)
    dev_display (Contour1)
    dev_set_color(‘red’)
    dev_display (Contour2)
    dev_set_color(‘yellow’)

intersection_lines (RowBegin1, ColBegin1, RowEnd1, ColEnd1, RowBegin2, ColBegin2, RowEnd2, ColEnd2, Row, Column, IsOverlapping1)

  • gen_cross_contour_xld (Cross, Row, Column, 30, 0.785398)

*2.2 只有一个点的时候输出
tuple_length(Row,RowLength)
if (RowLength=1)
gen_cross_contour_xld (Cross, Row, Column, 30, 2)
stop()
endif

  • endif
  • endif

endfor

你可能感兴趣的:(机器视觉,halcon,机器学习,深度学习)