python-3.x - 如何识别质心点是否接触到一条线?

标签 python-3.x opencv deep-learning computer-vision intrusion-detection

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

enter image description here

enter image description here

最佳答案

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

y - y1 = m * (x - x1),其中 m = (y2 - y1)/(x2-x1)

然后您可以检查一个点是否属于直线,替换 xy,并检查另一个是否与直线方程匹配。

在 Pyhton 中:

# 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")

但可能...

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

在 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")

关于python-3.x - 如何识别质心点是否接触到一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60718288/

相关文章:

Python - HTTP 多部分/表单数据 POST 请求

python - 如何使用Python向csv中的字符串添加单引号

python - 从日期时间对象中提取日期和月份

visual-c++ - 将 Triclops 中的图像转换为 opencv RGB 图像垫。

opencv - 错误13错误C1083:无法打开包含文件: 'cudnn.h':没有这样的文件或目录…\caffe\util\cudnn.hpp 5 1 convert_imageset

python-3.x - 查找包含图案的 socks 的轮廓

python - 如何从图像中提取椭圆轮廓并保存到不同的变量中?

c++ - 使用 OpenCV 进行图像捕获 - 选择超时错误

python - 用于自定义图像尺寸的 MobileNets

tensorflow - 将输入馈送到中间层,然后在 keras 中进行反向传播