Introduction to OpenCV

Raman Singh
4 min readJan 2, 2021

--

OpenCV is one of the most popular computer vision libraries. If you want to start your journey in the field of computer vision, then a thorough understanding of the concepts of OpenCV is of paramount importance.
OpenCV library is widely used in Python for building real-time Machine Learning and Deep Learning applications.

Its cross-platform support and availability in multiple programming languages allow us to develop applications that can be used on different systems. OpenCV is also fast and efficient as compared to the other libraries for computer vision (Caffe, Keras, etc).

Computer Vision Advantages

Now, in the OpenCV tutorial, let’s learn about the benefits of computer vision:

  • Open source: The source code of OpenCV is free to modify and customize according to individual requirements.
  • Fast: Originally OpenCV is written in C++. Its performance is as fast as C++, the Python wrappers use C++ code in the background.
  • Easier to integrate: OpenCV makes use of numpy arrays, which are efficient for performing operations and data can be used with other libraries like matplotlib and scikit-learn.
  • Ease of coding: Python is easier to code as compared to other languages. We don’t need to take care of pointers, memory management, etc.
  • Fast prototyping: With Python OpenCV library, we can quickly build prototypes. Python is concise, clean and shorter code is needed to perform some tasks with other languages.

Applications of Computer Vision

1. Event Detection

CCTV cameras are everywhere around us in offices, roads, hospitals, banks, train stations, parking lots, etc and 24/7 surveillance is difficult. The computer vision techniques allow us to monitor the events in real-time and detect anomalies or any specific action detection. Automatic Number Plate Recognition (ANPR) system can be used to control automatic gates, vehicle tracking, analyzing crowd and counting number of people.

2. Industrial Automation

Automatic inspection of objects and classifying them into different categories play a major role in manufacturing industries. Industrial robots use computer vision algorithms to perform various tasks like separating two or more different objects into their respective categories, detecting whether the product is labeled or not. If not, then we can reject the product on the conveyor belt.

3. Medical Image Processing

The progress in computer vision has led to extensive use of the medical imaging data to provide us with better prediction, diagnosis, and treatment of diseases. Some examples of this are the detection of tumors, arteriosclerosis or other malign changes, measurement of organ dimensions, blood flow, enhancement of ultrasonic or X-ray images that are interpreted by humans.

4. Self-driving Vehicles

Artificial Intelligence is on the boom right now and companies are investing in self-driving technology. Computer vision is used to detect lanes and find a path for autonomous vehicles. Information from various sensors is analyzed and used to detect objects on the path like traffic lights, traffic signs and according to obstacles, we decide the appropriate action that needs to be taken.

5. Military Applications

The military is probably one of the largest areas for computer vision. More advanced systems for missile guidance are developed to make dynamic decisions based on the information provided by various sensors including image sensors. Modern military concepts are emerging like “Battlefield awareness” in which strategic decisions are made after analyzing the information provided by sensors, detecting enemy soldiers or vehicles.

Installation

pip install opencv-python

Importing the library

import cv2

Basic Image Operations

Now that we have installed OpenCV on our workstations, let’s get our hands dirty with some of the functionalities that OpenCV offers.

Reading an image

my_pic = cv2.imread(‘mypic.png’)

Display an Image

cv2.imshow(‘mypic’, my_pic)

Saving an Image

Saving an image is a very commonly used feature, as we may need to update our image and save the changes to the file system for later use. OpenCV has an cv2.imwrite() function to save images.

Here is an example:

cv2.imwrite(‘mypic.png’, my_pic)

Resizing the Image

resize = cv2.resize(image, (800, 800))

Rotating the Image

# Calculating the center of the image

center = (w // 2, h // 2)

# Generating a rotation matrix

matrix = cv2.getRotationMatrix2D(center, -45, 1.0)

# Performing the affine transformation

rotated = cv2.warpAffine(image, matrix, (w, h))

Displaying text

# Copying the original image

output = image.copy()

# Adding the text using putText() function

text = cv2.putText(output, ‘OpenCV Demo’, (500, 550),

cv2.FONT_HERSHEY_SIMPLEX, 4, (255, 0, 0), 2)

Thank you for reading! I would appreciate any comments, notes, corrections, questions or suggestions — if there’s anything you’d like me to write about, please don’t hesitate to let me know.

--

--

Raman Singh
Raman Singh

No responses yet