Opencv車線検出


しゃせんけんしゅつ
ブログにまとめる:https://blog.csdn.net/sgfmby1994/article/details/78670630
実験手順:
コードフレーム:
完全コースPPTと実験要求スタンプ:https://github.com/MintLucas/Digital_image_process
#include 
#include 
#include 
#include 
#include 
#include "Lab01_grayConversion.hpp"
#include "Lab02_hisBalance.hpp"
#include "Lab03_spacialFilter.hpp"
#include "Lab04_noiseEliminate.hpp"
#include "Lab05_spectrumFileter.hpp"

using namespace std;
using namespace cv;

int main(){
    Mat img, img2, img3, gray, blur_img, edges, mask, masked_edges;
    img = imread("/Users/zhipeng/ustc_term2/Opencv/Opencv/Opencv/Digital_imgae_process/4.jpg",-1);
    img2 = img.clone();
    img3 = img.clone();
    if(img.empty()){
        cout << "read file error" << endl;
        return -1;
    }
    //imshow("srcImg", img);
    
    //        :
    cvtColor(img, gray, cv::COLOR_BGR2GRAY);
    GaussianBlur(gray, blur_img, Size(5, 5), 0);
    imshow("1gray", gray);
    
    //         :
    Canny(blur_img, edges, 100, 250);
    imshow("2canny", edges);
    
    //   ROI:            ,            
    mask = Mat::zeros(img.size(), CV_8UC1);
    vector<vector<Point>> contour;
    vector<Point> pts;
    pts.push_back(Point(0, img.rows));//   cols,   rows
    pts.push_back(Point(img.cols/2.0 - 20, img.rows*0.6));
    pts.push_back(Point(img.cols/2.0 + 20, img.rows*0.6));
    pts.push_back(Point(img.cols, img.rows));
    contour.push_back(pts);
    //         。     0,       。   -1,s      
    drawContours(mask, contour, 0, Scalar::all(255), -1);
    edges.copyTo(masked_edges, mask);
    imshow("3mask", mask);
    imshow("3masked_edges", masked_edges);
    
    //   :  hough    
    vector<Vec4i> lines, left_lines, right_lines;// Vec4i -> lines[i][0-3]
    //   lines        ,      xy    
    HoughLinesP(masked_edges, lines, 2, CV_PI/180, 30, 10, 30);
    //       
    vector<double> left_slope, right_slope;
    //Point2d -> left_centers[i].x
    vector<Point2d> left_centers, right_centers;
    list<Vec4i> list_lines;
    
    
    //Part1:   hough    list          
    vector<Vec4i> pro_lines;   //list(after erase) -> vector
    for(auto v:lines)
        pro_lines.push_back(v);

    for(int i=0; i<pro_lines.size(); i++)
    {
        line(img, Point(pro_lines[i][0], pro_lines[i][1]),
             Point(pro_lines[i][2], pro_lines[i][3]) ,Scalar(255,0,0),3,8);
    }
    imshow("houghListLines", img);
    
    
    double slo;
    Point2d center;
    int i = 0;
    for(auto v : lines) //vector->list
        list_lines.push_back(v);
    
    for (auto iter = list_lines.begin(); iter != list_lines.end(); ++i) {
        //slo=(y2-y1)/(x2-x1)
        slo = double((lines[i][3]-lines[i][1]))/double((lines[i][2]-lines[i][0])) ;
        //center=(x1+x2)/2, (y1+y2)/2
        center = Point( double(lines[i][2]+lines[i][0])/2 , double(lines[i][3]+lines[i][1])/2 );
        
        //         erase
        if(fabs(slo)<0.35)
        {
            iter = list_lines.erase(iter);
            continue;
        }
        
        //             
        if(slo > 0)
        {
            right_lines.push_back(lines[i]);
            right_centers.push_back(center);
            right_slope.push_back(slo);
        }
        else
        {
            left_lines.push_back(lines[i]);
            left_centers.push_back(center);
            left_slope.push_back(slo);
        }
        
        *iter = lines[i];
        iter++;
        
    }
    
    //Part2:         erase       
    vector<Vec4i> pro_lines2;   //list(after erase) -> vector
    for(auto v:list_lines)
        pro_lines2.push_back(v);

    for(int i=0; i<pro_lines2.size(); i++)
    {
        line(img2, Point(pro_lines2[i][0], pro_lines2[i][1]),
             Point(pro_lines2[i][2], pro_lines2[i][3]) ,Scalar(0,255,0),3,8);
    }
    imshow("sloSelectLines", img2);
    
    //left to mean slope and center
    double left_len, total_left_len=0, total_left_slope=0;
    Point2d total_left_center(0,0);
    for(int i = 0; i< left_lines.size(); i++)
    {
        //          
        left_len = sqrt( (left_lines[i][2]-left_lines[i][0])*(left_lines[i][2]-left_lines[i][0]) + (left_lines[i][3]-left_lines[i][1])*(left_lines[i][3]-left_lines[i][1]) );
        total_left_slope += left_len * left_slope[i];
        total_left_len += left_len;
        total_left_center = Point(total_left_center.x + left_len * left_centers[i].x , total_left_center.y + left_len * left_centers[i].y );
    }
    
    //             
    double mean_left_slope;
    Point2d mean_left_center;
    mean_left_slope = total_left_slope/ total_left_len;
    mean_left_center = Point( total_left_center.x/total_left_len, total_left_center.y/total_left_len);
    
    //right to mean slope and center
    double right_len, total_right_len=0, total_right_slope=0;
    Point2d total_right_center(0,0);
    for(int i = 0; i< right_lines.size(); i++)
    {
        right_len = sqrt( (right_lines[i][2]-right_lines[i][0])*(right_lines[i][2]-right_lines[i][0]) + (right_lines[i][3]-right_lines[i][1])*(right_lines[i][3]-right_lines[i][1]) );
        total_right_slope = total_right_slope + right_len * right_slope[i];
        total_right_len = total_right_len + right_len;
        total_right_center = Point(total_right_center.x + right_len * right_centers[i].x , total_right_center.y + right_len * right_centers[i].y );
    }
    
    double mean_right_slope;
    Point2d mean_right_center;
    mean_right_slope = total_right_slope/ total_right_len;
    mean_right_center = Point( total_right_center.x/total_right_len, total_right_center.y/total_right_len);
    
    //       y1  y2,                  0.6-1   y   
    double start_y = img.rows * 0.6;//   0,0,     0-0.6   
    double end_y = img.rows;
    //         ,     x1,x2    
    double left_start_x = (start_y - mean_left_center.y)/mean_left_slope + mean_left_center.x;
    double left_end_x = (end_y - mean_left_center.y)/mean_left_slope + mean_left_center.x;
    double right_start_x = (start_y - mean_right_center.y)/mean_right_slope + mean_right_center.x;
    double right_end_x = (end_y - mean_right_center.y)/mean_right_slope + mean_right_center.x;
    
    //Part3:                             
    line(img3, Point(left_start_x, start_y), Point(left_end_x, end_y), Scalar(0,0,255), 3, 8);
    line(img3, Point(right_start_x, start_y), Point(right_end_x, end_y), Scalar(0,0,255), 3, 8);
    
    imshow("4twoLinesInHorizon", img3);
    
    waitKey(0);
    return 0;
}


じっけんけっか