Ask Your Question

Drawing HoughLins to a certain point

asked 2017-07-27 03:19:49 -0500

vickal93 gravatar image

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) {
   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) 
       //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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-07-27 08:34:35 -0500

KjMag gravatar image

I understand that you need two things:

  1. Detect if a vertical line intersects a diagonal one (and by diagonal we really mean neither vertical nor horizontal one)
  2. Calculate the point of intersection

1. Intersection detection

Let v1 and v2 be the points that desribe the ends of a vertical line and d1 and d2 be analogical points of a diagonal one. Also let's assume v1.y > v2.y and d1.y > d2.y. Intersection takes place if v1.y > d2.y and in the same time v2.y < d1.y.

So, for each vertical line you check, you should determine which point has greater Y (i.e. vertical) coordinate, do the same for the diagonal one, and then do the check as described above.

2. Finding intersection point

You need to find lines equation, which is trivial since you have two points of the diagonal line and in case of vertical line equation is just x = d1.x . Put d1.x into diagonal line equation to find the intersection point.

When you have it, just replace the Y coordinate of your vertical line with the Y coordinate of the intersection point. Voila - your vertical line ends at the point of intersection with the diagonal line now.

edit flag offensive delete link more


So, I tried in the method verticalLines to do this one in the for loop: if (diagonalLine[i-1].y > pt2.y && diagonalLine[i].y < pt1.y) { std:: cout << "Intersection: "<< pt2.x << "\n"; before I implemented of course: vector<Point> diagonalLine = diagonalLines(src);

vickal93 gravatar imagevickal93 ( 2017-07-27 17:57:56 -0500 )edit

... this doesn't work. Especially, I just return a vector<Point> from diagonalLines()... Could you please help me out to fix this?

vickal93 gravatar imagevickal93 ( 2017-07-27 17:59:00 -0500 )edit

What is the relation between pt1.y and pt2.y and between diagonalLine[i-1] and diagonalLine[i]? Is the former always bigger than the latter in case of both point pairs? Don't you need to check that in each case?

KjMag gravatar imageKjMag ( 2017-08-03 07:41:36 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-07-27 03:19:49 -0500

Seen: 119 times

Last updated: Jul 27 '17