MATLAB and OPENCV hybrid programming under Ubuntu

Source: Internet
Author: User

Always wanted to learn the Mex interface function, but it was not used before, so no special care. This time because of running someone else's code, feel or make a matlab wrapper, this will be more convenient. But this involves a Mex mash-up and calls to OpenCV.

Thus, there are a lot of problems involved, including configuration problems, compilation problems, parameter transfer problems, and so on. However, once you can master him, you are equivalent to combining the advantages of MATLAB (easy to use, fast programming, no need to consider the definition of variables) and the advantages of C (faster calculation).

Here are a few links to learn about Mex files:

Www.cnblogs.com/lidabo/archive/2012/08/24/2654148.html

http://blog.csdn.net/njust_qhzt/article/details/8249008

Http://www.cnblogs.com/Key-Ky/p/4233581.html

Basic Format :

#include "mex.h"

void mexfunction (int nlhs, Mxarray *plhs[],
int NRHS, const mxarray *prhs[])
{

}

The four parameters are used to output and input data: NLHS The number of output parameters, PLHS output parameter pointers (NRHS and PRHS are related to input parameters). At the same time if it involves the compilation of OpenCV, generally also include "Mxarray." H

Note: The operation of our output and input parameters is done by pointers . (This is easy to understand, because our calculations are passed to Matlab, and we actually pass the pointer instead of the data.) MATLAB can access the in-memory data through these pointers. )

Mex file is essentially an interface file, so it involves data input and output, that is, from MATLAB data to C and from c to the data to MATLAB.

manipulating input data (matlab->c)

To manipulate the input data, you need to get the pointer address of the data through the MEX function MXGETPR. Mxgetm and MXGETN get the rows and columns of the matrix data (return integers). For real matrices, we can define double *m; To manipulate the real-matrix data. such as:

Double *m;

int m,n;

The pointer points to the data address of the first parameter

M = MXGETPR (Prhs[0]);

m = Mxgetm (Prhs[0]);

n = mxgetn (prhs[0]);

1. If the input is a single constant, you can use the

int x
x = Mxgetscalar (prhs[0]); This function obtains the value that Matlab passes over; 2. In the case of matrices (vectors are counted):

Datacursor = MXGETPR (Prhs[0]);//Gets the pointer of the first element of the input matrix int mrows = Mxgetm (Prhs[0]);   Gets the line int ncols = MXGETN (Prhs[0]) of the Matrix; Get the columns of the matrix
This allows the matrix pointer to be datacursor for further operation, and a detailed example is given below.
Special cases, if the transmission is image,image itself is also a matrix, but may be three-dimensional, so you can use the Mxarray function to implement the transfer.
3. If it is a string:
Char *input_buf; Input_buf = mxarraytostring (Prhs[0]); Convert Mxarray to C, C + + string using mxarraytostring

It is important to note that theMATLAB matrix data storage order is "from top to bottom, from left to right" , this is the same as FORTRAN. That is, for Matlab, the M x n matrix A. A () is *m,a (2,1) is * (m+1), and so on, a (I,J) is * (M + m* (j-1) + (i-1)).

Note: The MATLAB indicator starts at 1 and the C indicator starts at 0.

Operation output Data

For the output data, we need to allocate memory space first, there is a special MEX function can be used, such as:

Plhs[0] = Mxcreatedoublematrix (M,n, mxreal); Generates a real matrix of M x N.

In addition there are Mxcreatenumericmatrix (m,n,mxreal);. Refer to the third link above for details

As with the input data, to operate on the output data, we also need a pointer variable that points to the data , such as

Double *a;

A = MXGETPR (Plhs[0]);

Then we can pay a for the value.

Here are a few examples :

1. Matrix Incoming and outgoing

#include "mex.h"
void mexfunction (int nlhs, Mxarray *plhs[],
int NRHS, const mxarray *prhs[])
{
Double *indata;
Double *outdata;
int m,n;
int i,j;

 

INDATA=MXGETPR (Prhs[0]); Gets the first parameter pointer of the input
M=mxgetm (Prhs[0]);
N=MXGETN (Prhs[0]);

Plhs[0]=mxcreatedoublematrix (M,n,mxreal); Build a matrix for the output
OUTDATA=MXGETPR (Plhs[0]); Pointer
for (I=0;i <m;i++)

for (J=0;j <n;j++

Outdata[j*m+i] =indata[(n-1-j) *m+i];
}
2. Also the matrix incoming and outgoing

#include "mex.h" void mexfunction (int nlhs, Mxarray *plhs[], int nrhs, const Mxarray *prhs[]) {double *datacursor; A pointer to the first input variable
vector<vector< double > > parms; Using the vector definition matrix of C + +
Datacursor = MXGETPR (Prhs[0]);   Gets the pointer of the first element of the input matrix int mrows = Mxgetm (Prhs[0]);   Gets the line int ncols = MXGETN (Prhs[0]) of the Matrix;  Get the matrix of the columns printf ("%d_%d\n", Mrows, Ncols);  Print rows and Columns parms.resize (mrows); Initialize for (int i = 0; i < mrows; i++) {parms[i].resize (ncols);//Each one-dimensional array element Initializes a vector and then gets a matrix
} for (int i = 0, i < mrows; i++) {for (int j = 0; J < Ncols; J + +) {parms[i][j] = dat Acursor[j * mrows + i]; Copy the elements of the matrix to vector of vector}} =================================================================
The following talk about OpenCV, if the direct image to C, then you can directly use Cv::mat image = Mxarray (Plhs[0]). Tomat ();
The resulting image is the mat format, not the vector format. If you want to change from mat to vector format, you can use im[i*w+j] = image.at<float> (I,J);
To achieve conversions, note that it is a float, not a double, because the accuracy of OPENCV and C is not equivalent.









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.