home | non-tech | cs | ee | about
Abstract New

Open CV Tutorial - Getting started with OpenCV - Part 3

In order to process images we need to get a good understanding of how to work with matrices. OpenCV has a lot of functionality for matrix manipulation.

Playing around with matrices

In this article, we will see how to create, initialize and play around with matrices.

Multiply two matrices and find the inverse of the resultant matrix.

Assuming you have included the necessary header files (see previous posts) let us proceed onto the main program.

Declare a float array and initialize a OpenCV matrix with the contents of the float array


//Declare two openCV matrices
CvMat matrix1;
CvMat matrix2;

//Say we have a float array with values
double dataHeaderMat1[3][3] = {1, 2, 3, 1, 1, 1, 10, 10, 10};
double dataHeaderMat2[3][3] = {1, 1, 1, 1, 1, 1, 3, 2, 1};
double resultInit[3][3];
double invresultInit[3][3];

//Now, if we need to initialize the matrix with data 
//say (in this case) from  an float array.
cvInitMatHeader( &matrix1, 3, 3, CV_64FC1, dataHeaderMat1);
Function cvInitMatHeader - Used to initialize the matrix header structure
cvInitMatHeader( CvMat *mat, int rows, int cols, int type, void *dataSrc = NULL, int step = CV_AUTOSTEP);
where,
mat - Pointer to the matrix to be initialized
rows - Number of rows in the matrix
cols - Number of columns in the matrix
type - Data Type of elements, there are a lot of OpenCV defined types for type. CV_64FC1 is 64 bit floating point.
data - Pointer to the data source to be assigned to the matrix header. This parameter is optional

Another way to initialize the matrix


//Initialize the second matrix 
cvInitMatHeader( &matrix2, 3, 3, CV_64FC1, dataHeaderMat2);

//Initialize the matrix to store the results in 
CvMat resultMat = cvMat(3, 3, CV_64FC1, resultInit);

Function cvMat - Another version used to Initialize matrices
CvMat cvMat(int rows, int cols, int type, void *data = NULL);
where,
rows - Number of rows
cols - Number of columns
type - Data type of elements
data - Optional pointer to the data source
//Multiply the two matrices and store the result in the result mat
cvMatMul( &matrix1, &matrix2, &resultMat);
Function cvMatMulAdd - Function used to multiply two matrices
cvMatMul(CvArr *mat1, CvArr *mat2, CvArr *result);
where,
mat1 - First array
mat2 - Second array
result - Resultant array

Calculate the matrix inverse

Find the inverse of the matrix.
//Initialize a matrix to hold the results of the inverse
CvMat invResultMat = cvMat(3, 3, CV_64FC1, invresultInit);

//Find the inverse of the resultant matrix
cvInv(&resultMat, &invResultMat, CV_LU);

Function cvInv - Used to find the inverse of the matrix
where,
cvInv(const CvArr *src, const CvArr *dst, int method);
where,
src - Source Matrix
dst - Destination Matrix
method - Inversion method - OpenCV has a couple of methods CV_LU - The LU decomposition method and CV_SVD based on the Singular Value Decomposition method.


Final Code


void main() //your main program or function
{
    CvMat matrix1;
    CvMat matrix2;

    double dataHeaderMat1[3][3] = {1, 2, 3, 1, 1, 1, 10, 10, 10};
    double dataHeaderMat2[3][3] = {1, 1, 1, 1, 1, 1, 3, 2, 1};
    double resultInit[3][3];
    double invresultInit[3][3];
    
    cvInitMatHeader( &matrix1, 3, 3, CV_64FC1, dataHeaderMat1);
    cvInitMatHeader( &matrix2, 3, 3, CV_64FC1, dataHeaderMat2);
    CvMat resultMat = cvMat(3, 3, CV_64FC1, resultInit);
    cvMatMul( &matrix1, &matrix2, &resultMat);

    CvMat invResultMat = cvMat(3, 3, CV_64FC1, invresultInit);
    cvInv(&resultMat, &invResultMat, CV_LU);

}
Note - Accessing Individual Elements of a Matrix Individual elements of an OpenCV matrix can be accessed by the function CV_MAT_ELEM(matrix, datatype, row, column).

4 comments:

Anonymous said...

nice

CAO NAM said...

Nice!!!
Thank you!!!

James Mock said...

Thanks for this.

Anonymous said...

Thank you so much for this..:)

Post a Comment

© 2014 - 2015 abstract new. All rights reserved.