openCV求两条线的交点

openCV求两条线的交点,记录

原文链接:https://blog.csdn.net/yinhuan1649/article/details/78732775

include <opencv2/opencv.hpp>

include

using namespace cv;
using namespace std;

/函数功能:求两条直线交点/
/输入:两条Vec4i类型直线/
/返回:Point2f类型的点/
Point2f getCrossPoint(Vec4i LineA, Vec4i LineB)
{
double ka, kb;
ka = (double)(LineA[3] - LineA[1]) / (double)(LineA[2] - LineA[0]); //求出LineA斜率
kb = (double)(LineB[3] - LineB[1]) / (double)(LineB[2] - LineB[0]); //求出LineB斜率

Point2f crossPoint;
crossPoint.x = (ka*LineA[0] - LineA[1] - kb*LineB[0] + LineB[1]) / (ka - kb);
crossPoint.y = (ka*kb*(LineA[0] - LineB[0]) + ka*LineB[1] - kb*LineA[1]) / (ka - kb);
return crossPoint;

}

int main(){

Mat src, grayImg, binImg, result;
src = imread("./1.jpg");
//imshow("srcimage", src);
//waitKey(30);

/*检查图像是否载入*/
if (src.empty()) {
    printf("Error Loading Image...\n");
    return -1;
}

/*转为灰度图*/
if (src.channels() == 3){
    cvtColor(src, grayImg, CV_BGR2GRAY);
}
else if (src.channels() == 2){
    grayImg = src.clone();
}

/*二值化*/
threshold(grayImg, binImg, 100, 255, THRESH_BINARY);
//adaptiveThreshold(grayImg, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
//imshow("binary image", binImg);
//waitKey(100);

/*腐蚀*/
Mat element = getStructuringElement(MORPH_RECT, Size(2, 1));
Mat erodeImg; 
erode(binImg, erodeImg, element);
//imshow("erode", erodeImg);
//waitKey(100);

/*霍夫直线检测*/
vector<Vec4i> Lines;
HoughLinesP(erodeImg, Lines, 1, CV_PI / 360, 200, 100, 10);
Vec4i LineStand = Lines[0];
Vec4i LineAnother;
double ka = (double)(LineStand[1] - LineStand[3]) / (double)(LineStand[0] - LineStand[2]);
double kb;
for (int i = 1; i < Lines.size(); i++)
{
    double ki = (double)(Lines[i][1] - Lines[i][3]) / (double)(Lines[i][0] - Lines[i][2]);
    if (ki*ka < 0)        
    {
        LineAnother = Lines[i];
        kb = ki;
    }
}

/*画出两条直线*/
result = src.clone();
line(result, Point(LineStand[0], LineStand[1]), Point(LineStand[2], LineStand[3]), Scalar(0, 255, 0), 2, 8);
line(result, Point(LineAnother[0], LineAnother[1]), Point(LineAnother[2], LineAnother[3]), Scalar(0, 0, 255), 2, 8);
cout << "直线A过点(" << LineStand[0] << "," << LineStand[1] << ")以及点(" << LineStand[2]<<","<<LineStand[3] << ");斜率为:" << ka << endl;
cout << "直线B过点(" << LineAnother[0] << "," << LineAnother[1] << ")以及点(" << LineAnother[2] << "," << LineAnother[3] << ");斜率为:" << kb << endl;

/*求交点并画点保存,result.jpg存储在工程目录下*/
Point2f crossPoint;
crossPoint = getCrossPoint(LineStand, LineAnother);
circle(result, crossPoint, 6, Scalar(255, 0, 0));
imwrite("./result.jpg", result);
cout << "交点坐标为:" << crossPoint << endl;
cout << "Copyrigth@zym" << endl;
imshow("result", result);
waitKey(100);
system("pause");

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值