如何判断质心点是否接触直线?

Adi*_*Raj 2 opencv computer-vision intrusion-detection python-3.x deep-learning

我正在研究一种基于越线检测的入侵检测算法。我使用方程 y = mx+c 开发了一种基本算法,但当人靠近线时,它显示出一些错误的检测。我需要一些建议来使其成为完美的线接触算法。

在此输入图像描述

在此输入图像描述

Gus*_*eto 5

如果您的直线有起点和终点[x1, y1][x2, y2],则直线方程为:

y - y1 = m * (x - x1), 在哪里m = (y2 - y1)/(x2-x1)

然后,您可以检查一个点是否属于直线,用 或 替换其中x一个y,并检查另一个点是否与直线方程匹配。

在皮顿中:

# the two points that define the line
p1 = [1, 6]
p2 = [3, 2]

# extract x's and y's, just for an easy code reading
x1, y1 = p1
x2, y2 = p2

m = (y2-y1)/(x2-x1)

# your centroid
centroid = [2,4]
x3, y3 = centroid

# check if centroid belongs to the line
if (m * (x3-x1) + y1) == y3:
    print("Centroid belongs to line")

Run Code Online (Sandbox Code Playgroud)

但很可能...

...计算红点和线之间的距离(从点到线的距离),然后检查它是否足够近(即距离小于某个值),你会得到更好的结果。

在Python中:

# points that define the line
p1 = [1, 6]
p2 = [3, 2]
x1, y1 = p1
x2, y2 = p2

centroid = [2,4]
x3, y3 = centroid

# distance from centroid to line
import math # to calculate square root
dist = abs((y2-y1)*x3 - (x2-x1)*y3 + x2*y1 - y2*x1)/math.sqrt((y2-y1)**2 + (x2-x1)**2)

if dist < some_value:
    print("Near enough")
Run Code Online (Sandbox Code Playgroud)