OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Tue, 09 May 2017 03:33:44 -0500How to calculate slope, length and distance between hough lines?http://answers.opencv.org/question/146922/how-to-calculate-slope-length-and-distance-between-hough-lines/ To detect lines from my image I used problabistic hough transform. I got 15 lines from my image. now I want to calculate slope, length of each line and to calculate distance between nearest lines? Can anyone suggest me ideas or share code? Thanks in advanceMon, 08 May 2017 06:12:42 -0500http://answers.opencv.org/question/146922/how-to-calculate-slope-length-and-distance-between-hough-lines/Answer by berak for <p>To detect lines from my image I used problabistic hough transform. I got 15 lines from my image. now I want to calculate slope, length of each line and to calculate distance between nearest lines? Can anyone suggest me ideas or share code? Thanks in advance</p>
http://answers.opencv.org/question/146922/how-to-calculate-slope-length-and-distance-between-hough-lines/?answer=146926#post-id-146926really, this is not so much an opencv problem, but a maths one ..
[houghlinesP](http://docs.opencv.org/master/dd/d1a/group__imgproc__feature.html#ga8618180a5948286384e3b7ca02f6feeb) already gives you the endpoints of the line,
Vec4i line = lines[i];
Point a(line[0], line[1]);
Point b(line[2], line[3]);
so
* the [slope](https://en.wikipedia.org/wiki/Slope) is dy/dx , or
float(b.y - a.y) / float(b.x - a.x);
* the length is the euclidean distance, or:
norm(a,b);
no idea, what you mean with the distance, try to explain ?Mon, 08 May 2017 06:31:19 -0500http://answers.opencv.org/question/146922/how-to-calculate-slope-length-and-distance-between-hough-lines/?answer=146926#post-id-146926Comment by berak for <p>really, this is not so much an opencv problem, but a maths one ..</p>
<p><a href="http://docs.opencv.org/master/dd/d1a/group__imgproc__feature.html#ga8618180a5948286384e3b7ca02f6feeb">houghlinesP</a> already gives you the endpoints of the line, </p>
<pre><code>Vec4i line = lines[i];
Point a(line[0], line[1]);
Point b(line[2], line[3]);
</code></pre>
<p>so </p>
<ul>
<li><p>the <a href="https://en.wikipedia.org/wiki/Slope">slope</a> is dy/dx , or</p>
<p>float(b.y - a.y) / float(b.x - a.x);</p></li>
<li><p>the length is the euclidean distance, or:</p>
<p>norm(a,b);</p></li>
</ul>
<p>no idea, what you mean with the distance, try to explain ?</p>
http://answers.opencv.org/question/146922/how-to-calculate-slope-length-and-distance-between-hough-lines/?comment=147241#post-id-147241the distance is somewhat tricky. the link, @LBerger sent, assumes *infinite* lines, this probably won't work here.
by best guess, atm: take the shortest of the 4 point distances: a1,a2 a1,b2 b1,a2 b1,b2Tue, 09 May 2017 03:33:44 -0500http://answers.opencv.org/question/146922/how-to-calculate-slope-length-and-distance-between-hough-lines/?comment=147241#post-id-147241Comment by LBerger for <p>really, this is not so much an opencv problem, but a maths one ..</p>
<p><a href="http://docs.opencv.org/master/dd/d1a/group__imgproc__feature.html#ga8618180a5948286384e3b7ca02f6feeb">houghlinesP</a> already gives you the endpoints of the line, </p>
<pre><code>Vec4i line = lines[i];
Point a(line[0], line[1]);
Point b(line[2], line[3]);
</code></pre>
<p>so </p>
<ul>
<li><p>the <a href="https://en.wikipedia.org/wiki/Slope">slope</a> is dy/dx , or</p>
<p>float(b.y - a.y) / float(b.x - a.x);</p></li>
<li><p>the length is the euclidean distance, or:</p>
<p>norm(a,b);</p></li>
</ul>
<p>no idea, what you mean with the distance, try to explain ?</p>
http://answers.opencv.org/question/146922/how-to-calculate-slope-length-and-distance-between-hough-lines/?comment=146942#post-id-146942may be [this](https://en.wikipedia.org/wiki/Distance_between_two_straight_lines)Mon, 08 May 2017 07:28:33 -0500http://answers.opencv.org/question/146922/how-to-calculate-slope-length-and-distance-between-hough-lines/?comment=146942#post-id-146942Comment by Lavanya for <p>really, this is not so much an opencv problem, but a maths one ..</p>
<p><a href="http://docs.opencv.org/master/dd/d1a/group__imgproc__feature.html#ga8618180a5948286384e3b7ca02f6feeb">houghlinesP</a> already gives you the endpoints of the line, </p>
<pre><code>Vec4i line = lines[i];
Point a(line[0], line[1]);
Point b(line[2], line[3]);
</code></pre>
<p>so </p>
<ul>
<li><p>the <a href="https://en.wikipedia.org/wiki/Slope">slope</a> is dy/dx , or</p>
<p>float(b.y - a.y) / float(b.x - a.x);</p></li>
<li><p>the length is the euclidean distance, or:</p>
<p>norm(a,b);</p></li>
</ul>
<p>no idea, what you mean with the distance, try to explain ?</p>
http://answers.opencv.org/question/146922/how-to-calculate-slope-length-and-distance-between-hough-lines/?comment=146944#post-id-146944In my image I got two parallel lines. Now I calculated slope and length of each line respectively. I want to calculate distance between these two parallel lines.Mon, 08 May 2017 07:46:12 -0500http://answers.opencv.org/question/146922/how-to-calculate-slope-length-and-distance-between-hough-lines/?comment=146944#post-id-146944