top of page

What is OpenCV?

OpenCV is a cross-platform library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture and analysis including features like face detection and object detection.


Installing OpenCV

First of all, you need to download OpenCV onto your system. Follow the steps given below.


Step 1 − Open the homepage of OpenCV by clicking the following link: http://opencv.org/ On clicking, you will see its homepage as shown below.



Step 2 − Now, click the Downloads link highlighted in the above screenshot. On clicking, you will be directed to the downloads page of OpenCV.



Step 3 − On clicking the highlighted link in the above screenshot, a file named opencv-3.1.0.exe will be downloaded. Extract this file to generate a folder opencv in your system, as shown in the following screenshot.



Step 4 − Open the folder OpenCVbuildjava. Here you will find the jar file of OpenCV named opencv-310.jar. Save this file in a separate folder for further use.



OpenCV : Storing Image

To capture an image, we use devices like cameras and scanners. These devices record numerical values of the image (Ex: pixel values). OpenCV is a library which processes the digital images, therefore we need to store these images for processing.

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.

This class comprises of two data parts: the header and a pointer

  • Header − Contains information like size, method used for storing, and the address of the matrix (constant in size).

  • Pointer − Stores the pixel values of the image (Keeps on varying).


OpenCV : Reading Image

The Imgcodecs class of the package org.opencv.imgcodecs provides methods to read and write images. Using OpenCV, you can read an image and store it in a matrix (perform transformations on the matrix if needed). Later, you can write the processed matrix to a file.

The read() method of the Imgcodecs class is used to read an image using OpenCV. Following is the syntax of this method.

imread(filename)

It accepts an argument (filename), a variable of the String type representing the path of the file that is to be read.


Given below are the steps to be followed to read images in Java using OpenCV library.


Step 1: Load the OpenCV native library

Load the OpenCV native library using the load() method, as shown below.

//Loading the core library 
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

Step 2: Instantiate the Imgcodecs class

Instantiate the Imgcodecs class.

//Instantiating the Imgcodecs class 
Imgcodecs imageCodecs = new Imgcodecs();

Step 3: Reading the image

Read the image using the method imread(). This method accepts a string argument representing the path of the image and returns the image read as Mat object.

//Reading the Image from the file  
Mat matrix = imageCodecs.imread(Path of the image);

Example

The following program code shows how you can read an image using OpenCV library.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

public class ReadingImages 
{
    public static void main(String args[]) 
    {
        //Loading the OpenCV core library  
        System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
        
        //Instantiating the Imagecodecs class 
        Imgcodecs imageCodecs = new Imgcodecs();
        
        //Reading the Image from the file  
        String file ="C:/EXAMPLES/OpenCV/sample.jpg";
        Mat matrix = imageCodecs.imread(file);
        
        System.out.println("Image Loaded");
    }
}

On executing the above program, OpenCV loads the specified image and displays the following output −

Image Loaded


OpenCV: Writing Image

The write() method of the Imgcodecs class is used to write an image using OpenCV. To write an image, repeat the first three steps from the previous example.


To write an image, you need to invoke the imwrite() method of the Imgcodecs class.


Following is the syntax of this method.

imwrite(filename, mat)

This method accepts the following parameters −

  • filename − A String variable representing the path where to save the file.

  • mat − A Mat object representing the image to be written.


Example

Following program is an example to write an image using Java program using OpenCV library.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

public class WritingImages 
{
      public static void main(String args[]) 
      {
            //Loading the OpenCV core library  
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
            
            //Instantiating the imagecodecs class Imgcodecs 
            imageCodecs = new Imgcodecs();
            
            //Reading the Image from the file and storing it in to a       
            Matrix object 
            String file ="C:/EXAMPLES/OpenCV/sample.jpg";
            Mat matrix = imageCodecs.imread(file);
            
            System.out.println("Image Loaded ..........");
            
            String file2 = "C:/EXAMPLES/OpenCV/sample_resaved.jpg";
            
            //Writing the image 
            imageCodecs.imwrite(file2, matrix);
      
            System.out.println("Image Saved ............");
        }
    }

On executing the above program, you will get the following output −

Image Loaded .......... 
Image Saved ...........

If you open the specified path, you can observe the saved image as shown below −


Features of OpenCV Library

Using OpenCV library, you can −

  • Read and write images

  • Capture and save videos

  • Process images (filter, transform)

  • Perform feature detection

  • Detect specific objects such as faces, eyes, cars, in the videos or images.

  • Analyze the video, i.e., estimate the motion in it, subtract the background, and track objects in it.

OpenCV was originally developed in C++. In addition to it, Python and Java bindings were provided. OpenCV runs on various Operating Systems such as windows, Linux, OSx, FreeBSD, Net BSD, Open BSD, etc.


OpenCV Library Modules

Following are the main library modules of the OpenCV library.


1. Core Functionality

This module covers the basic data structures such as Scalar, Point, Range, etc., that are used to build OpenCV applications. In addition to these, it also includes the multidimensional array Mat, which is used to store the images. In the Java library of OpenCV, this module is included as a package with the name org.opencv.core.


2. Image Processing

This module covers various image processing operations such as image filtering, geometrical image transformations, color space conversion, histograms, etc. In the Java library of OpenCV, this module is included as a package with the name org.opencv.imgproc.


3. Video

This module covers the video analysis concepts such as motion estimation, background subtraction, and object tracking. In the Java library of OpenCV, this module is included as a package with the name org.opencv.video.


4. Video I/O

This module explains the video capturing and video codecs using OpenCV library. In the Java library of OpenCV, this module is included as a package with the name org.opencv.videoio.


5. calib3d

This module includes algorithms regarding basic multiple-view geometry algorithms, single and stereo camera calibration, object pose estimation, stereo correspondence and elements of 3D reconstruction. In the Java library of OpenCV, this module is included as a package with the name org.opencv.calib3d.


6. features2d

This module includes the concepts of feature detection and description. In the Java library of OpenCV, this module is included as a package with the name org.opencv.features2d.


7. Objdetect

This module includes the detection of objects and instances of the predefined classes such as faces, eyes, mugs, people, cars, etc. In the Java library of OpenCV, this module is included as a package with the name org.opencv.objdetect.


8. Highgui

This is an easy-to-use interface with simple UI capabilities. In the Java library of OpenCV, the features of this module is included in two different packages namely, org.opencv.imgcodecs and org.opencv.videoio.



Advantages:

Vast Algorithms

OpenCV gives access to more than 2,500 state-of-the-art and classic algorithms. By using this library, users can perform various tasks like removing red eyes, extracting 3D models of objects, following eye movements, etc.


Extensive Use

Big companies like IBM, Google, Toyota and even startups like Zeitera and Applied Minds are using OpenCV for multifarious tasks. This way, users are assured that they have access to a library that is being used by government institutions and enterprises.

In the vast community of OpenCV, users can ask for assistance and provide help to other developers. This gives developers access to insights of people about library and codes.


Efficient Solution

OpenCV provides algorithmic efficiency mainly to process real-time programs. Moreover, it has been designed in a way that allows it to take advantage of hardware acceleration and multi-core systems to deploy.



The Tech Platform

0 comments

Comments


bottom of page