OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Tue, 12 Sep 2017 08:06:36 -0500Two line segments collinearityhttp://answers.opencv.org/question/173943/two-line-segments-collinearity/I've faced with an simple problem: I can't write a function which tells me whether two line segments are collinear or not.
There is a piece of code:
<pre><code>
double isVectorsCollinear(Vec4i segment_1, Vec4i segment_2)
{
/* First vector */
double dx_1 = segment_1[2] - segment_1[0];
double dy_1 = segment_1[3] - segment_1[1];
/* Second vector */
double dx_2 = segment_2[2] - segment_2[0];
double dy_2 = segment_1[3] - segment_1[1];
//
return fabs(dx_1 * dy_2 - dx_2 * dy_1);
}
</code></pre>
The segments come from HoughLinesP which detects segmenst perfectly on simple image manually generated. But the result of this running this code is 10836, which is obviusly not correct. I guess it is because of pixel's coordinates integer values, but I'm not sure. It is very weird for me. What am I doing wrong?
Also I've tried another way to test collinearity which gave me correct result. Just consider the difference between dot and vectors lengths product
<pre><code>
double isVectorsCollinear(Vec4i segment_1, Vec4i segment_2)
{
Point vec_1 = Point(segment_1[2], segment_1[3]) - Point(segment_1[0], segment_1[1]);
Point vec_2 = Point(segment_2[2], segment_2[3]) - Point(segment_2[0], segment_2[1]);
return fabs(norm(vec_1) * norm(vec_2) - vec_1.dot(vec_2));
}
</code></pre>
I can use this code, but firstly, it is more computationally demanding (because of norm function calling), and secondly, I am really interested in why my first solution doesn't work?Sun, 10 Sep 2017 04:48:18 -0500http://answers.opencv.org/question/173943/two-line-segments-collinearity/Answer by VxW for <p>I've faced with an simple problem: I can't write a function which tells me whether two line segments are collinear or not.
There is a piece of code:
</p><pre><code>
double isVectorsCollinear(Vec4i segment_1, Vec4i segment_2)
{
/* First vector <em>/
double dx_1 = segment_1[2] - segment_1[0];
double dy_1 = segment_1[3] - segment_1[1];
/</em> Second vector */
double dx_2 = segment_2[2] - segment_2[0];
double dy_2 = segment_1[3] - segment_1[1];
//
return fabs(dx_1 * dy_2 - dx_2 * dy_1);
}
</code></pre><p></p>
<p>The segments come from HoughLinesP which detects segmenst perfectly on simple image manually generated. But the result of this running this code is 10836, which is obviusly not correct. I guess it is because of pixel's coordinates integer values, but I'm not sure. It is very weird for me. What am I doing wrong?
Also I've tried another way to test collinearity which gave me correct result. Just consider the difference between dot and vectors lengths product
</p><pre><code>
double isVectorsCollinear(Vec4i segment_1, Vec4i segment_2)
{
Point vec_1 = Point(segment_1[2], segment_1[3]) - Point(segment_1[0], segment_1[1]);
Point vec_2 = Point(segment_2[2], segment_2[3]) - Point(segment_2[0], segment_2[1]);
return fabs(norm(vec_1) * norm(vec_2) - vec_1.dot(vec_2));
}
</code></pre><p></p>
<p>I can use this code, but firstly, it is more computationally demanding (because of norm function calling), and secondly, I am really interested in why my first solution doesn't work?</p>
http://answers.opencv.org/question/173943/two-line-segments-collinearity/?answer=174096#post-id-174096Hi,
In the first example, I guess, you are using not the correct formula to check the collinearity of two lines. There exists several ways do to this. If you won't use norm function have a look at:
http://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
hope it helps!Tue, 12 Sep 2017 08:06:36 -0500http://answers.opencv.org/question/173943/two-line-segments-collinearity/?answer=174096#post-id-174096