Você está na página 1de 10

/// Real Time Road Lane Detection

/* Computer Vision Tutorials

by Shafik Alsalem (c)

2018

shafikalsalem@gmail.com

*/

#include <opencv2/highgui/highgui.hpp>

#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>

using namespace std;

using namespace cv;

int main()

VideoCapture cap(0);

if(!cap.isOpened())

cout<<"error";

return -1;

}
Mat frame1(640,480,CV_8UC3);

Mat whiteLane(640,480,CV_8UC3);

Mat yellowLane(640,480,CV_8UC3);

Mat LinesImg(640,480,CV_8UC3);

Mat HSV_Img(640,480,CV_8UC3);

while(1)

bool b=cap.read(frame1);

if(!b)

cout<<"err";

cap.open("laneD.mp4");

cap.read(frame1);

// break;

//resize image

resize(frame1, frame1, Size(640,480) );

//Sum the colour values in each channel

cv::Scalar sumImg=sum(frame1);

//normalise by the number of pixes in the image to obtain an extimate for the
illuminant

cv::Scalar illum=sumImg/(frame1.rows*frame1.cols);
// Split the image into different channels

std::vector<cv::Mat> rgbChannels(3);

cv::split(frame1, rgbChannels);

//Assign the three colour channels to CV::Mat variables for processing

cv::Mat redImg=rgbChannels[2];

cv::Mat greenImg=rgbChannels[1];

cv::Mat blueImg=rgbChannels[0];

//calculate scale factor for normalisation you can use 255 instead

double scale=(illum(0)+illum(1)+illum(2))/3;

//correct for illuminant (white balancing)

redImg=redImg*scale/illum(2);

greenImg=greenImg*scale/illum(1);

blueImg=blueImg*scale/illum(0);

//Assign the processed channels back into vector to use in the cv::merge()
function
rgbChannels[0]=blueImg;

rgbChannels[1]=greenImg;

rgbChannels[2]=redImg;

/// Merge the processed colour channels

Mat frame;

merge(rgbChannels, frame);

//Create a window

// convert our img to HSV Space

cvtColor(frame, HSV_Img, CV_RGB2HSV);

//white color thresholding

Scalar whiteMinScalar = Scalar(191,178,176);

Scalar whiteMaxScalar = Scalar(111,93,255);

inRange(HSV_Img, whiteMinScalar, whiteMaxScalar, whiteLane);

//yellow color thresholding

Scalar yellowMinScalar = Scalar(65,122,143);

Scalar yellowMaxScalar = Scalar(101,255,255);


inRange(HSV_Img, yellowMinScalar, yellowMaxScalar, yellowLane);

//namedWindow("framme");

//imshow("framme",HSV_Img);

//combine our two images in one image

addWeighted(whiteLane, 1.0, yellowLane, 1.0, 0.0, LinesImg);

// Edge detection using canny detector

int minCannyThreshold = 190;

int maxCannyThreshold = 230;

Canny(LinesImg, LinesImg, minCannyThreshold, maxCannyThreshold, 5, true);

// Morphological Operation

Mat k=getStructuringElement(CV_SHAPE_RECT,Size(9,9)); //MATLAB :k=Ones(9)

morphologyEx(LinesImg, LinesImg, MORPH_CLOSE, k);

// now applying hough transform TO dETECT Lines in our image

vector<Vec4i> lines;

double rho = 1;

double theta = CV_PI/180;

int threshold = 43;

double minLinLength = 35;

double maxLineGap = 300;

HoughLinesP(LinesImg, lines, rho, theta, threshold, minLinLength, maxLineGap );


float stt[3][yellowLane.rows][yellowLane.cols];

int gt[4];

for (int col = 0; col < yellowLane.cols; col++)

for (int row = 0; row < yellowLane.rows; row++)

Vec3i color = frame.at<Vec3f>(row,col);

int blue = color.val[0];

int green = color.val[1];

int red = color.val[2];

if((blue!=0)&&(green!=0)&&(red!=0))

printf("%d %d %d \n",blue,green,red);

//stt[0][row][col] = (LinesImg.at<uchar>(color.val[0],row,col));

//stt[1][row][col] =(double)(LinesImg.at<uchar>(color.val[1],row,col));

//stt[2][row][col] =(double)(LinesImg.at<uchar>(color.val[2],row,col));

break;

//for (int col = 0; col < yellowLane.cols; col++)

//for (int row = 0; row < yellowLane.rows; row++)

//{

//if(stt[row][col]==255)

//{

//gt[0]=row;

//gt[1]=col;

//break;

//}
//}

////printf("%d \n",gt[0]);

//for (int col = yellowLane.cols; col >0; col--)

//for (int row = yellowLane.rows; row > 0; row--)

//{

//if(stt[row][col]==255)

//{

//gt[2]=row;

//gt[3]=col;

//break;

//}

//}

//printf("%lf \n",stt[0][yellowLane.rows]);

//draw our lines

//for( size_t i = 0; i < lines.size(); i++ )

//{

//Vec4i l = lines[i]; // we have 4 elements p1=x1,y1 p2= x2,y2

//Scalar greenColor= Scalar(0,250,30); // B=0 G=250 R=30

//Scalar redColor= Scalar(0,0,255);

//stt++;

//if((stt==1)||(stt==4))

//{

//cout << line[i] << end1;

//}

//line( frame, Point(l[0], l[1]), Point(l[2], l[3]), greenColor, 50, CV_AA);

////if(stt>4)

////stt=0;
////line( frame, Point(l2[0], l2[1]), Point(l2[2], l2[3]), redColor , 3,
CV_AA);

//}

//line(frame,Point((gt[2] + gt[0])/2,gt[1]),Point((gt[2] +
gt[0])/2,gt[3]),Scalar(255),2,8,0);

namedWindow("frame");

imshow("frame",frame);

namedWindow("YellowLane");

imshow("YellowLane",yellowLane);

//namedWindow("WhiteLane");

//imshow("WhiteLane",HSV_Img);

///// Separate the image in 3 places ( B, G and R )

//vector<Mat> bgr_planes;

//split( HSV_Img, bgr_planes );

///// Establish the number of bins

//int histSize = 256;

///// Set the ranges ( for B,G,R) )

//float range[] = { 0, 256 } ;

//const float* histRange = { range };

//bool uniform = true; bool accumulate = false;


//Mat b_hist, g_hist, r_hist;

///// Compute the histograms:

//calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange,


uniform, accumulate );

//calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange,


uniform, accumulate );

//calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange,


uniform, accumulate );

//// Draw the histograms for B, G and R

//int hist_w = 512; int hist_h = 400;

//int bin_w = cvRound( (double) hist_w/histSize );

//Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );

///// Normalize the result to [ 0, histImage.rows ]

//normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

//normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

//normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

///// Draw for each channel

//for( int i = 1; i < histSize; i++ )

//{

//line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-


1)) ) ,

//Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),

//Scalar( 255, 0, 0), 2, 8, 0 );

//line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-


1)) ) ,

//Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),


//Scalar( 0, 255, 0), 2, 8, 0 );

//line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-


1)) ) ,

//Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),

//Scalar( 0, 0, 255), 2, 8, 0 );

//}

if(waitKey(30)==27)

cout<<"esc";

break;

return 0;

Você também pode gostar