天天看點

Halcon中關于角度計算和測量拟合的算子詳解

角度計算算子簡介

1、計算直線與水準軸之間的夾角

angle_lx( : : Row1, Column1, Row2, Column2 : Angle)

角度計算方式:将直線看作向量(有方向性),以直線與水準軸的交點為起點(旋轉中心)。如果終點在水準軸上方,則夾角為逆時針旋轉水準軸到向量的角度(帶正号)。如果終點在水準軸下方,則夾角為順時針旋轉水準軸到向量的角度(帶負号)。結果取決于定義線條的兩點的順序。

角度表示方式:弧度,-π<=Angle<π

2、計算兩條直線之間的夾角

angle_ll( : : RowA1, ColumnA1, RowA2, ColumnA2, RowB1, ColumnB1, RowB2, ColumnB2 : Angle)

角度計算方式:該算子計算原理與angle_lx類似,隻不過把水準軸替換為任意直線B

角度表示方式:弧度,-π<=Angle<=π

3、計算一條直線的方向

line_orientation( : : RowBegin, ColBegin, RowEnd, ColEnd : Phi)

角度計算方式:當直線(無方向性)大緻位于1、3象限的方向,角度為正。當直線大緻位于2、4象限的方向,角度為負。

角度表示方式:弧度,-π/2<Phi<=π/2

4、計算一條直線的參數

line_position( : : RowBegin, ColBegin, RowEnd, ColEnd : RowCenter, ColCenter, Length, Phi)

角度計算方式:同算子 line_orientation。

5、計算區域等效橢圓的參數

elliptic_axis(Regions : : : Ra, Rb, Phi)

角度計算方式:該區域等效橢圓的主軸(無方向性)相對于水準軸(有方向性)的角度。

6、計算區域的最小仿射外接矩形的參數

smallest_rectangle2(Regions : : : Row, Column, Phi, Length1, Length2)

角度計算方式:同算子elliptic_axis

7、計算區域的方向

orientation_region(Regions : : : Phi)

角度計算方式:計算原理基于算子elliptic_axis。此外,還計算了輪廓上離重心最大距離的點。如果此點的列坐标小于重心的列坐标,則elliptic_axis計算得到的角度值需要加上π。

角度表示方式:弧度,-π<=Phi<π

8、計算contours 或 polygons的等效橢圓的參數

elliptic_axis_xld(XLD : : : Ra, Rb, Phi)

角度計算方式:計算原理同算子elliptic_axis。需要注意的是輸入的contours 或 polygons不能自相交,如果他們不是閉合輪廓,那麼在計算過程中會自動閉合,由此可能會産生自相交,進而導緻最終的計算結果不準确。如果輸入的輪廓存在自相交,可用elliptic_axis_points_xld算子代替。

9、計算contours 或 polygons的方向

orientation_xld(XLD : : : Phi)

角度計算方式:計算原理同算子orientation_region。需要注意的是輸入的contours 或 polygons不能自相交,如果他們不是閉合輪廓,那麼在計算過程中會自動閉合,由此可能會産生自相交,進而導緻最終的計算結果不準确。如果輸入的輪廓存在自相交,可用orientation_points_xld算子代替。

角度表示方式:弧度,-π<Phi<=π

總結

計算角度時,如果有方向性,角度範圍一般是(-π,π)。如果沒有方向性,角度範圍一般是(-π/2,π/2)

拟合/測量

*************************測量算子*******************************

*畫仿射矩形

draw_rectangle2 (3600, Row5, Column4, Phi1, Length1, Length2)

gen_rectangle2 (Rectangle1, Row5, Column4, Phi1, Length1, Length2)

*擷取測量句柄

gen_measure_rectangle2 (Row5, Column4, Phi1, Length1, Length2, 512, 512, 'nearest_neighbor', MeasureHandle)

*句柄,平滑參數,灰階差,極性,選擇邊緣點,邊緣中心y,x坐标,幅度,距離

measure_pos (Image, MeasureHandle, 6.1, 65, 'all', 'all', RowEdge, ColumnEdge, Amplitude, Distance)

gen_cross_contour_xld (Cross, RowEdge, ColumnEdge, 6, Phi1)

close_measure (MeasureHandle)

*************************圖像--區域--輪廓************************

*區域轉輪廓

gen_contour_region_xld (SelectedRegions, Contours, 'border')

*輪廓點坐标

get_contour_xld (Contours, Row, Col)

gen_cross_contour_xld (Cross1, Row, Col, 6, Phi1)

*輪廓拟合圓

fit_circle_contour_xld (Contours, 'algebraic', -1, 0, 0, 3, 2, Row3, Column, Radius, StartPhi, EndPhi, PointOrder)

gen_circle_contour_xld (ContCircle, Row3, Column, Radius, 0, 6.28318, 'positive', 1)

*輪廓拟合橢圓或橢圓弧

fit_ellipse_contour_xld (Contours, 'fitzgibbon', -1, 0, 0, 200, 3, 2, Row4, Column3, Phi, Radius1, Radius2, StartPhi1, EndPhi1, PointOrder1)

gen_ellipse_contour_xld (ContEllipse, Row4, Column3, Phi, Radius1, Radius2, 0, 6.28318, 'positive', 1.5)

**************************圖像--輪廓**************************

*提取亞像素精度邊緣輪廓

edges_sub_pix (ImageReduced, Edges, 'canny', 1, 20, 40)

*篩選出指定輪廓

select_contours_xld (Edges, SelectedContours, 'contour_length', 0, 50, -0.5, 0.5)

dev_set_color ('green')

*拟合直線

fit_line_contour_xld (SelectedContours, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)

disp_line (3600, RowBegin, ColBegin, RowEnd, ColEnd)

****************************拟合矩形****************************

*生成感興趣的區域

read_image (Image, 'fabrik')

draw_rectangle1 (3600, Row11, Column11, Row2, Column2)

gen_rectangle1 (Rectangle, Row11, Column11, Row2, Column2)

reduce_domain (Image, Rectangle, ImageReduced)

*提取輪廓,拟合矩形

select_contours_xld (Edges, SelectedContours, 'contour_length', 80, 800, -0.5, 0.5)

fit_rectangle2_contour_xld (SelectedContours, 'regression', -1, 0, 0, 3, 2, Row1, Column1, Phi1, Length11, Length21, PointOrder)

gen_rectangle2 (Rectangle1, Row1, Column1, Phi1, Length11, Length21)

***********************************計算垂足、夾角***************************************

draw_rectangle1 (3600, Row1, Column1, Row2, Column2)

gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)

sort_contours_xld (Edges, SortedContours, 'upper_left', 'true', 'row')

*取輪廓上的點

select_obj (SortedContours, ObjectSelected, 2)

get_contour_xld (ObjectSelected, Row, Col)

*取輪廓上的線

select_obj (SortedContours, ObjectSelected1, 10)

fit_line_contour_xld (ObjectSelected1, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)

*計算點到線的垂足

projection_pl (Row[0],Col[0],RowBegin, ColBegin, RowEnd, ColEnd ,RowProj, ColProj)

*顯示點、直線、垂足、垂線

gen_cross_contour_xld (Cross,Row[0],Col[0], 6, 0.785398)

gen_cross_contour_xld (Cross1,RowProj, ColProj, 6, 0.785398)

disp_line (3600, Row[0],Col[0],RowProj, ColProj)

*計算兩條直線的夾角

angle_ll ( RowBegin, ColBegin, RowEnd, ColEnd, Row[0],Col[0],RowProj, ColProj, Angle)

*弧度轉角度deg,角度轉弧度rad

Ang:=deg(Angle)

*計算直線和水準軸x的夾角

angle_lx (RowBegin, ColBegin, RowEnd, ColEnd, Angle1)

Ang1:=deg(Angle1)

angle_lx (Row[0],Col[0],RowProj, ColProj, Angle2)

Ang2:=deg(Angle2)

項目案例

https://www.cnblogs.com/hanzhaoxin/archive/2013/02/12/2910338.html

halcon學習筆記——執行個體篇(2)長度和角度測量

繼續閱讀