Opencv6:line_orientation的Opencv实现(获取直线角度、两直线夹角)
(1)line_orientation简介:
line_orientation( : : RowBegin, ColBegin, RowEnd,ColEnd : Phi)
Halcon函数,给定直线上两点,以弧度形式返回直线的角度。
(2)本文扩展:
本文对line_orientation函数进行扩展,将实现给定两条直线上各两点,返回两直线夹角的功能,可选择角度形式或弧度形式返回。
当需要获取一条直线的角度时,只需将另一条直线设为与x轴平行的任一直线即可。
(3)实现思路:
①根据给定点的坐标分别获得两条直线的斜率;
②根据两斜率求得直线夹角的正切值;
③以角度或弧度形式返回直线夹角。
(4)代码:
#include <opencv2\opencv.hpp>
#include <iostream>
#include "FunctionsHalcon.h"
using namespace std;
using namespace cv;
//【1】根据两直线上两点计算两直线夹角
//当flag=0时返回弧度,当flag!-0时返回角度
float lines_orientation1(CvPoint A1, CvPoint A2, CvPoint B1, CvPoint B2, int flag)
{
//【1】根据直线上两点计算斜率
float kLine1 = (A2.y - A1.y)/(A2.x - A1.x);
float kLine2 = (B2.y - B1.y) / (B2.x - B1.x);
//【2】根据两个斜率计算夹角
float tan_k = 0; //直线夹角正切值
tan_k = (kLine2 - kLine1) / (1 + kLine2*kLine1);
float lines_arctan = atan(tan_k); //反正切获取夹角
//【3】以角度或弧度形式返回夹角
if (flag == 0)
{
return lines_arctan;
}
else
{
return lines_arctan* 180.0 / 3.1415926;;
}
}
int main()
{
CvPoint A1 = cvPoint(0,1);
CvPoint A2 = cvPoint(1,0);
CvPoint B1 = cvPoint(0,0);
CvPoint B2 = cvPoint(1,1);
float a = lines_orientation1(A1, A2, B1, B2, 1);
cout << a << endl;
waitKey(0);
system("pause");
return 0;
}