Opencv 如何寻找直线?

こ雲淡風輕ζ 提交于 2020-03-15 13:57:09
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);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!