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.
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
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
cvMatMul(CvArr *mat1, CvArr *mat2, CvArr *result);
where,
mat1 - First array
mat2 - Second array
result - Resultant array
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.
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 structurecvInitMatHeader( 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 matricesCvMat 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 matricescvMatMul(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 matrixwhere,
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:
nice
Nice!!!
Thank you!!!
Thanks for this.
Thank you so much for this..:)
Post a Comment