Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在不知道簇数的情况下,基于起点和终点对线进行聚类_Python_Line_Hough Transform - Fatal编程技术网

Python 在不知道簇数的情况下,基于起点和终点对线进行聚类

Python 在不知道簇数的情况下,基于起点和终点对线进行聚类,python,line,hough-transform,Python,Line,Hough Transform,我使用Hough变换来检测足球场中的线条。下面是检测到的行的示例(61行): 所有行都是cv2.HoughLinesP-函数的输出,并以以下格式显示为numpy数组: [x1, y1, x2, y2] 有3个不同的线组,我想分类球门线,球门区线和罚球区线。图中可见的第四行是由广告牌引起的,我想忽略这一行。 我正在努力选择集群的数量,k。这是因为当球从中线向球门区移动时,我们首先只看到一条线,即罚球区\u线。当球进一步向左移动时,相机可能会跟随,我们会看到更多的线。我使用以下公式计算每条线的

我使用Hough变换来检测足球场中的线条。下面是检测到的行的示例(61行):

所有行都是
cv2.HoughLinesP
-函数的输出,并以以下格式显示为
numpy数组

 [x1, y1, x2, y2]
有3个不同的线组,我想分类<代码>球门线,
球门区线
罚球区线
。图中可见的第四行是由广告牌引起的,我想忽略这一行。 我正在努力选择集群的数量,k。这是因为当球从中线向球门区移动时,我们首先只看到一条线,即
罚球区\u线
。当球进一步向左移动时,相机可能会跟随,我们会看到更多的线。我使用以下公式计算每条线的坡度和截距:

def gradient_intercept(x1, y1, x2, y2):
   dx = x2 - x1
   dy = y2 - y1
   radius = math.atan2(-dy, dx)
   radius %= 2 * math.pi
   gradient = -math.degrees(radius)
   if gradient <= -180:
     gradient = gradient + 180
   gradient = gradient + 90
   intercept = y1 - gradient * x1

   return gradient, intercept
该函数产生以下输出:

[[0.        6.10702413 5.12577724 ... 1.11858265 0.02889456 2.02399679]
[6.10702413 0.         0.98124689 ... 7.22560678 6.07812957 4.08302733]
[5.12577724 0.98124689 0.         ... 6.24435989 5.09688267 3.10178044]
...
[1.11858265 7.22560678 6.24435989 ... 0.         1.14747721 3.14257944]
[0.02889456 6.07812957 5.09688267 ... 1.14747721 0.         1.99510223]
[2.02399679 4.08302733 3.10178044 ... 3.14257944 1.99510223 0.        ]]
接下来,我需要根据截距将这些线聚集在一起。 我假设(在4个可见簇的情况下)

通过搜索,我相信这是适合我的情况

from sklearn.cluster import DBSCAN
db = DBSCAN(eps=0.2,min_samples=2)  # minimum of two lines in order to be considered a cluster
db.fit_predict(distance_matrix)
labels = db.labels_
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
检查
n\u集群
我有时会看到5或6的值问题:什么是eps?它是什么比例的?在我的案例中,什么是合适的值? 第二个问题我的方法(截距->距离矩阵)正确吗


提前感谢

intercept=y1-梯度*x1
似乎有点可疑。您最有可能需要的是
intercept=y1-(dy/dx)*x1
。不要麻烦使用集群。只需检查两个斜率截距对是否在彼此的公差范围内。如果是的话,它们是同一条直线上的线段。也就是说,我正在写一篇关于一维聚类算法的论文,该算法应该适用于你的情况。当我提供时,我会记住你的。
  intercept_billboards > intercept_goal_line > intercept_goal_area_line > intercept_penalty_area_line
from sklearn.cluster import DBSCAN
db = DBSCAN(eps=0.2,min_samples=2)  # minimum of two lines in order to be considered a cluster
db.fit_predict(distance_matrix)
labels = db.labels_
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)