OpenCV: Introduction and Simple Tricks in Python

By | August 16, 2020

OpenCV-AI-toolkit was first introduced nearly 20 years ago(in 1999) by Intel Research and it is getting richer and better every year since then. OpenCV was primarily written in C++ language but has bindings for Python, Java, and MATLAB that makes it easy to integrate into different ML/AI projects. You will find almost every Computer Vision(Computer Graphics based) project running on OpenCV-toolkit.

If your project involves ‘images or videos’ kind of data then OpenCV-kit is going to be handy for you. It brings a large set of functionalities to play with the image data. This toolkit offers almost all the operational actions you could think of applying to a given image. It’s widely used in computer vision(Computer Graphics) projects.

In this article, we are going to learn how to use OpenCV in order to perform simple and quick transformations/operations on your image dataset. Let’s get started:-

Agenda

  1. Why OpenCV?
  2. OpenCV installation
  3. Reading and Writing image data
  4. Simple OpenCV Operations
  5. Application Areas of OpenCV
  6. Conclusion
  7. References

1. Why OpenCV?

Well, there are multiple reasons why everyone is using OpenCV as the heart of their Computer Vision-based projects. It is fairly easy to code up and known for its fast speed. Integration with different computer vision projects is also simple. OpenCV is quite rich in functionalities and is growing very rapidly. Listed below are some reasons why everyone is opting for this toolkit in their vision based projects-

  • Fast Speed
  • Easy to code
  • Easy to integrate
  • Rich of functionalities

2. OpenCV installation

OpenCV-toolkit can easily be installed in your python environment. You can use the following pip commands to install it yourself-

If you need only main modules

i) For Standard Desktop environments (Windows, Linux, MacOS)

pip install opencv-python

ii) For Server (Headless) environments

pip install opencv-python-headless

If you need main + contrib modules (extra modules listing from Documentation)

i) For standard Desktop environments

pip install opencv-contrib-python

ii) For Server (Headless) environments

pip install opencv-contrib-python-headless

If you still face any difficulty in installing the library, kindly refer to the documentation here.


3. Reading and Writing Image Data

OpenCV library can be imported in python as CV2. Any given image can be read using cv2.imread() method by simply passing the image-path as argument. This method reads the image channels in BGR(blue, green and red) order that is opposite to how most of the common image formats store the image channels. Thus it is important to convert the read image into the RGB channel format using cv2.cvtColor() method. If you want a gray-scale image just pass the cv2.COLOR_RGB2GRAY argument into the cv2.cvtColor() method. Find the example code below-

import cv2

def read_image(image_path, gray=False):
    image = cv2.imread(image_path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    if gray:
        image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    return image

image_path = "opencv-logo.png"
image = read_image(image_path=image_path)
print (image.shape)
Out[1]: (794, 600, 3)

Image can be displayed using the following matplotlib method-

import matplotlib.pyplot as plt
%matplotlib inline

def show_image(image, figsize = (5, 5), gray=False):
    plt.figure(figsize=figsize)
    if gray:
        plt.imshow(image, cmap="gray")
    else:
        plt.imshow(image)
    plt.show()

image = read_image(image_path=image_path)
print (image.shape)
show_image(image)

Output looks like this:-

Reading image with OpenCV
Sample Output

Image can be easily written using cv2.imwrite() method. You can give the image name(the name you want to write your image with) as first argument and image object (to be saved) as the second argument.

cv2.imwrite("image_writing_example.png", image)

This method returns 'True' if image is successfully written.


4. Simple OpenCV operations

You can perform quite a few operations on images using cv2. Let’s look into few simple operations:-

A. Image Rotation

cv2.rotate()method can be used to rotate the image into the given direction.

def rotate_image(image, how="right"):
    value = 0
    if how=="left":
        value=2
    if how=="upside-down":
        value=1
    rotated = cv2.rotate(image, value)
    return rotated

image = read_image(image_path=image_path)
rotated_image = rotate_image(image, how="left")
show_image(rotated_image)

here is the output-

Rotated images
Sample outputs based on the given argument

B. Image Resizing

image can be resized to a different shape using cv2.resize() method-

image = read_image(image_path=image_path)
print ("old shape: ",image.shape)
image = cv2.resize(image, (50, 50))
print ("new shape: ",image.shape)
show_image(image)

here is the output:

Resized image
Resized output image

C. Inverting Colors

As each pixel’s intensity can vary from 0 to 255, image colours can be inverted if you subtract each pixel value from 255. Let’s try to invert this logo image-

image = read_image(image_path=image_path)
image = 255-image
show_image(image)

Here is the inverted image-

Inverted image
Inverted image

D. Image Cropping

Image objects read by cv2 are 3D-NumPy-arrays where dimensions represent the height, width, and the channels of the image accordingly (Image shape: (H * W * Ch)). You can simply crop the height or the width of the image by directly cropping the NumPy array. For example-

image = read_image(image_path=image_path)
image = image[100:300, 100:300, :]
show_image(image)

Output cropped image-

Cropped image
cropped image

5. Application Areas of OpenCV

OpenCV-toolkit acts as the heart of the most Computer Vision(Computer Graphics projects) projects. Any application area that involves playing with image kind of data, is probably using this library inside. Here is a quick list of the different common application areas of OpenCV-toolkit (source: Wikipedia):-

Click here to read my article on “Python predicts PUBG mobile” where I have used OpenCV to process my video data.

  1. 2D and 3D feature toolkits
  2. Egomotion estimation
  3. Facial recognition system
  4. Gesture recognition
  5. Human–computer interaction (HCI)
  6. Mobile robotics
  7. Motion understanding
  8. Object identification
  9. Segmentation and recognition
  10. Stereopsis stereo vision: depth perception from 2 cameras
  11. Structure from motion (SFM)
  12. Motion tracking
  13. Augmented reality

6. Conclusion

This article talks about the common Computer Vision (Computer Graphics) concepts. It explains the OpenCV-AI-toolkit in brief with examples of some simple operations over image data. It also lists down common advantages of using this library and why people choose it for their vision based projects. I hope this article gives enough background to the beginners and helps them in getting started on the c0mputer vision-based projects.

This was all about this post, thanks for reading. Kindly share your valuable feedback with me by commenting below.


7. Resources:

  1. https://opencv.org/
  2. https://en.wikipedia.org/wiki/OpenCV