My environment here is window 7 + vs2010 + MATLAB r2010b.
First, you must enter Mex-setup to determine the compiler to be used. Follow the prompts step by step.
The following is the c ++ file. The C ++ file name is the name of the function to be called in the future. Here is sum. cpp.
Sum. cpp:
# Include "Mex. H" // This is required. // The call form Re = sum (arr0, arr1) is used to add the two matrices and assign them to the result matrix. // Nlhs: Number of output parameters // plhs: output parameter list // nrhs: Number of input parameters // prhs: input parameter list void Mexico function (INT nlhs, mxarray * plhs [], int nrhs, const mxarray * prhs []) // equivalent to General Main () {int m0 = mxgetm (prhs [0]); // obtain the number of arr0 rows int N0 = mxgetn (prhs [0]); // obtain the number of arr0 columns double * parr0 = (double *) mxgetpr (prhs [0]); // obtain the arr0 pointer int M1 = mxgetm (prhs [1]); int n1 = mxgetn (prhs [1]); double * parr1 = (double *) mxgetpr (prhs [1]); If (M0! = N0 | M1! = N1) mexerrmsgtxt ("two matrix columns should be equal"); plhs [0] = mxcreatedoublematrix (M0, N0, mxreal); // create a M0 row, n0 matrix double * pre = (double *) mxgetpr (plhs [0]); For (INT I = 0; I <M0; I ++) {for (Int J = 0; j <N0; j ++) {pre [I * N0 + J] = parr0 [I * N0 + J] + parr1 [I * N0 + J]; // The two matrices are added to the result matrix one by one }}}
Set sum. place CPP in the current directory and enter Mex sum on the terminal. CPP can generate sum. mexw32, the generated file can be considered as the sum () function, and can be called directly according to the call specification.
Here I will add the two images main. M:
clear all;close all;clc;a=imread('rice.png');a=double(a);b=imread('cameraman.tif');b=double(b);re=SUM(a,b);re=mat2gray(re);imshow(re);
Final result: