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

查看:158
本文介绍了Python/OpenCV-使用OpenCV中Hough Line的两种不同方法在网球场中检测线-获得不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用openCV和hough变换来检测网球场中的线条.我想找到水平线和垂直线,以便找到交点并最终检测到网球场的拐角.

I'm trying to detect lines in a tennis court using openCV and hough transform. I would like to find horizontal and vertical lines in order to find intersection and finally detect the corner of the tennis court.

这里是原始图像.

但是我有一些问题.

1)我尝试使用HoughLineP. 这里的代码:

1)I tried to use 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

结果如下: houghLineP的结果

2)我尝试使用HoughLines 这里的代码

2)I tried to use HoughLines Here the code

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)

结果如下: houghLine的结果

在第一种情况下,我只有很少的线条,我想延长它们的长度,但是我没有找到...我尝试使用fitLine,但是它只能用于找到轮廓(此图中的findContours方法很糟糕)

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)

在第二种情况下,它工作良好,但是我有很多条线,几乎相同,并且在右下角,我没有任何相交以检测拐角...

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:我做了一种计算水平线和垂直线之间的交点的方法.

ps : I did a method which calculate the intersection between the horizontal lines and the vertical lines.

非常感谢,

推荐答案

看起来行,HougLines输出包含所需的行.您只需要过滤掉异常值.

Looks line the HougLines output contains the lines you want. 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 :

  • 该字段的外部限制,以大矩形表示
  • 由两条线表示的两条走廊,这些两条线在侧面切割了矩形
  • 服务方块,再次由两条线分别代表,一条平行于服务线,另一平行于边线

例如,您可以尝试从必须过滤掉异常值的模型中获利.为此,使用了一些方法,例如 RANSAC .基本思想是获取随机点,计算模型并在数据中检查是否合适.经过几次迭代,最合适的选择就是您要寻找的模型.这是一个众所周知的方法(由Fischler于1986年首次发布),因此您可以找到许多有关它的文档.让我们来看一个简单的示例算法,该算法可能对您有用(可能需要改编):

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. 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. Let's take a simple example algorithm that could work for you (probably with adaptations):

  1. 在直线相交处取4个随机点.计算将这些点映射到场的顶视图的透视投影P.您可以为此使用OpenCV的getPerspectiveTransform.现在,您可以在顶视图中看到该领域的模型.

  1. Take 4 random points within the lines intersections. Compute the perspective projection P that maps those points to a top view of the field. You can use OpenCV's getPerspectiveTransform for this. You now have your model of the field, in the top view.

由于您拥有球场的模型(根据网球规则),因此您可以说出其他线的交点(服务线与走廊线,服务正方形..)应该位于顶视图.如果对这些点应用透视变换P ^ {-1}的逆,则将它们放在图像空间中.

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.

检查是否达成共识:在图像空间中寻找与模型最接近的线相交点.在这里,您应该有一个度量标准:距离小于x像素或SSD的线的交点数.您将使用此指标为不同的模型评分.

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. You will use this metric to rate different models.

为模型评分:如果您定义的指标小于以前找到的最佳指标,那么这就是您当前的最佳模型

Rate your model : if the metric that you defined is less than the best previously found, this is now your current best model

迭代.您执行的迭代次数与最终选择一个好的模型的可能性直接相关.在Fischler和Bolls的开创性工作中寻找ow来设置迭代次数.

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.

在这里,在迭代的最后,您将找到最适合您的数据的模型,即,描述网球场的内在值和不适用的异常值.请注意,此方法对大量异常值(大于50%)具有鲁棒性,但是获得良好结果的机会只是统计上的(尽管如此,您可以通过调整迭代次数来设置期望的结果质量).

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).

希望这会有所帮助,

这篇关于Python/OpenCV-使用OpenCV中Hough Line的两种不同方法在网球场中检测线-获得不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆