opencv2.4.9 & VS2013环境

问题描述

在图中找出两条直线,并找到两条直线交点位置。

python opencv有向线段 opencv找线_斜率

思路:

1) 读图,二值化,简单腐蚀,之后直线在原图中比较明显,所以考虑直接用霍夫变换寻找直线。

2)霍夫直线检测可能会检测出多条重叠直线,利用上下两部分直线斜率相反筛选一下,选出两条直线。

3)两条直线求交点,变为解二元一次方程问题。

4)画出结果

实现:

1)调用opencv中的HoughLinesP函数:

void HoughLinesP( InputArray image, OutputArray lines,double rho, double theta, int threshold,double minLineLength=0, double maxLineGap=0 );

image为输入图像,要求是8位单通道图像

lines为输出的直线向量,每条线用4个元素表示,即直线的两个端点的4个坐标值

rho和theta分别为距离和角度的分辨率

threshold为阈值,即步骤3中的阈值

minLineLength为最小直线长度,在步骤5中要用到,即如果小于该值,则不被认为是一条直线

maxLineGap为最大直线间隙,在步骤4中要用到,即如果有两条线段是在一条直线上,但它们之间因为有间隙,所以被认为是两个线段,如果这个间隙大于该值,则被认为是两条线段,否则是一条。

2)检测出的直线为Vec4i类型,一共四位数。分别代表直线上的(x1,y1,x2,y2),求出每条直线写了,Ka * kB<0即代表两条直线鞋履相反

3)利用四个点坐标解方程,返回point2f类型的一个点

4)利用Line()以及circle()函数画出图像

代码:

/*Copyright@hitzym
Harbin Institute of Technology
Nov,29, 2017*/
# include <opencv2/opencv.hpp>  
# include <iostream>  

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;
}

运行结果:

python opencv有向线段 opencv找线_ci_02

python opencv有向线段 opencv找线_图像处理_03


一个很简单的小程序~

请各位看官多多指教!