Beginner’s Guide to OpenCV

0
562

Introduction To Beginner’s Guide to OpenCV

This is Beginner’s Guide to OpenCV, One of the most advanced and compelling types of Machine Learning applications of the present decade is undoubtedly Computer Vision. Knowingly or unknowingly, you have experienced one of its numerous utilization. From Tesla’s self-driving cars to the trite filters you use on Instagram to enhance that selfie of yours, Computer Vision has taken over the data science domain.

Computer vision is the field of computer science that is centered on replicating parts of the complexity of the human eye (our vision) and enabling machines to distinguish and process objects in images and videos in the same way that we do. If deep learning focuses on replicating the human brain, computer vision is based on developing algorithms that perceive objects the way the human eye does. While it sounds very futuristic, the machine learning and deep learning libraries of today have allowed computer vision applications to grow rapidly. One of the main factors that have enabled this is the amount of data that is churned and can be stored in recent times.

Another compelling factor is the existence of advanced libraries that can detect and label images. OpenCV is one such open-source library for computer vision, deep learning, and image recognition. It supports a variety of languages including Python, Java, and C++. Through this blog post, we will explain some important features in OpenCV and implement some basic image calibration methods.

Installing OpenCV

Let’s Start This Beginner’s Guide to OpenCV with Installation, You can easily install the OpenCV library by using pip install.

pip install opencv

OpenCV Modules

OpenCV is truly an extensive library and one of the best bets for implementing any computer vision application. Some of the features it comes bundled with are –

  1. Ability to read and write images
  2. Object detection in images as well as video files
  3. Processing images (applying filters, transforms)
  4. Feature detection
  5. Analyze and modify videos (add background, track specific objects etc.)

Let’s move on to find out about some important modules in OpenCV that make it so useful.

1. Core Functionality

The OpenCV core includes functionalities to scan, retrieve objects and store images. Operations included are masking, blending, contrast, brightness and even applying Fourier transform to an image. The main function of the core is its ability to store images in OpenCV’s popular multidimensional array – Mat. The core functions are available in the org.opencv.core package.

MatBasicImageForComputer.jpg
Source – OpenCV docs

2. Image Processing

Using the Image Processing component of OpenCV, you can perform a myriad of image transformation techniques. These include –

  1. Basic operations (smoothing, erosion and dilating, hit or miss, thresholding etc.)
  2. Transformations (Adding borders, Line transform, Circular transform, linear filters etc.)
  3. Intensity modification using histograms
  4. Contours
  5. Image segmentation
  6. Noise removal filter
Morphology_1_Result.jpg
Source – OpenCV docs

All these operations can be accessed using the org.opencv.imgproc package in OpenCV.

3. Video Analysis

org.opencv.video – Covers the video analysis concepts such as object motion estimation, background subtraction, and object tracking.

org.opencv.videoio – Contains video writer functions and ability to capture videos from cameras, image sequences and other files.

Opencv face recognition /detection system using java in Netbeans with  source code - Code Guid
Source – Code Guid

4. Feature Modification and Object Detection

The org.opencv.features2d framework includes key functionalities of performing feature detection and identifying descriptions. Object categorization and drawing functions can also be implemented using this library.

When it comes to computer vision, we know that object detection within images is highly essential. OpenCV contains the functionality to detect some predefined objects and instances like faces, eyes, cars, pedestrians etc. It is included as a package named org.opencv.objdetect.

5. Image Calibration

OpenCV also has a module that includes algorithms for basic multiple-view geometry algorithms, single and stereo camera calibration, object pose estimation, stereo correspondence and elements of 3D reconstruction. All this is available in the org.opencv.calib3d package.

Let us now learn some important features in OpenCV by implementing them.

First, we import OpenCV and other necessary packages.

Reading the Image

Viewing the image –

Converting to grayscale – This is an elementary technique used to convert images to black and white. A color image consists of 3 channel depths. While using grayscaling, we reduce the depth of the image to 1 channel. It also reduces model complexity and is useful in applications like edge detection, animations etc.

Now let’s implement some smoothing techniques

Average Blurring Median BlurringGaussian Blurring
In this technique, image is smoothened and depending on the size of the filter convolution is performed. It averages the pixels under the filter and assigns the value to the centre pixel under the filter.Here, the function calculates the median of the pixels under the filter and replaces the center value with the median value. It gives similar results like Average blurring.This technique uses external parameters called sigma x and sigma y. It performs a blurring of an image by using these parameters. If it is set to zero then it calculates based on the size of its kernel.

Morphological Techniques

MethodDefinitionOutput
ErosionThis technique removes the boundary pixels from the input image. It is used for probing and reducing the shapes contained in the input image. Generally used to remove noise in an image and shrink it.


DilationIn dilation, we add additional pixels to the input. It is used when the pixels are missing in the image. This is used to add dimensionality to images and avoid the loss of pixels.

Application to check no. of objects in an image using the aforementioned techniques –

First we load the object,

img = cv2.imread (‘shapes.jpeg’)
cv2.imshow(“Image”, img)
cv2.waitKey(0)

Next we detect edges by first converting it to grayscale and then using the Canny filter.

Additionally we apply thresholding to separate the objects from the background.

We use the contouring technique to find out the number of objects.

Thus through this blog post we have discussed Beginner’s Guide to OpenCV and some important methods and functions in the OpenCV library. Computer Vision is highly essential for deep learning, image analysis, and face recognition technologies. The advancements that deep learning has provided to the computer vision field have left no stone unturned in making the data science domain as advanced as it is. OpenCV is the leader in this very channel and if your projects involve image recognition and analysis, this should be your go-to library!

LEAVE A REPLY

Please enter your comment!
Please enter your name here