|
https://blog.csdn.net/chenchensuperb/article/details/135273486
1.有一个项目需要求取任意多边形的最大内接矩形,找了halcon中的相关算子只有一个符合类似需求:
inner_rectangle1 — Largest inner rectangle of a region.
The operator inner_rectangle1 determines the largest axis-parallel rectangle that fits into a region. The rectangle is described by the coordinates of the corner pixels (Row1, Column1, Row2, Column2).
使用这个算子只能求得Region内最大的轴平行内接矩形,无法算出任意角度的最大内接矩形。
于是换个角度思考,将Region进行旋转,使用该算子求出剖分角度的ROI内轴平行内接矩形,
然后筛选出最大面积的内接矩形,并将此角度的内接矩形结果进行逆旋转Region的旋转角度,得出最大内接矩形,代码如下:
dev_open_window (0, 0, 800, 700, 'black', WindowHandle)
*dev_update_off ()
*生成一个图片,图片的像素大小求取精度,像素数越多,精度越高,同时运行所花时间越多。
gen_image_const (Image, 'byte', 5120, 3840)
dev_display (Image)
*画一个ROI (画一个闭合的polygon)
draw_polygon (PolygonRegion1, WindowHandle)
fill_up (PolygonRegion1, RegionFillUp)
*//Region旋转起始角度
StartAngle:=-180
*旋转停止角度
StopAngle:=180//Region
*//剖分数
times:=1000
*//剖分角度间距
step:=(rad(StopAngle)-rad(StartAngle))/times
*存储内接矩形面积
AREAS:=[]
*存储角度
Angles:=[]
*存储内接矩形的左上和右下坐标位置
Row1s:=[]
Col1s:=[]
Row2s:=[]
Col2s:=[]
*开始计算
for Index := 1 to times by 1
*获取当前步的角度值
AngleStep:=(Index-1)*step+rad(StartAngle)
*获取Region的中心
area_center (RegionFillUp, Area, Row, Column)
*将Region旋转一个步的角度
vector_angle_to_rigid (Row, Column, 0, Row, Column, AngleStep, HomMat2D)
affine_trans_region (RegionFillUp, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
*求取轴平行内接矩形
inner_rectangle1 (RegionAffineTrans, Row1, Column1, Row2, Column2)
*存储内接矩形的结果
Row1s:=[Row1s,Row1]
Col1s:=[Col1s,Column1]
Row2s:=[Row2s,Row2]
Col2s:=[Col2s,Column2]
*获取矩形的面积
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
area_center (Rectangle, Area1, Row3, Column3)
*存储面积和角度
AREAS:=[AREAS,Area1]
Angles:=[Angles,AngleStep]
endfor
*对内接矩形的面积进行排序,找到最大的矩形面积和索引值
tuple_sort_index (AREAS, Indices)
tuple_length (Indices, Length)
*获取最大内接矩形的参数和Region旋转的角度
AngleOfMaxArea:=Angles[Indices[Length-1]]
Row1OfMaxRect:=Row1s[Indices[Length-1]]
Col1OfMaxRect:=Col1s[Indices[Length-1]]
Row2OfMaxRect:=Row2s[Indices[Length-1]]
Col2OfMaxRect:=Col2s[Indices[Length-1]]
*将找到的内接矩形反向旋转最大矩形面积索引处Region旋转的角度
area_center (RegionFillUp, Area, Row, Column)
vector_angle_to_rigid (Row, Column, AngleOfMaxArea, Row, Column,0, HomMat2D)
gen_rectangle1 (RectangleMax, Row1OfMaxRect, Col1OfMaxRect, Row2OfMaxRect, Col2OfMaxRect)
affine_trans_region (RectangleMax, RectangleMaxInPos, HomMat2D, 'nearest_neighbor')
dev_clear_window ()
dev_display (Image)
dev_set_color ('red')
dev_display (RegionFillUp)
dev_set_color ('green')
dev_display(RectangleMaxInPos)
*求取矩形的角度和面积相关参数
tuple_deg (AngleOfMaxArea, Deg)
area_center (RectangleMaxInPos, Area2, Row4, Column4)
效果如下:
————————————————
版权声明:本文为CSDN博主「chenchensuperb」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/chenchensuperb/article/details/135273486 |
|