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

点胶机胶水路径应用

  • 1. 寻找一种更简单的方法
  • 2. 处理效果
  • 3. Halcon代码
  • 4.资源下载

接续上一篇Halcon——点胶机胶水路径应用(1)
https://blog.csdn.net/weixin_38566632/article/details/121181385

1. 寻找一种更简单的方法

  • 在上一篇博客中使用的方法代码比较多,角点平移依靠三角函数关系,情况复杂,一不小心就搞错了。所以我寻求一种更简洁,稳定性更好的方法。
  • 接下来我将使用获取区域轮廓、轮廓分割和筛选等方法得到电池主体的边缘,然后做最小外接矩形,凭借改变最小外接矩形的半长和半宽就更容易计算角点的坐标了。

2. 处理效果

Halcon——点胶机胶水路径应用(2)_第1张图片
Halcon——点胶机胶水路径应用(2)_第2张图片

3. Halcon代码

TranslationX1:=120  //左移动距离
TranslationX2:=60   //右移动距离
Voaid1:=50  //上避让距离
Voaid2:=90  //下避让距离

GluePointR:=[]
GluePointC:=[]

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])
    

    * 电池主体区域最小外接矩形
    GetCellBody (Image, Rectangle)
    * 计算点胶起点和终点
    GetGluePoints (Rectangle, TranslationX1, Voaid1, TranslationX2, Voaid2, GluePointR, GluePointC)
    
    dev_set_color ('green')
    dev_display (Image)
    
    gen_cross_contour_xld (crosses, GluePointR, GluePointC, 96, 0)
    disp_message (3600, '1', 'image', GluePointR[0], GluePointC[0], 'green', 'false')
    disp_message (3600, '2', 'image', GluePointR[1], GluePointC[1], 'green', 'false')
    disp_message (3600, '3', 'image', GluePointR[2], GluePointC[2], 'green', 'false')
    disp_message (3600, '4', 'image', GluePointR[3], GluePointC[3], 'green', 'false')
    
    dev_display (crosses)

    stop()
endfor

GetCellBody

* ==================得到电池区域最小外接矩形==========================
gen_rectangle1 (ROI_0, 526.525, 1306.97, 3622.8, 4252.81)
reduce_domain (Image, ROI_0, ImageReduced)

threshold (ImageReduced, Regions, 0, 250)
fill_up (Regions, RegionFillUp)

gen_contour_region_xld (RegionFillUp, Contours, 'border')
segment_contours_xld (Contours, ContoursSplit, 'lines_circles', 5, 30, 5)   
select_shape_xld (ContoursSplit, SelectedXLD, ['contlength','height'], 'and', [753.82,1071.88], [4557.89,4799.62])
gen_region_contour_xld (SelectedXLD, Region, 'filled')
union1 (Region, RegionUnion)
smallest_rectangle2 (RegionUnion, Row, Column, Phi, Length1Org, Length2Org)
gen_rectangle2 (Rectangle1, Row, Column, Phi, Length1Org, Length2Org)
return()

GetGluePoints

* ==================计算点胶位置==========================
smallest_rectangle2 (Rectangle, Row, Column, Phi, Length1Org, Length2Org)

* 左上角点
Length2:=Length2Org - TranslationX1
Length1:=Length1Org-Voaid1
gen_rectangle2_contour_xld (Rectangle1,Row, Column, Phi, Length1, Length2)

if (Phi < 0)
    Phi:=rad(180)+Phi
endif

tuple_cos (Phi, Cos)
tuple_sin (Phi, Sin)

X := Length1*Cos - Length2*Sin
Y := Length1*Sin + Length2*Cos
gen_cross_contour_xld(Cross2, Row-Y, Column+X, 96, 0)
GluePointR[0]:=Row-Y
GluePointC[0]:=Column+X

* 右上角点
Length2:=Length2Org - TranslationX2
Length1:=Length1Org-Voaid1
gen_rectangle2_contour_xld (Rectangle1,Row, Column, Phi, Length1, Length2)

if (Phi < 0)
    Phi:=rad(180)+Phi
endif

tuple_cos (Phi, Cos)
tuple_sin (Phi, Sin)

X := Length1*Cos + Length2*Sin
Y := Length1*Sin - Length2*Cos
* gen_cross_contour_xld(Cross3, Row-Y, Column+X, 96, 0)
GluePointR[1]:=Row-Y
GluePointC[1]:=Column+X

* 左下角点
Length2:=Length2Org - TranslationX1
Length1:=Length1Org-Voaid2
* gen_rectangle2_contour_xld (Rectangle1,Row, Column, Phi, Length1, Length2)

X := -Length1*Cos - Length2*Sin
Y := -Length1*Sin + Length2*Cos
* gen_cross_contour_xld(Cross1, Row-Y,  Column+X, 96, 0)
GluePointR[2]:=Row-Y
GluePointC[2]:=Column+X

* 右下角点
Length2:=Length2Org - TranslationX2
Length1:=Length1Org-Voaid2
gen_rectangle2_contour_xld (Rectangle1,Row, Column, Phi, Length1, Length2)

X := -Length1*Cos + Length2*Sin
Y := -Length1*Sin - Length2*Cos
* gen_cross_contour_xld(Cross4, Row-Y, Column+X, 96, 0)
GluePointR[3]:=Row-Y
GluePointC[3]:=Column+X
return ()

4.资源下载

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

你可能感兴趣的:(机器视觉,Halcon,halcon,blob,模板匹配)