Explain the Mat class in Java OpenCV library


In OpenCV, images are stored in Using Mat object. It is nothing but 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.

If you try to read an image using the OpenCV library it will be read to a Mat object.

Mat matrix = Imgcodecs.imread(filePath);

You can instantiate this class manually using one of the following constructors −

  • Mat() − A no-arg constructor, used to create an empty matrix and pass this to other OpenCV methods.

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

  • 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 a parameter.

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

  • 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 a parameter.

Following are the prominent methods of this class −

  • Mat col(int x) − This method accepts an integer parameter representing the index of a column and retrieves and returns that column.

  • Mat row(int y) − This method accepts an integer parameter representing the index of a row and retrieves and returns that row.

  • int cols() − This method returns the number of columns in the matrix.

  • int rows() − This method returns the number of rows in the matrix.

  • Mat setTo(Mat value) − This method accepts an object of the Mat type and sets the array elements to the specified value.

  • Mat setTo(Scalar s) − This method accepts an object of the Scalar type and sets the array elements to the specified value.

Updated on: 09-Apr-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements