Question

I'm working on a program that require me to find the angle between two lines. I have found all of the points coordinates of the lines, referenced into the 0,0 points, but i dont understand how to implement the dot product in Opencv to find the angle.

I use visual C++ using opencv library. I also found out that i can use cv::Mat::dot function to get dot product, but i can't find any good example. I can not understand the explanation in this link.

Can anyone give me a good example? so i can understand how to use that function to find dot product of two lines. Thank you.

Was it helpful?

Solution

If you have two vectors representing your lines, and call them a and b (of type cv::Mat) then the dot product is calculated as

double p = a.dot(b);

Edit Example code, by comments from author.

float a[2] = {1.0, 2.0};
float b[2] = {3.0, 4.0};

cv::Mat AA(1,2,CV_32FC1,a);
cv::Mat BB(1,2,CV_32FC1,b);

cout << AA << endl;
cout << BB << endl;
cout << AA.dot(BB) << " should be equal to 11" << endl;

OTHER TIPS

Well, the dot product of two vectors A and B is defined as

(length of A) * (length of B) * cos(angle)

where angle represents the angle between the two vectors. So in order to find the angle between the two, first you have to find the dot product, then divide it by both the length of A and the length of B, then take the inverse cosine.

What this looks like in your case is something along these lines, assuming a and b are declared appropriately as cv::Mats:

double dotprod = a.dot(b);
double angle = arccos(dotprod / (a.size().height * b.size().height))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top