使用HoughLinesP的OpenCV,C ++,行检测 [英] OpenCV, C++, Line Detection with HoughLinesP

查看:108
本文介绍了使用HoughLinesP的OpenCV,C ++,行检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在两行之间裁剪图像,如下图所示.但是HoughLinesP不能很好地识别底线. 底线点并没有因为侵蚀而真正边缘化,但这很重要吗?

I want to crop the image between two lines, as shown in the image below. But the bottom line is not recognized well with HoughLinesP. The bottom line points are not really edged because of eroding, but is it important?

如何检测底线,然后根据这两条线裁切图像?

原始图片:

已处理的图像:

Canny抢走了

检测到的线:

线路检测代码:

Mat dst, cdst,src2;
cv::blur( src, src2, cv::Size(5,5) );
Canny(src2, dst, 150, 300, 5);
cvtColor(dst, cdst, CV_GRAY2BGR);

//Mat original = imread("final_sample1.png",0);

vector<Vec4i> lines;
HoughLinesP(dst, lines, 1, 2*CV_PI/180, 100,1000, 50 );

用于显示行:

   for( size_t i = 0; i < lines.size(); i++ )
{
    Vec4i l = lines[i];

   // oran = float(l[1] / col_size );
    double angle = atan2(l[3] - l[1], l[2] - l[0]) * 180.0 / CV_PI;

    if(angle  < 5  && angle >=-5   ){
    //if(1){
        cout << l[0] << "," << l[1] << "," << l[2] << "," << l[3] << endl;
        line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA);
    }
}

对于线条检测,将自适应阈值应用于原始图像可提供更可靠的结果.

For line detection appliying adaptive tresholding to original image gives more reliable results.

adaptiveThreshold(img,adaptiveTresholded,255,ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY,75,15);

我对20个具有不同行数的样本进行了测试,并且由于Micka的修改,我获得了不错的结果.为了检测正确的行,我放置了一个if语句.

I tested on 20 samples which has different number of rows , and thanks to Micka, with his modification, I got good results. To detect the right lines I put an if statement.

比率"变量是y1/图片的行大小.并检查线角以防止 不相关的行.

"ratio" variable is the y1 / row size of the image. And checking the line angle to prevent irrelevant lines.

for( size_t i = 0; i < lines.size(); i++ )
{
    Vec4i l = lines[i];

    raito = float(l[1] / row_size );
    double angle = atan2(l[3] - l[1], l[2] - l[0]) * 180.0 / CV_PI;

  if(angle  < 10 && angle >=- 10 && ratio > 0.15 && ratio< 0.8){
    //if(1){
        cout <<"Here: " <<  l[0] << "," << l[1] << "," << l[2] << "," << l[3] <<
        ", Oran: " << oran <<  endl;
        line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3);
    }
}

推荐答案

使用您的图像和此代码(基本上是您的代码,但减小了最大行距,并使用了一些膨胀来实际从非直线底线中制作出连接的straigt零件:

using your image and this code (basically yours but reduced the maximum line gap and used some dilates to actually make connected straigt parts out of the non-straight bottom line:

int main()
{
    cv::Mat input = cv::imread("../inputData/LongLine.jpg");

    cv::Mat gray;
    cv::cvtColor(input,gray,CV_BGR2GRAY);

    // threshold because of loaded jpeg artifacts
    cv::Mat edges = gray > 100;

    cv::dilate(edges, edges, cv::Mat());
    cv::dilate(edges, edges, cv::Mat());
    cv::dilate(edges, edges, cv::Mat());

    std::vector<cv::Vec4i> lines;
    cv::HoughLinesP(edges, lines, 1, 2*CV_PI/180, 100,1000, 10 );

    for( size_t i = 0; i < lines.size(); i++ )
    {
        cv::Vec4i l = lines[i];

        cv::line( input, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(0,0,255), 3);

    }


    cv::imwrite("../outputData/LongLine.png", input);
    cv::resize(input, input, cv::Size(), 0.1, 0.1);
    cv::imshow("input",input);
    cv::waitKey(0);
    return 0;
}

获得此结果:

对于HoughLinesP,重要的一点是,线条必须真正笔直,因为求和的线条的厚度应为1.因此,如果累加器仅错失一个像素,则线条会失败.由于底线不如前几条那么直,所以这可能是问题所在.

For HoughLinesP is is important that the line is really straight, because the summed lines are expected to have thickness 1. So if the accumulator misses lines by just one pixel, it fails. Since the bottom lines aren't as straight as the first ones, this might be the problem.

这篇关于使用HoughLinesP的OpenCV,C ++,行检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆