# How to Follow the Action on a Basketball Court

**URL:** https://forum.opencv.org/t/how-to-follow-the-action-on-a-basketball-court/3180
**Category:** Uncategorized
**Tags:** imgproc, practical, objdetect, optflow, tracking
**Created:** [May 6, 2021, 5:31am UTC](https://forum.opencv.org/t/how-to-follow-the-action-on-a-basketball-court/3180 "2021-05-06T05:31:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tps](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@tps](https://forum.opencv.org/u/tps)
#### Post date: [May 6, 2021, 5:31am UTC](https://forum.opencv.org/t/how-to-follow-the-action-on-a-basketball-court/3180/1 "2021-05-06T05:31:47Z")

</div>

Hi All

Is it possible?

I want to track players ( and the basketball if possible ) on a basketball court and decide where the action is.

Tracking input video comes from a stationary wide angel camera that covers the entire court.

Output from the tracking process should be pan instructions to a PTZ camera that follows the game. Initially only panning. No tilt or zoom.

All ideas are appreciated!

Thanks

Thomas S

---

<div class="post-metadata">

### Author: ![PommePomme](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/pommepomme/32/2005_2.png) [@PommePomme](https://forum.opencv.org/u/PommePomme)
#### Post date: [May 6, 2021, 7:55am UTC](https://forum.opencv.org/t/how-to-follow-the-action-on-a-basketball-court/3180/3 "2021-05-06T07:55:39Z")

</div>

Hi,

1. Try to detect the basketball, you will get a bounding box and will be able to calculate the center of this box. Then, calculate the distance between the center of your image and the center of the bounding box. If it is above a defined range, move the camera to the position of the ball but:

2. Try to detect all the players and then calculate if the percentage of 10 is above a defined number. Like if 7 or 8 players are at a position, it probably means that the game takes place there. Then, with all the players boxes you should be able to calculate a global center of all there positions, and move the camera to this point but:

I also know that some datasets provides action models, like “hitting a ball”, “playing something” so it maybe an alternative of the player detection.

Here some links that should be useful:

> **[Ball Tracking with OpenCV - PyImageSearch](https://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/)**
>
> Discover how use OpenCV for ball tracking. You'll learn how to perform basic object detection and tracking in video streams using OpenCV and Python.

> **[Simple object tracking with OpenCV - PyImageSearch](https://www.pyimagesearch.com/2018/07/23/simple-object-tracking-with-opencv/)**
>
> You will learn how to perform simple object tracking using OpenCV, Python, and the centroid tracking algorithm used to track objects in real-time.

[http://activity-net.org/explore.html](http://activity-net.org/explore.html)  
[http://hacs.csail.mit.edu/index.html#explore](http://hacs.csail.mit.edu/index.html#explore)  
[https://storage.googleapis.com/openimages/web/visualizer/index.html?set=train&type=relationships&c=throw](https://storage.googleapis.com/openimages/web/visualizer/index.html?set=train&type=relationships&c=throw)  
[https://www.kaggle.com/search?q=basketball](https://www.kaggle.com/search?q=basketball)

---

<div class="post-metadata">

### Author: ![PommePomme](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/pommepomme/32/2005_2.png) [@PommePomme](https://forum.opencv.org/u/PommePomme)
#### Post date: [May 10, 2021, 8:48am UTC](https://forum.opencv.org/t/how-to-follow-the-action-on-a-basketball-court/3180/4 "2021-05-10T08:48:22Z")

</div>

I recently found this video that actually show what I explained

[![](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/a5183b63f0d4ae17c470017c5eebc69aa019f627.jpeg "DEMO V1 04 29") ](https://www.youtube.com/watch?v=nT_Ax33S3no)

---

<div class="post-metadata">

### Author: ![tps](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@tps](https://forum.opencv.org/u/tps)
#### Post date: [May 11, 2021, 2:24pm UTC](https://forum.opencv.org/t/how-to-follow-the-action-on-a-basketball-court/3180/5 "2021-05-11T14:24:09Z")

</div>

Hi PommePomme,

Thanks a lot.

I did try Adrian Rosebrocks follow the ball script. It turns out the basketball on my video is way to small!

Do you know more about the code behind the YouTube video above?

Right now I try to use

calcOpticalFlowFarneback(prev, next, None, 0.5, 3, 15, 3, 5, 1.2, 0) and mag, ang = cv2.cartToPolar(flow[…,0], flow[…,1], mag, ang, True)

to get displacement vectors from one frame to the next.

Currently, I am trying to understand why I get displacement vectors from one frame to the next when nothing is changing from one frame to the next.

Thanks

Thomas S

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [May 11, 2021, 4:42pm UTC](https://forum.opencv.org/t/how-to-follow-the-action-on-a-basketball-court/3180/6 "2021-05-11T16:42:47Z")

</div>

> [@tps](#):
>
> Currently, I am trying to understand why I get displacement vectors from one frame to the next when nothing is changing from one frame to the next.

please be precise in your description.

you **always** get vectors. one for every pixel, unless the algorithm is one of those that _can_ mark a pixel’s vector as invalid for some reason.

when nothing moves, these vectors should simply be 0 or very close.

video usually contains noise. anything derived from noisy data (calculating optical flow) will also contain noise.

---

<div class="post-metadata">

### Author: ![tps](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@tps](https://forum.opencv.org/u/tps)
#### Post date: [May 12, 2021, 10:38am UTC](https://forum.opencv.org/t/how-to-follow-the-action-on-a-basketball-court/3180/7 "2021-05-12T10:38:52Z")

</div>

Hi crackwitz

Thanks!

I get the vectors from a call to

flow = cv2.calcOpticalFlowFarneback(prev, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)

flow is now a 3D array.

Now I am confused!

Does flow represent pixel displacement from frame ‘prev’ to frame ‘next’ where x displacement is in flow[:,:,0] and y displacement is in flow[:,:,1]

Or

do I have to use flow in this call:

mag, ang = cv2.cartToPolar(flow[…,0], flow[…,1], mag, ang, True)

and now I have pixel displacement distance in mag and displacement angel in ang?

Thanks

Thomas S

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [May 12, 2021, 2:20pm UTC](https://forum.opencv.org/t/how-to-follow-the-action-on-a-basketball-court/3180/8 "2021-05-12T14:20:13Z")

</div>

both. kinda. as you can imagine.

I am puzzled as to why you posed this as an either-or question.

it’s not a choice between two options.

the first is a fact.

the second is something you _can_ do, if you _need_ it.

the cartesian and polar **representations** of a vector represent the **same vector**. you decide what representation you need, and then you convert or you don’t.
