Opencv 如何寻找直线?

void findLine(const std::string file,std::double_t min_distance_value = 100.0)
{
    //https://github.com/opencv/opencv/blob/master/modules/imgproc/src/lsd.cpp
    auto lsd = cv::createLineSegmentDetector(
                    cv::LSD_REFINE_NONE
                );

    auto img = cv::imread(file,cv::IMREAD_GRAYSCALE);

    std::vector<cv::Vec4i> result;
    lsd->detect(img, result);
    lsd->drawSegments(img, result);

    for(int i = 0;i < result.size();++i){
        auto x1 = result[i][0];
        auto y1 = result[i][1];
        auto x2 = result[i][2];
        auto y2 = result[i][3];

        double distance_value = std::sqrt(std::pow((x1-x2),2) + std::pow((y1-y2),2));
        if(distance_value > min_distance_value){
            cv::line(img,cv::Point2i(x1,y1),cv::Point2i(x2,y2),cv::Scalar(0,0,255),3);
            qDebug() << x1 << x2 << y1 << y2;
        }
    }

    cv::namedWindow("FindLine", cv::WINDOW_GUI_EXPANDED);
    cv::imshow("FindLine", img);
    cv::waitKey(0);
}

posted @ 2020-03-15 12:45  學海無涯  阅读(1665)  评论(0编辑  收藏  举报