简体   繁体   English

Python / OpenCV - 在OpenCV中使用两种不同的Hough Line方法检测网球场中的线 - 得到不同的结果

[英]Python/OpenCV - Detect lines in a tennis court using two differents methods of Hough Line in OpenCV - Get differents results

I'm trying to detect lines in a tennis court using openCV and hough transform. 我正试图用openCV和hough变换检测网球场的线条。 I would like to find horizontal and vertical lines in order to find intersection and finally detect the corner of the tennis court. 我想找到水平和垂直线,以找到交叉点,最后检测网球场的角落。

Here the original image. 这里是原始图片。 在此输入图像描述

But i have some problems. 但我有一些问题。

1)I tried to use HoughLineP . 1)我试图使用HoughLineP。 Here the code : 这里的代码:

gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

edges = cv2.Canny(gray,100,200,apertureSize = 3)
lines = cv2.HoughLinesP(edges, 1, np.pi/2, 6, None, 50, 10);
for line in lines[0]:
    pt1 = (line[0],line[1])
    pt2 = (line[2],line[3])
    cv2.line(img, pt1, pt2, (0,0,255), 2)
cv2.imshow('dst',img)

return res

Here the result : Result of houghLineP 结果如下: houghLineP的结果

2)I tried to use HoughLines Here the code 2)我试图在这里使用HoughLines代码

gray=cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)

edges = cv2.Canny(gray,100,200,apertureSize = 3)


#Lignes
lines = cv2.HoughLines(edges,1,np.pi/70,110)
for rho,theta in lines[0]:
    if (np.pi/70 <= theta <= np.pi/7) or (2.056 < theta < 4.970) or (1.570 <= theta <= 1.600): #(2,6 <=theta <= 26) or (theta >118 and theta <= 285)

        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a*rho
        y0 = b*rho
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))

        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))

        cv2.line(res,(x1,y1),(x2,y2),(0,0,255),1)

Here the result : Result of houghLine 结果如下: houghLine的结果

In the first case, i have only little lines, and i thought about prolong them but i didn't find...I tried using fitLine but it only works with find contours (findContours method is awful in this picture) 在第一种情况下,我只有很少的线条,我想延长它们,但我没有找到...我尝试使用fitLine但它只适用于查找轮廓(findContours方法在这张图片中很糟糕)

In the second case, it works well but i have lots of lines, almost the same and on the bottom right, i don't have any intersection in order to detect the corner... 在第二种情况下,它运作良好,但我有很多线,几乎相同,在右下方,我没有任何交叉点,以检测角落...

Maybe i'm on the wrong way... 也许我走错了路......

Do you have some ideas or something that could be works ? 你有一些想法或可能有用的东西吗? At the end, i would like only interest points concerning only the tennis court. 最后,我想只关注网球场的兴趣点。

ps : I did a method which calculate the intersection between the horizontal lines and the vertical lines. ps:我做了一个计算水平线和垂直线之间交点的方法。 在此输入图像描述

Many thanks, 非常感谢,

Looks line the HougLines output contains the lines you want. 看起来行HougLines输出包含你想要的行。 You only need to filter out the outliers. 您只需要过滤掉异常值。

For this, you could use a model of your tennis field. 为此,您可以使用您的网球场模型。 What you know about it is that you have : 你知道的是你有:

  • The outer limits of the field, represented by a big rectangle 该字段的外部限制,由一个大矩形表示
  • The two corridors, represented by two lines that cut the rectangle on the sides 两条走廊,由两条切割两侧矩形的线条代表
  • The service squares, represented again each by two lines, one parallel to the service line and one parallel to the sides 服务方块再次由两条线表示,一条线平行于服务线,一条平行于侧面

You could try for instance to take profit from the model you have to filter out the outliers. 例如,您可以尝试从模型中获利,以过滤掉异常值。 Some approaches, like RANSAC , are made for this. 一些方法,如RANSAC ,就是为此而制定的。 The basic idea is to take random points, compute the model and check within the data if that fits. 基本思想是采用随机点,计算模型并检查数据是否合适。 After some iterations, the best fit is most probably the model you look for. 经过一些迭代后,最合适的是你寻找的模型。 This is a quite known approach (first published by Fischler in 1986) so you can find lots of documentation on it. 这是一种众所周知的方法(由Fischler于1986年首次发布),因此您可以找到很多文档。 Let's take a simple example algorithm that could work for you (probably with adaptations): 让我们采用一个可以为您工作的简单示例算法(可能需要进行调整):

  1. Take 4 random points within the lines intersections. 在线交叉点内取4个随机点。 Compute the perspective projection P that maps those points to a top view of the field. 计算将这些点映射到该字段的顶视图的透视投影P. You can use OpenCV's getPerspectiveTransform for this. 您可以使用OpenCV的getPerspectiveTransform。 You now have your model of the field, in the top view. 您现在可以在顶视图中找到该字段的模型。

  2. Since you have a model of the field (based on the rules of tennis), you are able to say where the other intersections of lines (service lines with corridor lines, service squares ..) should be on the top view. 由于您拥有该领域的模型(基于网球规则),您可以说出线的其他交叉点(带有走廊线,服务方格......的服务线)应位于顶视图的哪个位置。 If you apply the inverse of the perspective transform P^{-1} for these points, you have them in the image space. 如果对这些点应用透视变换P ^ { - 1}的反转,则可以在图像空间中使用它们。

  3. Check for consensus : look for closest intersections of lines in the image space to the ones of your model. 检查是否达成共识:查找图像空间中线条与模型的最近交点。 Here you should have a metric : number of intersections of lines at a distance less than x pixels, or SSD. 在这里,您应该有一个指标:距离小于x像素的线的交点数,或SSD。 You will use this metric to rate different models. 您将使用此指标对不同的模型进行评级。

  4. Rate your model : if the metric that you defined is less than the best previously found, this is now your current best model 为您的模型评分:如果您定义的指标小于之前找到的最佳指标,那么现在这是您当前的最佳模型

  5. Iterate. 重复。 The number of iterations that you do directly relates to the probability of selecting a good model in the end. 您所做的迭代次数与最终选择好模型的概率直接相关。 Look in seminal work of Fischler and Bolls for ow to set the number of iterations. 查看Fischler和Bolls的开创性工作,以设置迭代次数。

Here it is, in the end of the iterations you will have found the model that best fits your data, ie inliers that describe a tennis field and outliers that don't. 在迭代结束时,您将找到最适合您数据的模型,即描述网球场的内点和不适合的异常值。 Note that this method is robust to a large number of outliers (more than 50 percent), but the chance of having a good result is only statistical (you can set the expected quality of your result by tuning the number of iterations, though). 请注意,此方法对于大量异常值(超过50%)是稳健的,但是获得良好结果的可能性仅是统计的(您可以通过调整迭代次数来设置结果的预期质量)。

Hope this helps, 希望这可以帮助,

Ben

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM