#include #include #include #include #include #include using namespace std; using namespace cv; using namespace raspicam; // Image Processing variables Mat frame, Matrix, framePers, frameGray, frameThresh, frameEdge, frameFinal, frameFinalDuplicate, frameFinalDuplicate1; Mat ROILane, ROILaneEnd; int LeftLanePos, RightLanePos, frameCenter, laneCenter, Result, laneEnd; RaspiCam_Cv Camera; stringstream ss; vector histrogramLane; vector histrogramLaneEnd; Point2f Source[] = {Point2f(40,135),Point2f(360,135),Point2f(0,185), Point2f(400,185)}; Point2f Destination[] = {Point2f(100,0),Point2f(280,0),Point2f(100,240), Point2f(280,240)}; //Machine Learning variables CascadeClassifier Stop_Cascade, Object_Cascade, Object2_Cascade; Mat frame_Stop, RoI_Stop, gray_Stop, frame_Object, RoI_Object, gray_Object, frame_Object2, RoI_Object2, gray_Object2; vector Stop, Object, Object2; int dist_Stop, dist_Object; void Setup ( int argc,char **argv, RaspiCam_Cv &Camera ) { Camera.set ( CAP_PROP_FRAME_WIDTH, ( "-w",argc,argv,400 ) ); Camera.set ( CAP_PROP_FRAME_HEIGHT, ( "-h",argc,argv,240 ) ); Camera.set ( CAP_PROP_BRIGHTNESS, ( "-br",argc,argv,50 ) ); Camera.set ( CAP_PROP_CONTRAST ,( "-co",argc,argv,50 ) ); Camera.set ( CAP_PROP_SATURATION, ( "-sa",argc,argv,50 ) ); Camera.set ( CAP_PROP_GAIN, ( "-g",argc,argv ,50 ) ); Camera.set ( CAP_PROP_FPS, ( "-fps",argc,argv,0)); } void Capture() { Camera.grab(); Camera.retrieve( frame); cvtColor(frame, frame_Stop, COLOR_BGR2RGB); cvtColor(frame, frame_Object, COLOR_BGR2RGB); cvtColor(frame, frame_Object2, COLOR_BGR2RGB); cvtColor(frame, frame, COLOR_BGR2RGB); } void Perspective() { line(frame,Source[0], Source[1], Scalar(0,255,255), 2); line(frame,Source[1], Source[3], Scalar(0,255,255), 2); line(frame,Source[3], Source[2], Scalar(0,255,255), 2); line(frame,Source[2], Source[0], Scalar(0,255,255), 2); Matrix = getPerspectiveTransform(Source, Destination); warpPerspective(frame, framePers, Matrix, Size(400,240)); } void Threshold() { cvtColor(framePers, frameGray, COLOR_RGB2GRAY); inRange(frameGray, 200, 255, frameThresh); Canny(frameGray,frameEdge, 900, 900, 3, false); add(frameThresh, frameEdge, frameFinal); cvtColor(frameFinal, frameFinal, COLOR_GRAY2RGB); cvtColor(frameFinal, frameFinalDuplicate, COLOR_RGB2BGR); //used in histrogram function only cvtColor(frameFinal, frameFinalDuplicate1, COLOR_RGB2BGR); //used in histrogram function only } void Histrogram() { histrogramLane.resize(400); histrogramLane.clear(); for(int i=0; i<400; i++) //frame.size().width = 400 { ROILane = frameFinalDuplicate(Rect(i,140,1,100)); divide(255, ROILane, ROILane); histrogramLane.push_back((int)(sum(ROILane)[0])); } histrogramLaneEnd.resize(400); histrogramLaneEnd.clear(); for (int i = 0; i < 400; i++) { ROILaneEnd = frameFinalDuplicate1(Rect(i, 0, 1, 240)); divide(255, ROILaneEnd, ROILaneEnd); histrogramLaneEnd.push_back((int)(sum(ROILaneEnd)[0])); } laneEnd = sum(histrogramLaneEnd)[0]; cout<<"Lane END = "<:: iterator LeftPtr; LeftPtr = max_element(histrogramLane.begin(), histrogramLane.begin() + 150); LeftLanePos = distance(histrogramLane.begin(), LeftPtr); vector:: iterator RightPtr; RightPtr = max_element(histrogramLane.begin() +250, histrogramLane.end()); RightLanePos = distance(histrogramLane.begin(), RightPtr); line(frameFinal, Point2f(LeftLanePos, 0), Point2f(LeftLanePos, 240), Scalar(0, 255,0), 2); line(frameFinal, Point2f(RightLanePos, 0), Point2f(RightLanePos, 240), Scalar(0,255,0), 2); } void LaneCenter() { laneCenter = (RightLanePos-LeftLanePos)/2 +LeftLanePos; frameCenter = 188; line(frameFinal, Point2f(laneCenter,0), Point2f(laneCenter,240), Scalar(0,255,0), 3); line(frameFinal, Point2f(frameCenter,0), Point2f(frameCenter,240), Scalar(255,0,0), 3); Result = laneCenter-frameCenter; } void Stop_detection() { if(!Stop_Cascade.load("//home//pi//Desktop//MACHINE LEARNING//red_traffic.xml")) { printf("Unable to open stop cascade file"); } RoI_Stop = frame_Stop(Rect(200,0,200,240)); cvtColor(RoI_Stop, gray_Stop, COLOR_RGB2GRAY); equalizeHist(gray_Stop, gray_Stop); Stop_Cascade.detectMultiScale(gray_Stop, Stop); for(int i=0; i 3000) { digitalWrite(21, 1); digitalWrite(22, 1); //decimal = 7 digitalWrite(23, 1); digitalWrite(24, 0); cout<<"Lane End"<0 && Result <10) { digitalWrite(21, 1); digitalWrite(22, 0); //decimal = 1 digitalWrite(23, 0); digitalWrite(24, 0); cout<<"Right1"<=10 && Result <20) { digitalWrite(21, 0); digitalWrite(22, 1); //decimal = 2 digitalWrite(23, 0); digitalWrite(24, 0); cout<<"Right2"<20) { digitalWrite(21, 1); digitalWrite(22, 1); //decimal = 3 digitalWrite(23, 0); digitalWrite(24, 0); cout<<"Right3"<-10) { digitalWrite(21, 0); digitalWrite(22, 0); //decimal = 4 digitalWrite(23, 1); digitalWrite(24, 0); cout<<"Left1"<-20) { digitalWrite(21, 1); digitalWrite(22, 0); //decimal = 5 digitalWrite(23, 1); digitalWrite(24, 0); cout<<"Left2"< 3000) { ss.str(" "); ss.clear(); ss<<" Lane End"; putText(frame, ss.str(), Point2f(1,50), 0,1, Scalar(255,0,0), 2); } else if (Result == 0) { ss.str(" "); ss.clear(); ss<<"Result = "< 0) { ss.str(" "); ss.clear(); ss<<"Result = "< elapsed_seconds = end-start; float t = elapsed_seconds.count(); int FPS = 1/t; //cout<<"FPS = "<