Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path detection dev #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions tasks/path/locator/cv_solution/cpp_code/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <iostream>
#include <string>
#include <vector>
#include <sys/time.h>
#include <time.h>
#include <map>

namespace constants
Expand Down Expand Up @@ -48,20 +48,19 @@ Logger::~Logger()

string Logger::getDate()
{
struct timeval tv;
gettimeofday(&tv, NULL);
time_t tp = time(NULL);
tm *ts = localtime(&tp);

string date = "[" + to_string(ts->tm_year + 1900) + "-" + to_string(ts->tm_mon + 1) + "-" + to_string(ts->tm_mday)
+ " | " + to_string(ts->tm_hour) + ":" + to_string(ts->tm_min) + ":" + to_string(ts->tm_sec) + "." + to_string(tv.tv_usec) + "]";
+ " | " + to_string(ts->tm_hour) + ":" + to_string(ts->tm_min) + ":" + to_string(ts->tm_sec) + "]";

return date;
}

string Logger::createFileName()
{
return "Logs/PathDetection " + getDate() + ".log";
//return "Logs/PathDetection " + getDate() + ".log";
return "path_logs.log";
}

void Logger::saveLog(int frameNumber, vector<vector<double>> lineValues, vector<double> lineAverage)
Expand Down
1 change: 1 addition & 0 deletions tasks/path/locator/cv_solution/cpp_code/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include <fstream>
#include <vector>

using namespace std;

Expand Down
16 changes: 11 additions & 5 deletions tasks/path/locator/cv_solution/cpp_code/main.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#include "path.hpp"
#include <iostream>

int main()
{
const string pathVideo = "a.mp4";
const string pathImage = "f.png";
const string pathImage = "path1.png";
cv::Mat image = cv::imread(pathImage);

PathDetector detector;
cout << detector.getRotationAngle(image) << endl;
detector.getIntersectionCoordinates(image);

PathDetector detector;
std::cout << detector.getRotationAngle(image) << std::endl;
map<std::string, double> myMap = detector.getIntersectionCoordinates(image);

for (auto elem : myMap)
{
std::cout << elem.first << " " << elem.second << "\n";
}

return 0;
}
81 changes: 30 additions & 51 deletions tasks/path/locator/cv_solution/cpp_code/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,7 @@ PathDetector::~PathDetector()

void PathDetector::run()
{
isRunning = true;

createControlWindow();

while (isRunning)
{
frame = captureSingleFrame();

vector<double> tempVector = findLinesParameters(frame);

updateParameters(tempVector);

printParameters("ActualAfter", actualParameters);

countAngleDifference();

//cout << "rotationAngle:" << getRotationAngle() << "\n";

//getIntersectionCoordinates();

printFrame(frame);

frameNumber++;

checkIfRunning();
}
}

void PathDetector::createControlWindow()
Expand All @@ -105,9 +80,9 @@ vector<double> PathDetector::findLinesParameters(cv::Mat frame)
{
vector<vector<double>> tempParameters(4);
vector<double> averageParameters(4);

vector<cv::Vec2f> lines = detectLines(frame);

if (!lines.empty())
{
tempParameters = sortParameters(lines);
Expand Down Expand Up @@ -188,7 +163,7 @@ vector<double>PathDetector::countAverage(vector<vector<double>> &tempParameters)
return averageParameters;
}

vector<cv::Vec2f> PathDetector::detectLines(cv::Mat &frame)
vector<cv::Vec2f> PathDetector::detectLines(cv::Mat frame)
{
const int rho = 1; //The resolution of the parameter \rho in pixels
const int tresh = 50; //The minimum number of intersections to “detect” a line
Expand All @@ -198,7 +173,7 @@ vector<cv::Vec2f> PathDetector::detectLines(cv::Mat &frame)
vector<cv::Vec2f> lines;

prepareImage(frame);

HoughLines(frame, lines, rho, theta, tresh, srn, stn);

return lines;
Expand Down Expand Up @@ -280,8 +255,6 @@ void PathDetector::updateParameters(vector<double> vector)
void PathDetector::printFrame(cv::Mat printedFrame)
{
countCoordinates(printedFrame);

cv::imshow("Path", printedFrame);
}

void PathDetector::countCoordinates(cv::Mat &printedFrame)
Expand All @@ -305,6 +278,10 @@ void PathDetector::countCoordinates(cv::Mat &printedFrame)
pt2.x = cvRound(x0 - 1000 * -sin(actualParameters[2]));
pt2.y = cvRound(y0 - 1000 * cos(actualParameters[2]));
line(printedFrame, pt1, pt2, cv::Scalar(0, 0, 255), 3, CV_AA);



imwrite("lines.png", printedFrame);
}

void PathDetector::countAngleDifference()
Expand All @@ -327,57 +304,59 @@ void PathDetector::countAngleDifference()
}
}

int PathDetector::getRotationAngle(cv::Mat &frame)
int PathDetector::getRotationAngle(const cv::Mat& frame)
{
vector<double> tempVector = findLinesParameters(frame);

cv::Mat cloned_frame = frame.clone();
vector<double> tempVector = findLinesParameters(cloned_frame);

updateParameters(tempVector);
printParameters("ActualAfter", actualParameters);

//printParameters("ActualAfter", actualParameters);

countAngleDifference();

printFrame(frame);
printFrame(cloned_frame);

return abs(angleDifference[0]) < abs(angleDifference[1]) ? int(angleDifference[1]) : int(angleDifference[0]);
}

void PathDetector::normalizeCoordinates(double& x, double& y, cv::Mat frame)
{
x = (x - (frame.size().width/2))/(frame.size().width/2);
y = ((frame.size().height/2) - y)/(frame.size().height/2);
x = (abs(x) - (frame.size().width/2))/(frame.size().width/2);
y = ((frame.size().height/2) - abs(y))/(frame.size().height/2);
}

map<string,int> PathDetector::getIntersectionCoordinates(cv::Mat &frame)
map<string,double> PathDetector::getIntersectionCoordinates(const cv::Mat& frame)
{
vector<double> tempVector = findLinesParameters(frame);

cv::Mat cloned_frame = frame.clone();
vector<double> tempVector = findLinesParameters(cloned_frame);

updateParameters(tempVector);
printParameters("ActualAfter", actualParameters);

//printParameters("ActualAfter", actualParameters);

/*
A1*x + B1*y + C1 = 0, A2*x + B2*y + C2 = 0
A = cosTheta, B = sinTheta, C = Rho

X = (B1 * C1 - B2 * C2) / D
X = (B1 * C2 - B2 * C1) / D
Y = (A1 * C2 - A2 * C1) / D
where D = A1 * B2 - A2 * B2
where D = A1 * B2 - A2 * B1
*/
map<string, int> coordinates;
map<string, double> coordinates;
double d,x,y;

d = cos(actualParameters[0])*sin(actualParameters[2]) - cos(actualParameters[2])*sin(actualParameters[0]);
x = (sin(actualParameters[0]) * actualParameters[1] - sin(actualParameters[2])*actualParameters[3])/d;
x = (sin(actualParameters[0]) * actualParameters[3] - sin(actualParameters[2])*actualParameters[1])/d;
y = (cos(actualParameters[0])*actualParameters[3] - cos(actualParameters[2])*actualParameters[1])/d;

normalizeCoordinates(x, y, frame);
normalizeCoordinates(x, y, cloned_frame);

printFrame(frame);
printFrame(cloned_frame);

coordinates["x"] = x;
coordinates["y"] = y;

return coordinates;
}

Expand Down
7 changes: 3 additions & 4 deletions tasks/path/locator/cv_solution/cpp_code/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class PathDetector
~PathDetector();

void run();
int getRotationAngle(cv::Mat &image);
map<string,int> getIntersectionCoordinates(cv::Mat &image);
int getRotationAngle(const cv::Mat& image);
map<string,double> getIntersectionCoordinates(const cv::Mat& image);

private:
bool isRunning = false;
Expand Down Expand Up @@ -45,7 +45,6 @@ class PathDetector
std::vector<double> angleDifference;
double momentumPercent = 0.4;
cv::VideoCapture videoCap;
cv::Mat frame;
cv::Mat image;
Logger logger;

Expand All @@ -66,7 +65,7 @@ class PathDetector
void assignLines(std::vector<cv::Vec2f> &lines, size_t &i, std::vector<std::vector<double>> &tempParameters);
bool isFirstLine(std::vector<cv::Vec2f> &lines, const size_t &i);
void assignFirstParameters(std::vector<double> & vector);
std::vector<cv::Vec2f> detectLines(cv::Mat &frame);
std::vector<cv::Vec2f> detectLines(cv::Mat frame);

void prepareImage(cv::Mat &frame);
void cannyEdges(cv::Mat &blurredImg);
Expand Down
31 changes: 31 additions & 0 deletions tasks/path/locator/cv_solution/cpp_code/wrapper/wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#define BOOST_PYTHON_STATIC_LIB
#define PY_ARRAY_UNIQUE_SYMBOL pbcvt_ARRAY_API
#include <boost/python.hpp>
#include <boost/python/dict.hpp>
#include <pyboostcvconverter/pyboostcvconverter.hpp>
#include <opencv2/opencv.hpp>
#include "path.hpp"

using namespace boost::python;

#if (PY_VERSION_HEX >= 0x03000000)
static void *init_ar() {
#else
static void init_ar() {
#endif
Py_Initialize();

import_array();
return NUMPY_IMPORT_ARRAY_RETVAL;
}

BOOST_PYTHON_MODULE (path_detection_cv) {
init_ar();
to_python_converter<cv::Mat,pbcvt::matToNDArrayBoostConverter>();
pbcvt ::matFromNDArrayBoostConverter();

class_<PathDetector>("path_detector", init<>())
.def("get_path_cordinates", &PathDetector::getIntersectionCoordinates)
.def("get_rotation_angle", &PathDetector::getRotationAngle)
;
}