ubuntu system C++ call matlab procedures method details

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

preface

Recently, I have been studying how C++ calls matlab program due to the need of work. I found that there is less information on the Internet, so I will summarize and share what I have learned. I won't say any more in the following.

Experimental platform:

ubuntu matlab R2016b g++

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 mex-setup C++

Then enter the command: mbuild, setup

Also choose mex-setup C++ -client MBUILD

2. Compile.m documents

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 is completed, the image is saved 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 the.m file

**** note **** : compile the.m file under ubuntu. The file name should start with lib, which is crucial when configuring the path. Of course, it is better to start with lib under window.


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

Where -W is the encapsulation format after the control is compiled;

cpplib means lib compiled to C++;

The 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, libMypr.h libMypr.cpp libMypr.so libMypr.exports these four files are what we need to call in c++;

4. Write C++ code (test.cpp) under the test file on your desktop and copy the compiled.m file under it as well :(note initialization)


#include "mclmcrrt.h" 
#include "mclmcr.h" 
#include "mclcppclass.h" 
#include "matrix.h" 
#include <iostream> 
#include "libMypr.h" 
using namespace std ;  
 
int main(int argc, char* argv[])  
{  
 // Initialization,   in C++ call matlab Must be initialized.  
 if( !libMyprInitialize())  
 {  
  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   
 libMyprTerminate();    
 return 0;  
}  

5. If the above is ready, follow the steps below and you will be able to run.

(1) generate the compiled file test.o


g++ - c test.cpp -I/usr/local/MATLAB/R2016b/extern/include/ -o test.o

(2) set matlab dynamic file path


export LD_LIBRARY_PATH="/home/liupeng/Desktop/test:/usr/local/MATLAB/R2016b/runtime/glnxa64:$LD_LIBRARY_PATH"

(3) generate the execution file testApp


g++ -o testApp test.o -L/home/liupeng/Desktop/test
-L/usr/local/MATLAB/R2016b/runtime/glnxa64 -lmwmclmcrrt -L/home/liupeng/Desktop/test-lMypr

(4) operation./testApp

conclusion


Related articles: