halcon已经有算子fit_line_contour_xld
当然也可以自己实现:
* 先获得xld曲线上的点坐标 get_contour_xld (ObjectSelected2, Row2, Col2) tuple_length (Row2, Length2) create_matrix (Length2, 1, Col2, MatrixID_y2) //y create_matrix (Length2, 2, 1, MatrixID_x2) //x set_value_matrix (MatrixID_x2, [0:Length2-1], gen_tuple_const(Length2, 0), Row2) //把X矩阵第一列换成MX的值 mult_matrix (MatrixID_x2, MatrixID_x2, 'ATB', MatrixMultID_x2x2) //ATB=A的转置*B,实际上就是xT*x mult_matrix (MatrixID_x2, MatrixID_y2, 'ATB', MatrixMultID_x2y2) //xT*y invert_matrix (MatrixMultID_x2x2, 'general', 0, MatrixInvID_x2x2) //xTx的逆 mult_matrix (MatrixInvID_x2x2, MatrixMultID_x2y2, 'AB', MatrixMultID_beta2) //xTx的逆 * xT*y ;就成了系数 β(a,b组成的矩阵) get_full_matrix (MatrixMultID_beta2, beta_Values2) //β的值,也就是a和b的值 X20:=Row2[0] X2n:=Row2[Length2-1] Cols_2:=beta_Values2[0] * [X20,X2n] + beta_Values2[1] * 生成xld直线 gen_contour_polygon_xld (Contour2, [X20,X2n], Cols_2) length_xld (Contour2, Length_Con2) * 计算距离 (原曲线到拟合直线) distance_pl (Row2, Col2, X20, Cols_2[0], X2n, Cols_2[1], Distance2_C2)
---
参考文献
http://ihalcon.com/read-17610.html