What is OpenCV?

The OpenCV library is a collection of functions for programming and manipulating images. It offers an array of utility classes for manipulating and processing images, and building applications that leverage images. This series covers techniques and code to perform image processing with OpenCV in python

Reading an Image

The first step in image processing is reading an image into a variable. The imread() fuction performs the reading of an image into a variable stored as a numpy object. Numpy objects have a number of fuctions that make image manipulation possible.

import cv2
import numpy as np
import matplotlib.pyplot as plt

The imread() function can take two arguments:

1. The path of the file: str
2. Color Scheme i.e. BGR (blue-gree-red), GRAY e.t.c

Note: By default, OpenCV reads an image in the BGR format. It is almost always the case that you may need to perform color conversion to RGB. More on this later

Reading sample Image

img = cv2.imread('cat.png')
# converting from default BGR to RGB colorscheme
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        
plt.imshow(img)
opencv_read_cat_img

Visualize Images in OpenCV

While in the above function, I have used matplotlib, OpenCV offers visualization tools as well. The code below will pop up a window to visualize the image read to the variable $img$

cv2.imshow('Cat', img)
cv2.waitKey()
cv2.destroyAllWindows()

Color Schemes

OpenCV provides a number of ways to read images into different color schemes. In the image above, we read the cat.png image without specifying any color scheme. In reality, OpenCV2 implements cv2.IMREAD_COLOR by default. This is often the BGR scheme. Below are a list of some of the options:

1. IMREAD_COLOR: This is the default read color scheme with a 3-channel BGR

2. IMREAD_GRAYSCALE: Read an image with a grayscale color scheme

3. IMREAD_ANYCOLOR: Read an image with either COLOR or GRAYSCALE depending on metadata that comes with the image

4. IMREAD_UNCHANGED: Read an image as is. This may include additional channels such as alpha channel

5. IMREAD_REDUCED_COLOR_2: Read an image with COLOR but with it's resolution reduced by Half

These are some of the read configurations. A few more exist and can be found in the OpenCV documentation. For demonstration, let's read the above image with a grayscale scheme.

gray_cat = cv2.imread('cat.png', cv2.IMREAD_GRAYSCALE)
plt.imshow(gray_cat, cmap='gray')
plt.axis('off')
opencv_read_cat_img_gray_scale

Image Shape

When images are read with OpenCV, they are stores as numpy objects which makes it possible to manipulate and process images. One utility function/attribute in numpy is the shape attribute which returns the number of pixels of the width and height of the image and the number of channels BGR.

img.shape , type(img)
((300, 300, 3), numpy.ndarray)