问题描述:`
通过吸盘区域与工件区域的交集得到需要的吸盘,计算得到的吸盘在吸盘中的坐标,即吸盘序号。
问题分析:
这个问题的关键是使用Halcon的函数 sort_region (RegionXP, SortedRegions, ‘character’, ‘true’, ‘row’)。
sort_region作用:根据区域的相对位置进行分类。将Regions(多个区域)按照一定的顺序进行排列。
根据实际情况,这儿的第三个参数取值是 ‘character’。取’character’时,会对区域进行从左到右,从上到下的排序。
//函数原型
sort_region(Regions : SortedRegions : SortMode, Order, RowOrCol : )
/****************************第三个参数**************************/
'first_point'
The point with the lowest column value in the first row of the region.
'last_point'
The point with the highest column value in the last row of the region.
'upper_left'
Upper left corner of the surrounding rectangle.
'upper_right'
Upper right corner of the surrounding rectangle.
'lower_left'
Lower left corner of the surrounding rectangle.
'lower_right'
Lower right corner of the surrounding rectangle.
'character'
从左到右,从上到下的排序。
源码:
对原吸盘区域进行排序,得到每个吸盘的排序序号。再将吸盘与工件交集后,去掉面积为0的区域。
//对区域按从左到右、从上倒下的顺序排序
sort_region (RegionXP, SortedRegions, 'character', 'true', 'row')
area_center (SortedRegions, XPArea, XPRow, XPColumn)
count_obj (SortedRegions, XPNumber)
//工件与吸盘交集
intersection (AllGJRegion, RegionXP, GJRegionIntersection)
count_obj (GJRegionIntersection, Number)
area_center (GJRegionIntersection, GJArea, GJRow, RIColugetmn)
//全部废料与吸盘的交集
intersection (AllFLRegion, RegionXP, FLRegionIntersection)
area_center (FLRegionIntersection, FLArea, FLRow, FLColumn)
//去掉工件交集中面积为零的区域
emptytuple := []
for RInum := 0 to Number-1 by 1
if (GJArea[RInum]<=0)
emptytuple[RInum] := RInum+1
endif
endfor
remove_obj (GJRegionIntersection, GJRegionIntersection, emptytuple)
然后计算交集中每个吸盘的面积和中心坐标,依次判断所得区域中每个吸盘的面积是否大于1/2,再利用中心坐标通过 get_region_index () 函数获取其在原吸盘坐标中的排序序号。
if (RIArea[RIpart-1]>0)
select_obj (RegionIntersection, RISel, RIpart)
connection (RISel, ConnectedRI)
count_obj (ConnectedRI, IntersectionNumer)
* 求交集中吸盘面积和中心
area_center (ConnectedRI, ISArea, ISRow, ISColumn)
k := 0
for i := 0 to IntersectionNumer-1 by 1
get_region_index (SortedRegions, round(ISRow[i]), round(ISColumn[i]), IntersectionIndex)
tuple_length (IntersectionIndex, Length1)
if (Length1>0 and ISArea[i] > (XPArea[IntersectionIndex-1]/2))
Intersection[k] := IntersectionIndex
// 通过下标求商得到吸盘x上的坐标
// GJx[k] := int((IntersectionIndex-1) / 20) +1
// 通过下标取余得到吸盘y上的坐标
// GJy[k] := (IntersectionIndex-1) % 20 +1
k := k+1
endif
endfor
endif
总结:
刚开始做使没有思绪的,后面慢慢上手,还是能把它做好,感觉自己对Halcon的一些算子很生疏,只能慢慢熟悉了,加油,没有灵魂的键盘手!!!!!!!!!!!
为了使运行速度进一步提高,其中加入了多线程。多线程实现部分下一篇写,时间紧迫,还得干作业。。。。