OpenCV中两点之间直线的斜率和长度

Slope and Length of a line between 2 points in OpenCV

我需要比较 2 张图片以找到它们之间的相似线条。在两张图片中,我都使用 LSD(线段检测器)方法,然后我找到线并且我知道每条线的起点和终点的坐标。

我的问题是:OpenCV中有没有函数可以找到每条线的斜率和长度,以便我可以轻松比较它们?

我的环境是:OpenCV 3.1、C++ 和 Visual Studio 2015

嗯,这是一道数学题。

假设你有两个点:p<sub>1</sub>(x<sub>1</sub>,y<sub>1</sub>)p<sub>2</sub>(x<sub>2</sub>,y<sub>2</sub>)。我们将 p<sub>1</sub> 称为 "start" 和 p<sub>2</sub>线段的"end",正如你所称的点。

slope  = (y2 - y1) / (x2 - x1)
length = norm(p2 - p1)

示例代码:

cv::Point p1 = cv::Point(5,0); // "start"
cv::Point p2 = cv::Point(10,0); // "end"

// we know this is a horizontal line, then it should have
// slope = 0 and length = 5. Let's see...

// take care with division by zero caused by vertical lines
double slope = (p2.y - p1.y) / (double)(p2.x - p1.x);
// (0 - 0) / (10 - 5) -> 0/5 -> slope = 0 (that's correct, right?)

double length = cv::norm(p2 - p1);
// p_2 - p_1 = (5, 0)
// norm((0,5)) = sqrt(5^2 + 0^2) = sqrt(25) -> length = 5 (that's correct, right?)