OpenCV - Storing Images



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).

The Mat Class

The OpenCV Java library provides this class with the same name (Mat) within the package org.opencv.core.

Constructors

The Mat class of OpenCV Java library has various constructors, using which you can construct the Mat object.

S.No Constructors and Description
1

Mat()

This is the default constructor with no parameters in most cases. We use this to constructor to create an empty matrix and pass this to other OpenCV methods.

2

Mat(int rows, int cols, int type)

This constructor accepts three parameters of integer type representing the number of rows and columns in a 2D array and the type of the array (that is to be used to store data).

3

Mat(int rows, int cols, int type, Scalar s)

Including the parameters of the previous one, this constructor additionally accepts an object of the class Scalar as parameter.

4

Mat(Size size, int type)

This constructor accepts two parameters, an object representing the size of the matrix and an integer representing the type of the array used to store the data.

5

Mat(Size size, int type, Scalar s)

Including the parameters of the previous one, this constructor additionally accepts an object of the class Scalar as parameter.

6

Mat(long addr)

7

Mat(Mat m, Range rowRange)

This constructor accepts an object of another matrix and an object of the class Range representing the range of the rows to be taken to create a new matrix.

8

Mat(Mat m, Range rowRange, Range colRange)

Including the parameters of the previous one, this constructor additionally accepts an object of the class. Range representing the column range.

9

Mat(Mat m, Rect roi)

This constructor accepts two objects, one representing another matrix and the other representing the Region Of Interest.

Note

  • Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices.

  • The type of the matrices were represented by various fields of the class CvType which belongs to the package org.opencv.core.

Methods and Description

Following are some of the methods provided by the Mat class.

S.No Methods and Description
1

Mat col(int x)

This method accepts an integer parameter representing the index of a column and retrieves and returns that column.

2

Mat row(int y)

This method accepts an integer parameter representing the index of a row and retrieves and returns that row.

3

int cols()

This method returns the number of columns in the matrix.

4

int rows()

This method returns the number of rows in the matrix.

5

Mat setTo(Mat value)

This method accepts an object of the Mat type and sets the array elements to the specified value.

6

Mat setTo(Scalar s)

This method accepts an object of the Scalar type and sets the array elements to the specified value.

Creating and Displaying the Matrix

In this section, we are going to discuss our first OpenCV example. We will see how to create and display a simple OpenCV matrix.

Given below are the steps to be followed to create and display a matrix in OpenCV.

Step 1: Load the OpenCV native library

While writing Java code using OpenCV library, the first step you need to do is to load the native library of OpenCV using the loadLibrary(). Load the OpenCV native library as shown below.

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

Step 2: Instantiate the Mat class

Instantiate the Mat class using any of the functions mentioned in this chapter earlier.

//Creating a matrix 
Mat matrix = new Mat(5, 5, CvType.CV_8UC1, new Scalar(0));

Step 3: Fill the matrix using the methods

You can retrieve particular rows/columns of a matrix by passing index values to the methods row()/col().

And, you can set values to these using any of the variants of the setTo() methods.

//Retrieving the row with index 0 
Mat row0 = matrix.row(0); 
     
//setting values of all elements in the row with index 0 
row0.setTo(new Scalar(1)); 
     
//Retrieving the row with index 3 
Mat col3 = matrix.col(3);  
     
//setting values of all elements in the row with index 3 
col3.setTo(new Scalar(3));

Example

You can use the following program code to create and display a simple matrix in Java using OpenCV library.

import org.opencv.core.Core; 
import org.opencv.core.Mat;  
import org.opencv.core.CvType;  
import org.opencv.core.Scalar;   

class DisplayingMatrix { 
   public static void main(String[] args) {     
      //Loading the core library 
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);     

      //Creating a matrix 
      Mat matrix = new Mat(5, 5, CvType.CV_8UC1, new Scalar(0));  

      //Retrieving the row with index 0 
      Mat row0 = matrix.row(0);

      //setting values of all elements in the row with index 0 
      row0.setTo(new Scalar(1)); 

      //Retrieving the row with index 3 
      Mat col3 = matrix.col(3);  

      //setting values of all elements in the row with index 3 
      col3.setTo(new Scalar(3)); 

      //Printing the matrix 
      System.out.println("OpenCV Mat data:\n" + matrix.dump()); 
   } 
}

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

OpenCV Mat data: 
[  1,   1,   1,   3,   1; 
   0,   0,   0,   3,   0; 
   0,   0,   0,   3,   0; 
   0,   0,   0,   3,   0; 
   0,   0,   0,   3,   0]

Loading Image using JavaSE API

The BufferedImage class of the java.awt.image.BufferedImage package is used to store an image and the ImageIO class of the package import javax.imageio provides methods to read and write Images.

Example

You can use the following program code to load and save images using JavaSE library.

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO;
  
public class LoadingImage_JSE_library {
   public static void main( String[] args ) throws IOException {
      //Input File 
      File input = new File("C:/EXAMPLES/OpenCV/sample.jpg");
          
      //Reading the image 
      BufferedImage image = ImageIO.read(input);
      
      //Saving the image with a different name
      File ouptut = new File("C:/OpenCV/sample.jpg");
      ImageIO.write(image, "jpg", ouptut);
         
      System.out.println("image Saved");
   } 
}

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

image Saved

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

Loading Image using JavaSE API
Advertisements