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

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

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MechMaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值