Intro to OpenCV- Video Input, Color And Video Analysis With Python

Nishank Sharma
the ML blog
Published in
6 min readJul 22, 2017

Hello guys,

It’s Nishank here, welcome to your first OpenCV wit Python tutorial. Starting here, we will build upon many concepts of OpenCV from beginner to advanced level and we will end it with a project!

Now OpenCV is the one of them most searched tutorials in artificial intelligence but unfortunately, there are very few tutorials about it, and many of tutorials that are there are very complex to understand. So my goal here is to make learning OpenCV, fun and understandable to everyone. We will go very deep but at the same time will maintain the simplicity of the series. So let’s start!

As an intro to OpenCV, we will start by

  • importing images
  • importing live video from web cam
  • converting our video/image to grayscale
  • drawing various shapes on our output
  • writing text on our output

Sounds fun right? Let’s start by installing the required libraries. Before we start with our libraries, I would like you to install Enthought’s Canopy which is comprehensive Python analysis environment. It has an inbuilt package manager which makes it easier for us to install libraries. After installing Canopy, open it and go to Package Manager tool and search for opencv in the available tools.

Package Manager In Canopy

If you don’t want to go through the work of installing Canopy, you can install OpenCV using terminal also (But I would recommend using Canopy as it will be helpful for installing libraries in future also). Just type

Next we will try to get an image and show it

Importing Images

Reading Image

We imported cv2 which is the OpenCV library we will be using in this entire series. Then using cv2.imread() function, we read our image (note that the image should be same folder as your code, else give the path). And the we used cv2.imshow() function to show the image stored in the variable imgon screen and gave the window a name image. In my case, I imported an image of a watch.

Image Output

Next, we used cv2.waitKey(0) to wait for any key to be pressed and then used cv2.destroyAllWindows() to close all windows. Simple enough?

Now just change the code in cv2.imread() function, a little bit.

Image In Grayscale

The option cv2.IMREAD_GRAYSCALE will import our image and will convert it to grayscale. It would look something like this.

Grayscale Image

Working Wth Live Video

Now that we are done with image, let’s start some video processing. I hope you are familiar with the concept of loop in python. If not see it here.

We start by declaring and defining the variable we will be using for capturing the video.

Declaring Stream

We used cv2.VideoCapture(0) here to define our variable stream for video capture. In this code 0 is used to specify the primary camera, you can 0,1,2 depending on how many cameras are you using.

Don’t be overwhelmed. We will take it bit by bit. First, we used a while loop (if you don’t about loops in python, refer to the link above). We did so in order to take frames of our video continuously, since a video is made up of multiple frames or images, the while loop is only to take those images, do modifications and show them continuously, so that what we see is a video.

After starting the while loop, we took our stream variable, to take input using the function stream.read() and stored the input in a variable frame. The variable ret is for a different function that we will discuss later, so for now don’t worry about it.

Next we used cv2.imshow() function, to show our frame . Remember that all of this is in a while loop, so it we will see it as a video. The next step is similar to what we have done in the image but with a minor difference.

Here, we have change it to cv2.waitKey(1)to indicate that the window will be closed only if 1 key, i.e 'q'will be pressed. We have used 0xFF to take 8 bit value of q, in a nutshell, “& 0xff” effectively masks the variable so it leaves only the value in the last 8 bits, and ignores all the rest of the bits. And ord() is used to take numerical value of ‘q’ . So, in short, all windows will be closed when you press q. We get something like this, after execution-

Ignore My Face

Next, we will try to convert it to grayscale. We just need to add a single line to our code, and that would do.

Grayscale Video

We just added cv2.cvtColor() function and used frame and cv2.COLOR_RGB2GRAY as an input. I think this part of code needs no further explanation. After running the code, we would see the following output-

Again, ignore my face

Shapes And Text

Now that we have our video ready, let’s draw some shapes on it. We will start with a line. To draw a line, write the following code inside the loop, after stream.read() and before cv2.imshow() .

Drawing A Line

Using cv2.line() function, we can draw a line. The first input frame is the variable or set of images on which we want to draw a line. The next 2 inputs are the co-ordinates for the line and the third input (185,128,41) is the specified color for the line (note that OpenCV uses BGR format). Now the last input is the thickness of the line. You can change these according to your needs. After running the code-

Just Ignore It

Now let’s write some text on our video.

Putting Text

We declared what font we want to use by cv2.FONT_HERSHEY_SIMPLEX , we will use Hershey Simplex Font. Next, we used cv2.putText() function to put our text into the frame . It would look something like this-

You Know The Drill

Next we will draw a rectangle, which has the same syntax as the of line but with a few changes.

Drawing Rectangle

Here, cv2.rectangle() function takes 2 different inputs from cv2.line() function. The second and third inputs are coordinates of top-left corner and bottom-right corner respectively, rest all parameters are the same. It will yield us the following video output-

Yeah!

I think we are done with the basics of OpenCV. All the code used in this post can be found by clicking the banner below. I will be posting the second post to the series soon. Stay tuned and subscribe to our newsletter for an awesome experience and never missing an update.

--

--

Nishank Sharma
the ML blog

Hello, I’m Nishank. I design beautiful, usable and enjoyable interfaces.