我需要比较2张图片才能在其中找到相似的线条。在两张图片中,我都使用LSD(线段检测器)方法,然后找到线,并且知道每条线的起点和终点的坐标。
我的问题是:OpenCV中是否有任何函数可以查找每条线的斜率和长度,以便我可以轻松比较它们?
我的环境是:OpenCV 3.1,C ++和Visual Studio 2015
好吧,这是一个数学问题。
假设您有两点:和。我们叫“开始”和该线段的“结束”,当你打电话给你的之分。p1(x1,y1)
p2(x2,y2)
p1
p2
斜率=(y 2 -y 1)/(x 2 -x 1) 长度=范数(p 2 -p 1)
样例代码:
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?)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句