Mat

The Mat class is used to represent a multidimensional single-channel or multi-channel dense array. Vectors, matrices, grayscale or color images, solid elements, point clouds, tensors, and histograms that can be used to store real or complex numbers (higher-dimensional histograms are best stored with SparseMat). In short, Mat is for storing multidimensional matrices. Mat object contains all kinds of basic information and image pixel data. Mat consists of a header and a data part, in which the header also contains a pointer to data.


Mat method

Due to space constraints, it cannot be shown. Go to the CSDN or public account to view the information.

methods use
public int height() {} The number of rows
public int width() {} The number of columns
public long getNativeObjAddr() {} Native object address

Mat bit operation

methods operation
public static void bitwise_not(Mat src, Mat dst) According to a non
public static void bitwise_and(Mat src1, Mat src2, Mat dst) Bitwise and
public static void bitwise_or(Mat src1, Mat src2, Mat dst) Bitwise or
public static void bitwise_xor(Mat src1, Mat src2, Mat dst) The bitwise exclusive or
An operation operation
The operation 1 becomes 0,0 becomes 1
With the operation The binary counterpart of the result is 1 only if both corresponding binary digits are 1, otherwise 0
Or operation The corresponding binary bit of the result is 0 only if both corresponding binary bits are 0, otherwise it is 1
Exclusive or operation The corresponding binary bit of the result is 1 only if the two corresponding binary bits are not identical, otherwise it is 0

Mat arithmetic operation

methods operation
public static void add(Mat src1, Mat src2, Mat dst) Matrix addition
public static void subtract(Mat src1, Mat src2, Mat dst) Matrix subtraction
public static void multiply(Mat src1, Mat src2, Mat dst) Matrix multiplication
public static void divide(Mat src1, Mat src2, Mat dst, double scale, int dtype) Matrix division

Matrix addition

The usual matrix addition is defined as two matrices of the same size. The sum of two M by n matrices A and B, labeled A plus B, is also an M by n matrix in which the elements are the sum of their corresponding elements. Such as:


Matrix subtraction

Subtraction of matrices, as long as they’re of the same size. Each element in A-B is the value of the subtraction of its corresponding element, and the matrix will have the same size as A and B. Such as:


Matrix multiplication

1. A and B can be multiplied when the number of columns of A is equal to the number of rows of B.

2. The number of rows of C is equal to the number of rows of A, and the number of columns of C is equal to the number of columns of B.

3. The MTH row and NTH column of product C is equal to the sum of the products of the MTH row of matrix A and the corresponding columns of matrix B.




Matrix division

For matrix division, we generally don’t say matrix division, usually talk about matrix inverse

Specific operations: we first divide the matrix into its inverse matrix, and then with another matrix matrix for matrix multiplication operations

Matrix inversion: Let A be A square matrix of order N on the number field, if there is another moment B of order N on the same number field, such that: AB=BA=E. Then we call B the inverse of A, and A is called an invertible matrix. Where E is the identity matrix. Typical matrix inversion methods include: inverse matrix by definition, elementary transformation, adjoint matrix, identity deformation, etc.

Mat bit operation and arithmetic operation implementation

class MatOperationActivity : AppCompatActivity() {



private lateinit var mBinding: ActivityMatOperationBinding

private lateinit var bgr: Mat

private lateinit var source: Mat



override fun onCreate(savedInstanceState: Bundle?). {

super.onCreate(savedInstanceState)

mBinding = DataBindingUtil.setContentView(this, R.layout.activity_mat_operation)



bgr = Utils.loadResource(this, R.drawable.lena)

source = Mat()

Imgproc.cvtColor(bgr, source, Imgproc.COLOR_BGR2RGB)

mBinding.ivSource.setImageResource(R.drawable.lena)

val bitmap = Bitmap.createBitmap(source.width(), source.height(), Bitmap.Config.ARGB_8888)

bitmap.density = DisplayMetrics.DENSITY_XXHIGH

Utils.matToBitmap(bgr, bitmap)

mBinding.ivBgr.setImageBitmap(bitmap)



}



override fun onCreateOptionsMenu(menu: Menu?).: Boolean {

menuInflater.inflate(R.menu.menu_mat_operation, menu)

return true

}



override fun onOptionsItemSelected(item: MenuItem): Boolean {

when (item.itemId) {

R.id.bitwise_not

-> bitwiseNot(source)

R.id.bitwise_and

-> bitwiseAnd(source, bgr)

R.id.bitwise_xor

-> bitwiseXor(source, bgr)

R.id.bitwise_or

-> bitwiseOr(source, bgr)

R.id.add

-> add(source, bgr)

R.id.subtract

-> subtract(source, bgr)

R.id.multiply

-> multiply(source, bgr)

R.id.divide

-> divide(source, bgr)

}

return true

}



private fun showResult(dst: Mat) {

val bitmap = Bitmap.createBitmap(dst.width(), dst.height(), Bitmap.Config.ARGB_8888)

Utils.matToBitmap(dst, bitmap)

mBinding.ivResult.setImageBitmap(bitmap)

}



private fun bitwiseNot(source: Mat) {

val dst = Mat()

Core.bitwise_not(source, dst)

showResult(dst)

dst.release()

}



private fun bitwiseAnd(source: Mat, attach: Mat) {

val dst = Mat()

Core.bitwise_and(source, attach, dst)

showResult(dst)

dst.release()

}



private fun bitwiseOr(source: Mat, attach: Mat) {

val dst = Mat()

Core.bitwise_or(source, attach, dst)

showResult(dst)

dst.release()

}



private fun bitwiseXor(source: Mat, attach: Mat) {

val dst = Mat()

Core.bitwise_xor(source, attach, dst)

showResult(dst)

dst.release()

}



private fun add(source: Mat, attach: Mat) {

val dst = Mat()

Core.add(source, attach, dst)

showResult(dst)

dst.release()

}



private fun subtract(source: Mat, attach: Mat) {

val dst = Mat()

Core.subtract(source, attach, dst)

showResult(dst)

dst.release()

}



private fun multiply(source: Mat, attach: Mat) {

val dst = Mat()

Core.multiply(source, attach, dst)

showResult(dst)

dst.release()

}



private fun divide(source: Mat, attach: Mat) {

val dst = Mat()

Core.divide(source, attach, dst, 50.0, -1)

Core.convertScaleAbs(dst, dst)

showResult(dst)

dst.release()

}

}

Copy the code

The results

According to a non


Bitwise and


Bitwise or


The bitwise exclusive or


Matrix addition


Matrix subtraction


Matrix multiplication


Matrix division


The source code

https://github.com/onlyloveyd/LearningAndroidOpenCV


This article is formatted using MDNICE