OpenCV study notes: matrix/vector processing

Environment: CentOS7
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
$ pkg-config --modversion opencv
2.4.13
In general:
1.OpenCV uses C language for matrix operations. But there are actually many alternatives to the C++ language that can be done more efficiently.
2. In OpenCV, a vector is treated as an N-dimensional matrix with a dimension of 1.

3. The matrix is ​​stored row-row, and each row is aligned with 4 bytes (32 bits).

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<cv.h>
#include<highgui.h>
#include<iostream>
using namespace std;

int main(int argc,char**argv)
{
/*******Allocate memory for new matrix*******/
    /*CvMat* cvCreateMat(int rows, int cols, int type);
        type: Matrix element type. Specified by CV_<bit_depth>(S|U|F)C<number_of_channels>.
        For example: CV_8UC1 , CV_32SC2. */
    CvMat* M1 = cvCreateMat(4,4,CV_32FC1);

    /* free matrix memory */
    cvReleaseMat(&M1);

    /* copy matrix */
    CvMat* M2 = cvCreateMat(4,4,CV_32FC1);
    CvMat* M3;
    M3=cvCloneMat(M2);

    /*Initialize the matrix*/
    double a4[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    CvMat M4 = cvMat(3, 4, CV_64FC1, a4);
    //Equivalent to:
    CvMat M5;
    cvInitMatHeader(&M5, 3, 4, CV_64FC1, a4);

    /*Initialize the matrix to the identity matrix*/
    CvMat* M6 = cvCreateMat(4,4,CV_32FC1);
    cvSetIdentity(M6);

/*******Access matrix elements*******/
    /* Suppose you need to access the (i, j)th element of a 2D floating point matrix.*/
    /* indirect access */
    int i=1, j=2;
    //void cvmSet(CvMat*, int, int, double)
    cvmSet(&M5,i,j,99.0);
    //double cvmGet(const CvMat*, int, int)
    double t = cvmGet(&M5,i,j);
    printf("%lf\n",t);

    /* Direct access (assuming matrix data is aligned in 4-byte rows) */
    CvMat* M7 = cvCreateMat(4,4,CV_32FC1);
    int n = M7->cols;
    float *data7 = M7->data.fl;
    data7[i*n+j] = 13.0;
    printf("%lf\n",cvmGet(M7,i,j));

    /*Direct access (possible alignment gaps when there may be gaps in the row alignment of the data):*/
    CvMat* M8 = cvCreateMat(4,4,CV_32FC1);
    int step8 = M8->step/sizeof(float);
    float *data8 = M8->data.fl;
    (data8+i*step8)[j] = 23.0;
    printf("%lf\n",cvmGet(M8,i,j));

    /*Direct access to the initialized matrix*/
    double a9[16];
    CvMat M9 = cvMat (3, 4, CV_64FC1, a9);
    a9[i*4+j] = 32.0; // M9(i,j)=2.0;
    printf("%lf\n",cvmGet(&M9,i,j));

    return 0;
}

Compile and run:

$ make
g++ main.cpp `pkg-config --cflags --libs opencv`
$ ./a.out
99.000000
13.000000
23.000000
32.000000
In addition, OpenCV also provides operations between matrices, element-level operations between matrices, vector product, operations on a single matrix (transpose, inversion, etc.), inhomogeneous linear equation solving, eigenvalues ​​and eigenvectors (matrix For square matrix), singular value decomposition (SVD) and other operations, please refer to "OpenCV Chinese Reference Manual" for details

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325766061&siteId=291194637