获取旋转矩形的四个角点

以halcon为例: opencv yolo obb 等标注工具

都是一样的

首先创建函数:

旋转矩形在halcon里::

draw_rectangle2 (WindowHandle, Row, Column, Phi, Width, Height)

gen_rectangle2 (RectangleRo, Row, Column, Phi, Width,  Height)

 Row 中心点Y, Column 中心点X, Phi 角度(弧度), Width 矩形宽度,  Height 矩形高度

在halcon中  参数顺序经常会是 Row, Column , 囫囵吞枣的理解就是:Y.X

在halcon中  参数顺序经常会是 Width,Height 剑鸣质疑(见名知意)

不解释了 反正意思就是参数顺序有违常理(也不是常理就是顺其自然的顺序)

首先创建函数:

函数名:    get_rectangle2_points
输入参数:  Row, Column, Phi,  Width , Height, bl,
输出参数:
    Corner1Row, Corner1Col, \
    Corner2Row, Corner2Col, \
    Corner3Row, Corner3Col,\
    Corner4Row, Corner4Col\


get_rectangle2_points (Row, Column, Phi,  Width , Height, bl, \
Corner1Row, Corner1Col, \
Corner2Row, Corner2Col, \
Corner3Row, Corner3Col,\
Corner4Row, Corner4Col\
)

函数内部:

* Row, 中心Y
* Column, 中心X
* Phi 弧度
* Width  矩形宽
* Height, 矩形高

bl 就是 比例
* *1.0 刚好重合到点上
* 如果需要点 比原来矩形大 那么大于1试试   如果需要 比原来小 那么小于1试试

Length1 :=Width*bl
Length2 :=Height*bl
tmp_cos_p_L1:=(cos(Phi) * Length1)
tmp_sin_p_L1:=(sin(Phi) * Length1)
tmp_cos_p_L2:=(cos(Phi) * Length2)
tmp_sin_p_L2:=(sin(Phi) * Length2)
* 角点1 (右上)
Corner1Row := Row    + tmp_cos_p_L1 - tmp_sin_p_L2
Corner1Col := Column + tmp_sin_p_L1 + tmp_cos_p_L2
* 角点2 (右下)
Corner2Row := Row    - tmp_sin_p_L2 - tmp_cos_p_L1
Corner2Col := Column + tmp_cos_p_L2 - tmp_sin_p_L1
* 角点3 (左下)
Corner3Row := Row    - tmp_cos_p_L1 + tmp_sin_p_L2
Corner3Col := Column - tmp_sin_p_L1 - tmp_cos_p_L2
* 角点4 (左上)
Corner4Row := Row    + tmp_sin_p_L2 + tmp_cos_p_L1
Corner4Col := Column - tmp_cos_p_L2 + tmp_sin_p_L1

接下来完整程序:

dev_clear_window ()
gen_image_const (Image, 'byte', 512*3, 512*3)
gen_image_proto (Image, ImageCleared, 80)
gen_empty_obj (EmptyObject)
gen_empty_region (EmptyRegion)
win_width:=512
win_height:=512
get_image_size (Image, imgWidth, imgHeight)
scale:=max2(1.0 * imgWidth / win_width, 1.0 * imgHeight / win_height)
w:=win_width*scale
h:=win_height*scale
dev_open_window (0, 0, win_width,win_height,  'black', WindowHandle)
dev_display (Image)
dev_set_part (-(h - imgHeight) / 2, -(w - imgWidth) / 2, imgHeight + (h - imgHeight) / 2, imgWidth + (w - imgWidth) / 2)
dev_clear_window ()
dev_display (Image)

* threshold (Image, Regionttttth, 128, 255)
* get_image_size (Image, Width, Height)
* region_to_bin (Regionttttth, BinImage, 250, 0, Width, Height)
dev_clear_window ()
dev_set_check ('give_error')
dev_set_draw ('margin')
dev_set_line_width (4)
* dev_set_draw ('fill')
draw_rectangle2 (WindowHandle, Row, Column, Phi, Width, Height)
gen_rectangle2 (RectangleRo, Row, Column, Phi, Width,  Height)
bl:=1.1
get_rectangle2_points (Row, Column, Phi,  Width , Height, bl, \
Corner1Row, Corner1Col, \
Corner2Row, Corner2Col, \
Corner3Row, Corner3Col,\
Corner4Row, Corner4Col\
)
gen_circle (Circle1, Corner1Row, Corner1Col, 10)
gen_circle (Circle2, Corner2Row, Corner2Col, 10)
gen_circle (Circle3, Corner3Row, Corner3Col, 10)
gen_circle (Circle4, Corner4Row, Corner4Col, 10)
dev_clear_window ()
dev_display (Image)
dev_display (RectangleRo)
dev_set_color ('#007777')
dev_display (Circle1)
dev_set_color ('yellow')
dev_display (Circle2)
dev_set_color ('green')
dev_display (Circle3)
dev_set_color ('blue')
dev_display (Circle4)
stop ()

运行结果: 比例1.1 四个角点比矩形框大一圈 也可以写 1.01 1.02试试 

 运行结果: 比例1.0 四个角点与矩形框重合. 

当然 不限于halcon 其他程序一样的 这里说的是计算方式

最后总结一下: 上学时候 不好好学数学 ,现在cos sin 都用起来很痛苦.

明白吗数学不行就很痛苦.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值