Skip to content

path locator cv_solution

BartoszStucke edited this page Oct 31, 2019 · 2 revisions

Xavier_ROV4/tasks/path/locator/cv_solution/

Code is used to find angle of which boat has to turn following the path and a place of the turn. It was created with usage of OpenCV 4.1 library. It is based on Hough Lines algorithm which finds lines of a path contour on thresholded image.

void PathDetector::prepareImage(cv::Mat &frame) { cv::cvtColor(frame, frame, CV_BGR2HSV);

thresholdImage(frame);

doMorphOperations(frame);

blurrImage(frame);

cannyEdges(frame);

}

vectorcv::Vec2f PathDetector::detectLines(cv::Mat frame) { const int rho = 1; const int tresh = 50; const int srn = 0, stn = 0; const double theta = CV_PI / 180;

vector<cv::Vec2f> lines;

prepareImage(frame);

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

return lines;

}

Program sorts lines for 2 groups based on their angle (one group for one part of the path) and finds their average. This enables to find the axis of the path and their intersection point.

getIntersectionCoordinates function returns map: X:x_coordinates, Y:y_coordinates

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

updateParameters(tempVector);

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[3] - sin(actualParameters[2])*actualParameters[1])/d;
y = (cos(actualParameters[0])*actualParameters[3] - cos(actualParameters[2])*actualParameters[1])/d;

normalizeCoordinates(x, y, cloned_frame);

printFrame(cloned_frame);

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

return coordinates;

}

getRotationAngle function returns integer of an turning angle

int PathDetector::getRotationAngle(const cv::Mat& frame) { cv::Mat cloned_frame = frame.clone(); vector tempVector = findLinesParameters(cloned_frame);

updateParameters(tempVector);

countAngleDifference();

printFrame(cloned_frame);

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

}

Clone this wiki locally