Getting Started with OpenCV

How to Install OpenCV?

This blog shows how you can get started with OpenCV and Python in Windows. In a step by step guide, you will see how to install the required packages and get stared with OpenCV. Welcome to the OpenCV installatin guide. Before we go any further, let’s prepare our working environment and set ourselves up for the exercises we will be doing as we move along. Here we will start by downloading and installing the required software libraries and packages.

Reading, writing and displaying Images using OpenCV

Reading, displaying and writing images in OpenCV is very simple. Let us start by reading and displaying an image. To read an image in OpenCV we can use imread() function. This function will return a numpy array. This array can be stored in a variable which can then be used to display it. The following lines of code shows a simple way to read an image and display it. To display an image, we can use OpenCV's imshow() function.

How to Capture Videos using OpenCV

This section explains how to read and display videos using OpenCV. OpenCV provides VideoCapture object for reading and writing videos. We can also use VideoCapture object to capture images from camera as well. The constructor can be used to read a video by providing a string path of the video to read. We can also provide the integer value indicating the camera index to capture the camera. Let us look at an example to read a video file and display the frames one by one. Normally, the video frames are display at 25 FPS, which means 25 frames are displayed in one second. In this example, we will need to handle it manually by using waitKey() method of OpenCV.

Color Conversions in OpenCV

OpenCV allows us to work with various colorspaces. Colorspaces have their importance for various applications. We can choose a colorspace according to our application requirements. Fortunately, OpenCV provides more than 150 color-space conversion methods. In the scope of this book, we will cover only few of them. However, once you learn about the color conversion for some of them, you can definitely work for rest of them.

How to draw basic shapes on images using OpenCV?

Drawing shapes on the images is very easy with OpenCV. In this section, we will learn how to draw lines, circles, rectangles, ellipses, and text on the image. Drawing Line To draw a line, we will use OpenCV's line() function. The formal parameters of the line() function implemented in OpenCV. Drawing Circle We can draw a circle in OpenCV using the circle() function. To draw a circle, we need its center coordinates and radius. Let us see an example. The following code shows how to draw a red circle on a black image.

Accessing and Manipulating Pixel values in Images using OpenCV

Pixels are the building blocks of images. These intensity values are stored in a 2-D array, and these intensities are shown on the monitor for visualization and understanding by a human. These individual intensity values are called pixels in image processing terminology. From a programmer's perspective, the image is just a 2-D array. These values in the array can be accessed and manipulated easily. In OpenCV-Python, the 2-D array is basically a Numpy array. So, working with images in OpenCV-Python is more like working with Numpy arrays. We can access and manipulate the pixel values in two ways. Let us start with the easy but slower way. The following lines of code read an image and access the pixel value at location (0,0).

Working with the Region of Interest (ROI) in Images

Sometimes we are interested in some parts of the image rather than working in the whole image region. This small region is what we call it Region of interest or, in short, ROI. We learned about accessing pixel values from a particular location in the previous section. However, here we want to access more than one pixel at a time. Naturally, we should mention the start of the location from where we want to start reading pixels and the end location. In other words, we select a rectangular region to access or modify the pixel values. For instance, we want to read the pixel values shown in the rectangular region below: