Drawing HoughLins to a certain point
My idea is that all vertical lines should not cross a diagonal one. Therefore I created a method, which returns all points of a diagonal line:
vector<Point> diagonalLines(Mat src) {
vector<Point> hitPoint;
Scalar mu, sigma;
meanStdDev(src, mu, sigma);
Canny(src, dst, mu.val[0] - sigma.val[0], mu.val[0] + sigma.val[0], 3, false);
cvtColor(dst, cdst, CV_GRAY2BGR);
HoughLinesP(dst, lines, 1, CV_PI / 180, 100, 50, 10);
sort(lines.begin(), lines.end(), vec4iSortByX());
for (size_t i = 1; i < lines.size(); i++) {
Vec4i current = lines[i];
Point pt1 = Point(current[0], current[1]);
Point pt2 = Point(current[2], current[3]);
double angle = atan2(pt2.y - pt1.y, pt2.x - pt1.x) * 180.0 / CV_PI;
if (angle != -90 && angle != 90) {
hitPoint.push_back(pt1);
hitPoint.push_back(pt2);
}
}
return hitPoint;
}
After that there's a method to detect all essential vertial lines:
void verticalLines(Mat src) {
double maxLineGap = 200.0;
vector<Vec4i> elemLinesCur, elemLinesPrev;
Scalar mu, sigma;
meanStdDev(src, mu, sigma);
Canny(src, dst, mu.val[0] - sigma.val[0], mu.val[0] + sigma.val[0], 3, false);
cvtColor(dst, cdst, CV_GRAY2BGR);
HoughLinesP(dst, lines, 1, CV_PI / 2, 100, 50, maxLineGap);
sort(lines.begin(), lines.end(), vec4iSortByX());
Vec4i current, previous;
Point pt1, pt2, ppt1, ppt2;
for (size_t i = 1; i < lines.size(); i++) {
current = lines[i];
pt1 = Point(current[0], current[1]);
pt2 = Point(current[2], current[3]);
for (int k = 0; k < maxLineGap; k++) {
for (auto d : diagonalLines(src))
if (pt1 == d)
maxLineGap--;
}
//do some stuff
}
Yes, they similiar. So in for (int k = 0; k < maxLineGap; k++) { //...
I'm trying to say if any point of a vertical line hits a point of diagonal line, make the maxLineGap
shorter, so it goes exactly to the diagonal line and not above of it. It should be something like a intersection and between the intersection points the line should be drawen.
As you could image, this approach doesn't work and the computing time is long. What am I doing wrong?