This part of the tutorial is going to be very simple. We’ll just be drawing a simple interface in the video window, to display some information.
Let’s start by using this code from the last chapter of this tutorial with some minor modifications:
First we use the cap.get() methods to calculate the middle coordinates in our video (width/2, height/2).
Then, before we call imshow() we use cv2.putText(). As the name suggests, this method writes text on the video frame. Usage is: cv.PutText(img, text, org, font, color), where org is the origin (bottom-left corner) of the text to write.
If you run the code, you’ll see this:
We can also draw lines, circles, etc. into the video frame, OpenCV has many methods to draw geometric shapes.
Let’s draw some lines:
This time, we’re working outside the try block and using two video windows, one to display the raw video and another one to show the modified one with lines.
In order for polylines to work, it needs to receive a numpy array with the coordinate pairs (x and y) for each point in the line, in our case, beginning and end. If you want to specify the points like I did, you also need to call reshape(-1,1,2) for it to work with polylines().
If you run this code you´ll see:
Here´s OpenCV’s documentation for drawing functions.
This is the end of this chapter. In the next one we’ll use background substraction to actually start with the counter.