栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

C/C++ 轨道区域检测(基于霍夫变换)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C/C++ 轨道区域检测(基于霍夫变换)

Python 轨道区域检测代码转c++ 效果可看这篇博客
cv::Mat rect_crop(cv::Mat& img){
    float rate = 0.25;  //ROI比例
    float x, y, w, h;	//ROI坐标
    cv::Mat mask = cv::Mat::zeros(img.size().height,img.size().width, CV_8UC3);
    // 同宽高mask
    x = img.size().width*rate;
    y = img.size().height*rate;
    w = img.size().width*(1-rate) - x;
    h = img.size().height*(1-rate) - y;
    //ROI坐标
    cv::Rect r = cv::Rect(x, y, w, h);
    // Rect
    cv::Mat crop_img = img(r);
    // 获取crop_img
    crop_img.copyTo(mask(r));
    // mask(r)用crop_img代替
    cvtColor(mask,mask,COLOR_BGR2GRAY);
    // BGR->GRAY    
    cv::GaussianBlur(mask, mask,cv::Size(17, 17), 0, 0);
    // GaussinBlur
    cv::Canny(mask, mask,10,70);
    // Canny
    std::vector lines;
    // Vec2f 的 vector 接收lines
    cv::HoughLines(mask, lines, 1,PI/180, 60);
    // 霍夫变换得到lines
    cvtColor(mask,mask,COLOR_GRAY2BGR);
    // GRAY->BGR    
    cout<::const_iterator it= lines.begin();
    // 迭代器
    float max_theta = 0.0f;
    float min_theta = 3.0f;
    float max_rho = 0.0f;
    float min_rho = 0.0f;
    // 循环获取最左侧和最右侧的直线 /简单的theta筛选 可替换更加高级的办法
    while (it!=lines.end())
    {
        cout<<(*it)<PI/12 && theta < 11*PI/12)
        {
            if (theta > PI/2 + PI/9 || theta < PI/2 - PI/9)
            {
                cout<<"-----"< max_theta){max_theta = theta;max_rho=rho;}
                if(theta < min_theta){min_theta = theta;min_rho=rho;}
                // theta_filter_lines.push_back(*it);
            }
        }
        it++;
    }

    std::vector a = {Vec2f(min_rho,min_theta),Vec2f(max_rho,max_theta)};
    std::vector::const_iterator ita= a.begin();
    // 通过最大和最小theta来画出轨道的左右两条直线
    while (ita!=a.end())
    {
        float rho= (*ita)[0]; // first element is distance rho
        float theta= (*ita)[1]; // second element is angle theta
        cv::Point pt1(0,rho/sin(theta));
        // point of intersection of the line with last column
        cv::Point pt2(mask.cols, (rho - mask.cols * cos(theta))/sin(theta));
        // draw a white line
        cv::line(mask, pt1, pt2, cv::Scalar(0,255,0), 1);
        ita++;
    }




    // cout<::const_iterator it1= theta_filter_lines.begin();
    // while (it1!=theta_filter_lines.end())
    // {
    //     float rho= (*it1)[0]; // first element is distance rho
    //     float theta= (*it1)[1]; // second element is angle theta
    //     cv::Point pt1(0,rho/sin(theta));
    //     // point of intersection of the line with last column
    //     cv::Point pt2(mask.cols, (rho - mask.cols * cos(theta))/sin(theta));
    //     // draw a white line
    //     cv::line(mask, pt1, pt2, cv::Scalar(0,255,0), 1);
    //     it1++;
    // }
    return mask;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/619423.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号