halcon拓展系列—計算任意矩形的四個角點座標算子find_rectangle2_points

計算任意矩形的四個角點座標,基本數學方法利用到了初中高中數學知識:正弦定理和餘弦定理

一、基礎知識

1、halcon的矩形rectangle2定義

 

draw_rectangle2( : : WindowHandle : RowColumnPhiLength1Length2)

smallest_rectangle2(Regions : : : RowColumnPhiLength1Length2)

draw_rectangle2:窗口有個箭頭方向,這個方向就是矩形的角度Phi,和Phi方向一致的邊爲Length1,和Phi方向垂直的邊爲Length2

smallest_rectangle2:Length1爲長度較長的邊,Length2爲長度較短的邊,且滿足Phi爲長邊Length1方向的角度,角度範圍

- pi / 2 < Phi && Phi <= pi / 2

兩者相同點:Phi都是Length1的角度

兩者不同點:draw_rectangle2的Length1,Length2和Phi與邊長度無關,smallest_rectangle2與邊長度有關

 

2、正弦定理和餘弦定理

 

二、計算任意矩形的四個角點座標

步驟

1、統一矩形描述

把rectangle2轉爲PI/2>Phi>PI/4,或者-PI/4>Phi>-PI/2,修改矩形的的描述方式,讓矩形的Phi較大,這樣保證矩形縱方向是Length1,橫方向是Length2,

2、計算點A座標

x=x(OM) - x(AM) = Length1*cos(angle) - Length2*sin(angle)---------------------1.1
y=y(ON) + y(AN) = Length1*sin(angle) + Length2*cos(angle)---------------------1.2

 

 

設xLengh1 = Length1*cos(angle)

xLength2 =  Length2*sin(angle)

yLength1 = Length1*sin(angle)

yLength2 = Length2*cos(angle)

簡化1.1和1.2爲

x = xLength1 - xLength2------------------------------------------1.3
y = yLength1 + yLength2------------------------------------------1.4

 

即A(xLength1 - xLength2, yLength1 + yLength2 )

同理,可以計算出B,C,D點的座標,過程中要考慮到Phi有正負之分

 

詳細代碼如下

* ****************************************
* * 求rectangle2的四個直角點
* * 0********|*********2
* * *********|**********
* * *********|**********
* * *********|**********
* * *********|**********
* * *********|**********
* * 1********|*********3
* *注:以與水平方向所成角度較大的邊中線爲軸線
* ****************************************
pi := acos(0)*2
if (phi >= 0 and phi < pi/4)
    phi := phi - pi/2
    Tem := length1
    length1 := length2
    length2 := Tem
elseif (phi > -pi/4 and phi < 0)
    phi := phi + pi/2
    Tem := length1
    length1 := length2
    length2 := Tem
endif
* 
if (phi >= 0)
    la := phi    ///63
    lb := la - pi/2    ////-26
    tuple_tan (la, tem1)
    tuple_tan (lb, tem2)
    tuple_sqrt ((length1 * length1) / (1 + tem1 * tem1), xLength1)
    tuple_sqrt ((length2 * length2) / (1 + tem2 * tem2), xLength2)
    tuple_sqrt ((tem1*tem1*length1*length1) / (1 + tem1 * tem1), yLength1)
    tuple_sqrt ((tem2 * tem2 * length2 * length2) / (1 + tem2 * tem2), yLength2)
    * 左上
    gen_cross_contour_xld (Cross, 1, columnCenter + xLength1- xLength2, 6, 0.785398)
    mColumnUpLeft := columnCenter + xLength1 - xLength2
    nRowUpLeft := rowCenter - yLength1 - yLength2
    * 左下
    mColumnDownLeft := columnCenter - xLength1  - xLength2
    nRowDownLeft := rowCenter + yLength1 - yLength2
    * 右上
    mColumnUpRight := columnCenter + xLength1  + xLength2
    nRowUpRight  := rowCenter - yLength1 + yLength2
    * 右下
    mColumnDownRight := columnCenter - xLength1  + xLength2
    nRowDownRight  := rowCenter + yLength1 + yLength2 
else
    la := phi   
    lb := la - pi/2    
    tuple_tan (la, tem1)
    tuple_tan (lb, tem2)
    tuple_sqrt ((length1 * length1) / (1 + tem1 * tem1), xLength1)
    tuple_sqrt ((length2 * length2) / (1 + tem2 * tem2), xLength2)
    tuple_sqrt ((tem1*tem1*length1*length1) / (1 + tem1 * tem1), yLength1)
    tuple_sqrt ((tem2 * tem2 * length2 * length2) / (1 + tem2 * tem2), yLength2)
    * 左上
    mColumnUpLeft := columnCenter - xLength1 - xLength2
    nRowUpLeft := rowCenter - yLength1 + yLength2
*     disp_cross (3600, nRowUpLeft, mColumnUpLeft, 16, 0)
    * 左下
    mColumnDownLeft := columnCenter + xLength1  - xLength2
    nRowDownLeft := rowCenter + yLength1 + yLength2
*     disp_cross (3600, nRowDownLeft, mColumnDownLeft, 16, 0)
    * 右上
    mColumnUpRight := columnCenter - xLength1  + xLength2
    nRowUpRight  := rowCenter - yLength1 - yLength2
    * 右下
    mColumnDownRight := columnCenter + xLength1  + xLength2
    nRowDownRight  := rowCenter + yLength1 - yLength2 
endif
row := []
column := []
row[0] := nRowUpLeft
column[0] := mColumnUpLeft
row[1] := nRowDownLeft
column[1] := mColumnDownLeft
row[2] := nRowUpRight
column[2] := mColumnUpRight
row[3] := nRowDownRight
column[3] := mColumnDownRight
return ()

運行效果圖如下

注:代碼裏面推導過程稍有不同,用到了tan(angle)=sin(angle)/cos(angle),最終結果都是一樣的;基於這個方法,可以做很多算法,比如Blob粗定位,Blob精定位,直線濾波等;

因爲方法沒有引用其他方法,上述代碼就是所有源碼,這裏就不上傳源碼

如需要hdvp函數請留言個人郵箱

 

 

————————————————
版權聲明:本文爲CSDN博主「谷棵」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/gukewee/article/details/105787343

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章