windows system C++ call matlab procedures method details

  • 2020-05-27 06:52:17
  • OfStack

preface

I have already introduced the method of C++ calling matlab program in ubuntu system. If you need, you can refer to this article. This paper will introduce the content of C++ calling matlab program under windows, and share it for your reference and learning.

Experimental platform:

matlab R2016b VS2013

Ideas:

1. Set the compiler of matlab and use the external compiler VC or gcc.

2. Compile the m file into dll

3. Set VS's Include path and lib's link library path

4. Write C++ to call dll

Steps:

1. Set the compiler for matlab

Under the command line window, enter and execute the following command: mex �setup

In the compiler that appears, select VS2013

Then enter the command: mbuild �setup VS2013 is also selected

2. Compile.m file

The first parameter is the file name (path) of the image. The second parameter is the size of the threshold value of the segmentation image. After the segmentation, save the image as result.bmp; The return value is the data of the original image.


function imagedata=improcess(filename,threshold); 
imagedata=double(imread(filename)); 
newbuf=imagedata; 
[M N]=size(imagedata); 
for i=1:1:M 
 for j=1:1:N 
  if imagedata(i,j)>threshold 
   newbuf(i,j)=255; 
  else 
   newbuf(i,j)=0; 
  end 
 end 
end 
imwrite(uint8(newbuf),'result.bmp'); 
return; 

3. Compile.m


mcc -W cpplib:pr_lib -T link:lib improcess.m

Explanation: where -W is the encapsulation format after the control is compiled;

cpplib means lib compiled into C++;

cpplib colon is the name of the compiled library;

-T represents the target, link:lib represents the target to connect to a library file, and the name of the target is the name of the.m function.

After compiling, pr_lib.h pr_lib.libpr_lib.dll these three files are what we need to call in c++; These three files correspond to the three files generated when we wrote c++ dll;

Call steps in VS

1. Set the VC environment

Include the header file path of matlab and the corresponding library file path to VS; In VS, click on the project properties and then in the 'VC++ directory' TAB, which will contain the last line of the directory, add the matlab related header file; For example, the path corresponding to my matlab is:

........\MATLAB\R2016b\extern\include

........\MATLAB\R2016b\extern\include\win64

Add the path to the matlab related statically linked library to the library directory:

........\MATLAB\R2016b\extern\lib\win32\microsoft

Will link - > Input:


libeng.lib
libmat.lib
libmex.lib
mclmcrrt.lib
pr_lib.lib

2. Create a new console-based hello World program;

2.1 add the required header files and the required static link library

If you have added lib to the link library above, just add the header file below.


#pragma comment(lib,"mclmcrrt.lib") 
#pragma comment(lib,"libmx.lib") 
#pragma comment(lib,"libmat.lib") 
#include "matrix.h" 
#include "stdafx.h" 
#include <iostream> 
#include "pr_lib.h" 

2.2 copy the pr_lib.h pr_lib.libpr_lib.dll file generated by the compilation of matlab into the project directory; And added the header and static link library to the project:


#pragma comment(lib,"pr_lib.lib")
#include "pr_lib.h"

2.3 edit main function and call improcess function;


int main(int argc, char* argv[]) 
{ 
 // Initialization,   in C++ call matlab Must be initialized.  
 if( !pr_libInitialize()) 
 { 
  printf("Could not initialize !"); 
  return -1;  
 } 
 char f_name[10]="lenna.pgm"; 
// Must be lenna.pgm Image, copy to project directory  
 mwArray file_name(f_name);//'lenna.pgm' 
 mwArray m_threshold(1,1, mxDOUBLE_CLASS); 
 m_threshold(1,1)=128;// The threshold for 128 
 mwArray ImageData(512,512, mxDOUBLE_CLASS); 
 improcess(1,ImageData,file_name,m_threshold); 
//1, Is the number of return values, ImageData Is used to receive the return value  
 printf("\n End of image processing , Already image at threshold value 128 Separated! \n"); 
 double *resultdata=new double[512*512]; 
 ImageData.GetData(resultdata,512*512); 
 printf("\n Image data has been obtained ...\n"); 
 for(int i=0;i<512;i++) 
 { 
  for(int j=0;j<512;j++) 
  { 
   printf("%0.1f ",resultdata[512*i+j]); 
  } 
  printf("\n"); 
 } 
 delete []resultdata; 
 //  The back is 1 The program that terminates the call  
 // terminate the lib  
 pr_libTerminate();   
 return 0; 
} 

As can be seen from the above program, c++ and matlab function data transfer is completed with the mwArray class defined by matlab! The data types supported by this class are:


/*typedef enum
 {
 mxUNKNOWN_CLASS = 0,
 mxCELL_CLASS,
 mxSTRUCT_CLASS,
 mxLOGICAL_CLASS,
 mxCHAR_CLASS,
 mxVOID_CLASS,
 mxDOUBLE_CLASS,
 mxSINGLE_CLASS,
 mxINT8_CLASS,
 mxUINT8_CLASS,
 mxINT16_CLASS,
 mxUINT16_CLASS,
 mxINT32_CLASS,
 mxUINT32_CLASS,
 mxINT64_CLASS,
 mxUINT64_CLASS,
 mxFUNCTION_CLASS,
 mxOPAQUE_CLASS,
  mxOBJECT_CLASS}*/

We should also note that the array of objects defined by mwArray class still starts at 1 instead of 0 as c++ does! For a more detailed use of mwArray, see mclcomclass.h

conclusion


Related articles: