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<cv::Vec2f> lines;
    // Vec2f 的 vector 接收lines
    cv::HoughLines(mask, lines, 1,PI/180, 60);
    // 霍夫变换得到lines
    cvtColor(mask,mask,COLOR_GRAY2BGR);
    // GRAY->BGR    
    cout<<lines.size()<<endl;
    std::vector<cv::Vec2f>::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)<<endl;
        float rho= (*it)[0]; // first element is distance rho
        float theta= (*it)[1]; // second element is angle theta
        
        
        if (theta >PI/12 && theta < 11*PI/12)
        {
            if (theta > PI/2 + PI/9 || theta < PI/2 - PI/9)
            {
                cout<<"-----"<<rho<<"----"<<theta<<endl;
                if(theta > 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<cv::Vec2f> a = {Vec2f(min_rho,min_theta),Vec2f(max_rho,max_theta)};
    std::vector<cv::Vec2f>::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<<theta_filter_lines.size()<<endl;
    // std::vector<cv::Vec2f>::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;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值