diff --git a/Processing/processing.cpp b/Processing/processing.cpp index e76c0d1..c82b98a 100644 --- a/Processing/processing.cpp +++ b/Processing/processing.cpp @@ -8,15 +8,49 @@ Processing::~Processing() { } +static double angle( Point pt1, Point pt2, Point pt0 ) +{ + double dx1 = pt1.x - pt0.x; + double dy1 = pt1.y - pt0.y; + double dx2 = pt2.x - pt0.x; + double dy2 = pt2.y - pt0.y; + return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10); +} + void Processing::processImage(Mat& inputPicture, int thresholdValue, int gaussKernelSize) { //Idea here is: Processing module consists of two methods: // One (this) to do all kinds of stuff to the picture (grayscale conversion, threshold, gauss etc etc) // And one (the other one) to segment the lines. // No return value here as the input is passed by reference -> directly modified. + vector lines; + + cvtColor(inputPicture, inputPicture, COLOR_BGR2GRAY); threshold(inputPicture, inputPicture, thresholdValue, 255, THRESH_BINARY); GaussianBlur(inputPicture, inputPicture, Size(gaussKernelSize, gaussKernelSize), 0); + + Canny(inputPicture, inputPicture, 50, 200, 3); + + HoughLinesP(inputPicture, lines, 1, CV_PI/180, 150, 0, 0); + //Draw lines + for( size_t i = 0; i < lines.size(); i++ ) + { + line( inputPicture, Point(lines[i][0], lines[i][1]), + Point( lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 ); + } + + imshow("Result", inputPicture); + waitKey(0); + destroyWindow("Result"); + + for( size_t i = 0; i < lines.size(); i++ ) + { + line( inputPicture, Point(lines[i][0], lines[i][1]), + Point( lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 ); + + } + } std::vector Processing::calculateLineSegments(const Mat& inputPicture) diff --git a/Processing/processing.h b/Processing/processing.h index 48aadf6..d6a4a8b 100644 --- a/Processing/processing.h +++ b/Processing/processing.h @@ -1,8 +1,10 @@ #include #include #include +#include using namespace cv; +using namespace std; class Processing { diff --git a/Utils/utils.h b/Utils/utils.h index d837d3c..c30d401 100644 --- a/Utils/utils.h +++ b/Utils/utils.h @@ -1,5 +1,11 @@ #pragma once +#include + +using namespace cv; +using namespace std; + + class LFRPoint { private: @@ -39,4 +45,18 @@ public: LFRLine(LFRPoint start, LFRVector dir); LFRLine(LFRPoint start, LFRPoint end); ~LFRLine(); +}; + +class VectorOfLines{ + private: + float theta; + float m; + float zeroPoint; + public: + VectorOfLines(); + ~VectorOfLines(); + float calcTheta(cv::Point x, Point y); + float calcM(float theta); + float calcZeroPoint(cv::Point x, float m); + }; \ No newline at end of file diff --git a/autonomous_mode_main.cpp b/autonomous_mode_main.cpp index 7f9834f..9f0f10c 100644 --- a/autonomous_mode_main.cpp +++ b/autonomous_mode_main.cpp @@ -6,10 +6,10 @@ int main(void) //Disable opencv logging messages cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_WARNING); - const int thresholdBinary = 110; + const int thresholdBinary = 140; const int videoHeight = 240; const int videoWidth = 320; - const int gaussKernelSize = 5; + const int gaussKernelSize = 11; LFR lfr(videoHeight, videoWidth, thresholdBinary, gaussKernelSize); lfr.startLoop(); diff --git a/lfr.cpp b/lfr.cpp index 0161fcb..7496555 100644 --- a/lfr.cpp +++ b/lfr.cpp @@ -22,7 +22,7 @@ void LFR::loop() namedWindow("Display window"); while(iAmLooping) { - Mat image = input.readWebcam(); + Mat image = input.readFile("C:\\Line-Following-Robot\\Test_data"); processing.processImage(image, this->thresholdBinary, this->gaussKernelSize); std::vector lines = processing.calculateLineSegments(image); imshow("Display window", image);