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

有些话、适合烂在心里 提交于 2019-12-03 16:41:06

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 :

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

  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.

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

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

Hope this helps,

Ben

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!