Halcon——点胶机胶水路径应用(1)

胶水路径应用

  • 1. 项目要求
  • 2. 算法分析
  • 3. 效果展示
  • 4. halcon代码
  • 5. 资源下载

1. 项目要求

  • 如下图,点胶机想在红色箭头路径上点胶,电池使用背光源定位。
  • 要求:操作简单(尝试不使用模板),可以设置胶水路径的偏移量。
    Halcon——点胶机胶水路径应用(1)_第1张图片

2. 算法分析

  • 第一种方法想到的是使用模板匹配找到电池位置,然后检测直线的区域使用仿射变换跟随模板移动,找到四条边,求出四个交点。胶水路径也是直线,可以使用边缘直线偏移得到。
  • 第二种方法第一种不同的是,不再使用模板匹配进行粗定位,而是使用Blob分析直接确定抓取直线的大致区域,然后抓直线求交点,后面做法相同。
  • 这里我采用了第二种方法,代码比较多,但是写成软件后,操作起来步骤会少。

3. 效果展示

  • 由于暂时没能拿到原图,所以自己用画图软件花了几张图来测试。

原图
Halcon——点胶机胶水路径应用(1)_第2张图片
找到电池主体
Halcon——点胶机胶水路径应用(1)_第3张图片
找到放置抓边工具的位置
Halcon——点胶机胶水路径应用(1)_第4张图片
抓边
Halcon——点胶机胶水路径应用(1)_第5张图片
计算胶水路径
Halcon——点胶机胶水路径应用(1)_第6张图片
带有角度1
Halcon——点胶机胶水路径应用(1)_第7张图片
带有角度2
Halcon——点胶机胶水路径应用(1)_第8张图片

4. halcon代码

list_files ('', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    rgb1_to_gray (Image, GrayImage)
    
    dev_set_line_width (2)
    * 1. 分割电池主体部分
    GetCellBody (GrayImage, Rectangle)
    
    dev_display (GrayImage)
    dev_display (Rectangle)
    stop()
    
    * 2. 计算角点 左上角点作为起点,逆时针排列
    GetCornerPoints (GrayImage, Rectangle, CornerRow, CornerCol)
    
    gen_cross_contour_xld (Cross, CornerRow, CornerCol, 150, 0)
    dev_display (GrayImage)
    dev_display (Cross)
    stop()
    
    
    * 3. 边缘测量 求交点
    Elements:=20
    DetectHeight:=150
    DetectWidth:=5
    Sigma:=3
    Threshold:=30
    Transition:='all'
    Select:='all'
    ActiveNum:=5
    
    GetIntersetcionPoints (GrayImage, Line1, Line2, Line3, Line4, \
                           Elements, DetectHeight, DetectWidth, Sigma, Threshold, Transition, Select,\
                           CornerRow, CornerCol, ActiveNum, Row1, Column1, Row2, Column2, Row3, Column3, Row4, Column4)
    
    
    dev_display (GrayImage)
    dev_set_color ('green')
    dev_display (Line1)
    dev_display (Line2)
    dev_display (Line3)
    dev_display (Line4)
    stop()

    * 4. 将边缘线沿着垂线方向向电芯内侧平移
    TranslationX:=150  //控制点胶线径向方向的移动距离
    Avoid1:=100        //控制点胶线切向方向的移动距离1
    Avoid2:=300        //控制点胶线切向方向的移动距离2
    
    GetGluePoints (Line1, Line2, Glue1, Glue2, TranslationX, Avoid1, Avoid2, Row1, Column1, Row2, Column2, Row3, Column3, Row4, Column4, RowNew1, ColumnNew1, RowNew2, ColumnNew2, RowNew3, ColumnNew3, RowNew4, ColumnNew4)
    
    dev_display (GrayImage)
    dev_set_color ('red')
    dev_display (Glue1)
    dev_set_color ('green')
    dev_display (Glue2)
    stop()
endfor
  • GetCellBody
threshold (GrayImage, Regions1, 0, 83)
connection (Regions1, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 2.74491e+006, 5e+006)
opening_rectangle1 (SelectedRegions, RegionOpening, 400, 400)
smallest_rectangle2 (RegionOpening, Row, Column, Phi, Length1, Length2)
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
return ()
  • GetCornerPoints
get_image_size (GrayImage, Width, Height)
gen_image_const (Image1, 'byte', Width, Height)
paint_region (Image1, Image1, ImageResult, 255, 'fill')
paint_region (Rectangle, ImageResult, ImageResult, 0, 'fill')
points_foerstner (ImageResult, 1, 2, 3, 200, 0.1, 'gauss', 'false', \
                  RowJunctions, ColumnJunctions, CoRRJunctions, CoRCJunctions,\
                  CoCCJunctions, RowArea, ColumnArea, CoRRArea, CoRCArea, CoCCArea)

* 角点排序 检测角点是从上向下逐行检测的 排序是从左上角点开始,从左到右,从上到下
CornerRow:=[]
CornerCol:=[]
if (ColumnJunctions[0]<ColumnJunctions[1])
    CornerRow[0]:=RowJunctions[0]
    CornerCol[0]:=ColumnJunctions[0]
    CornerRow[1]:=RowJunctions[1]
    CornerCol[1]:=ColumnJunctions[1]
else
    CornerRow[0]:=RowJunctions[1]
    CornerCol[0]:=ColumnJunctions[1]
    CornerRow[1]:=RowJunctions[0]
    CornerCol[1]:=ColumnJunctions[0]
endif

if (ColumnJunctions[2]<ColumnJunctions[3])
    CornerRow[2]:=RowJunctions[2]
    CornerCol[2]:=ColumnJunctions[2]
    CornerRow[3]:=RowJunctions[3]
    CornerCol[3]:=ColumnJunctions[3]
else
    CornerRow[2]:=RowJunctions[3]
    CornerCol[2]:=ColumnJunctions[3]
    CornerRow[3]:=RowJunctions[2]
    CornerCol[3]:=ColumnJunctions[2]
endif
return ()
  • GetIntersetcionPoints
rake (GrayImage, Regions, Elements, DetectHeight, DetectWidth, Sigma, Threshold, Transition, Select, CornerRow[0], CornerCol[0], CornerRow[1], CornerCol[1], ResultRow, ResultColumn)
pts_to_best_line (Line1, ResultRow, ResultColumn, ActiveNum, Row01, Column01, Row10, Column10)

rake (GrayImage, Regions, Elements, DetectHeight, DetectWidth, Sigma, Threshold, Transition, Select, CornerRow[2], CornerCol[2], CornerRow[3], CornerCol[3], ResultRow, ResultColumn)
pts_to_best_line (Line2, ResultRow, ResultColumn, ActiveNum, Row23, Column23, Row32, Column32)

rake (GrayImage, Regions, Elements, DetectHeight, DetectWidth, Sigma, Threshold, Transition, Select, CornerRow[0], CornerCol[0], CornerRow[2], CornerCol[2], ResultRow, ResultColumn)
pts_to_best_line (Line3, ResultRow, ResultColumn, ActiveNum, Row02, Column02, Row20, Column20)

rake (GrayImage, Regions, Elements, DetectHeight, DetectWidth, Sigma, Threshold, Transition, Select, CornerRow[1], CornerCol[1], CornerRow[3], CornerCol[3], ResultRow, ResultColumn)
pts_to_best_line (Line4, ResultRow, ResultColumn, ActiveNum, Row13, Column13, Row31, Column31)

intersection_lines (Row01, Column01, Row10, Column10, Row02, Column02, Row20, Column20, Row1, Column1, IsOverlapping)
intersection_lines (Row01, Column01, Row10, Column10, Row13, Column13, Row31, Column31, Row2, Column2, IsOverlapping)
intersection_lines (Row23, Column23, Row32, Column32, Row02, Column02, Row20, Column20, Row3, Column3, IsOverlapping)
intersection_lines (Row23, Column23, Row32, Column32, Row13, Column13, Row31, Column31, Row4, Column4, IsOverlapping)

gen_cross_contour_xld (Cross1, Row1, Column1, 96, 0)
gen_cross_contour_xld (Cross2, Row2, Column2, 96, 0)
gen_cross_contour_xld (Cross3, Row3, Column3, 96, 0)
gen_cross_contour_xld (Cross4, Row4, Column4, 96, 0)
return ()
  • GetGluePoints
* 计算边缘线Line1的角度Phi1
gen_region_contour_xld (Line1, Region, 'filled')
orientation_region (Region, Phi1)

if (Phi1>rad(90))
    Phi1:=Phi1-rad(180)
endif

* Cross1平移
RowNew1:=Row1+TranslationX*cos(Phi1)
ColumnNew1:=Column1+TranslationX*sin(Phi1)
RowNew1:=RowNew1-Avoid1*sin(Phi1)
ColumnNew1:=ColumnNew1+Avoid1*cos(Phi1)
gen_cross_contour_xld (CrossNew1, RowNew1, ColumnNew1, 96, 0)
dev_display (CrossNew1)

* Cross2平移
RowNew2:=Row2+TranslationX*cos(Phi1)
ColumnNew2:=Column2+TranslationX*sin(Phi1)
RowNew2:=RowNew2+Avoid2*sin(Phi1)
ColumnNew2:=ColumnNew2-Avoid2*cos(Phi1)
gen_cross_contour_xld (CrossNew2, RowNew2, ColumnNew2, 96, 0)
dev_display (CrossNew2)


* 计算边缘线Line2的角度Phi2
gen_region_contour_xld (Line2, Region, 'filled')
orientation_region (Region, Phi2)

* Cross3平移
RowNew3:=Row3-TranslationX*cos(Phi1)
ColumnNew3:=Column3-TranslationX*sin(Phi1)
RowNew3:=RowNew3-Avoid1*sin(Phi1)
ColumnNew3:=ColumnNew3+Avoid1*cos(Phi1)
gen_cross_contour_xld (CrossNew3, RowNew3, ColumnNew3, 96, 0)
dev_display (CrossNew3)

* Cross4平移
RowNew4:=Row4-TranslationX*cos(Phi1)
ColumnNew4:=Column4-TranslationX*sin(Phi1)
RowNew4:=RowNew4+Avoid2*sin(Phi1)
ColumnNew4:=ColumnNew4-Avoid2*cos(Phi1)
gen_cross_contour_xld (CrossNew4, RowNew4, ColumnNew4, 96, 0)
dev_display (CrossNew4)

* 连接点胶线
gen_contour_polygon_xld (Glue1, [RowNew1,RowNew2], [ColumnNew1,ColumnNew2])
gen_contour_polygon_xld (Glue2, [RowNew3,RowNew4], [ColumnNew3,ColumnNew4])
return ()

5. 资源下载

https://download.csdn.net/download/weixin_38566632/37707775

你可能感兴趣的:(Halcon,halcon,点胶机,角点,交点)