Halcon提取图像中的最小外接矩形 得出矩形的中心点和角度

由于这段时间在做一个视觉项目,里面由的产品类似于矩形,

使用模板匹配定位坐标系,边缘提取计算。可以做到精定位。

但是项目中,在相机视野内存在多个矩形产品(客户要求的定位精度要求:±0.5)比较松

所以我选择了  图像处理+矩形提取的方式,快捷,简单,客户对机器也不需要操作转向,矩形产品进去视野内直接抓取

halcon代码

read_image (Image, 'C:/Users/windows7_dg/Pictures/66.bmp')
threshold (Image, Region, 100, 255)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 300000, 800000)
union1 (SelectedRegions, RegionUnion)
reduce_domain (Image, RegionUnion, ImageReduced)
boundary (RegionUnion, RegionBorder, 'inner')
dilation_rectangle1 (RegionBorder, RegionDilation, 11, 11)
reduce_domain (Image, RegionDilation, ImageReduced1)
edges_sub_pix (ImageReduced1, Edges, 'canny', 5, 10, 120)           //注意参数的填写,这里的参数直接影响矩形的提取
select_shape_xld (Edges, RectangleEdges, 'contlength', 'and', 2, 99999)
fit_rectangle2_contour_xld (RectangleEdges, 'tukey', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
gen_rectangle2_contour_xld (Rectangle, Row, Column, Phi, Length1, Length2)
dev_set_color ('red')
dev_display (Rectangle)
count_obj (RectangleEdges, Number)
for Index := 1 to Number by 1
    tuple_deg(Phi, Deg)           //弧度转角度
endfor
 

 

 

 

 

 

你可能感兴趣的:(C#)