Matlab has great advantages in data analysis when processing images. Because other programming languages, such as C + +, are generally used when docking with enterprises, Matlab functions can be compiled into dll files and called in C + +. There will be many errors when using it for the first time, and many errors can not be modified by the prompt of the compiler. This paper will detail the whole process of use and list almost all possible errors, hoping to be helpful to readers.
Experimental environment
- Operating system: Windows XP
- C++ IDE: Visual Studio 2010
- Matlab version: Matlab 2012a
1. Preliminary environmental configuration
The method in this article is applicable to all versions of Visual Studio+Matlab. The key is to ensure that the C + + compiler used can be supported by the currently used Matlab, View the compilers supported by each version of Matlab
Since Microsoft Visual C++ 2010 Professional is required for work docking, only the old version of MatlabR2012 supports VS2010. Click Detail to see
2. 32-bit Matlab installation
Because the operating system used is WindowsXP, it is a 32-bit operating system, which is only slightly different from Matlab in installation. The installation package downloaded from the Internet is usually a 32-bit / 64 bit installation package, while the installation tutorial only gives a 64 bit installation tutorial. In fact, we just need to find the corresponding 32-bit setup Exe can be installed according to the 64 bit method.
Generally, click the unzipped installation package to see
We need to click the bin folder under the same directory to see the win32 and win64 folders, which store 32-bit and 64 bit installers respectively. We can select the required win32 to open it and use the setup under the win32 directory Exe according to the 64 bit installation tutorial, you can complete the 32-bit Matlab installation.
After installation, open Matlab. We can enter mexext in the Command window to view it
Returning mexw32 indicates that the current Matlab supports the generation of win32 dynamic link libraries, and the 64 bit corresponds to mexw64.
3. Compiling dynamic link library with Matlab
Step 1: enter mex -setup in the command line window of matlab, and the following interface (old version) will appear. The new version will list all compilers existing in the current system. Click to select.
Step 2: create an image processing function in Matlab
function fun_imshow(path) I=imread(path); I1=rgb2gray(I); [Can,~]=edge(I1,'canny'); imshow(Can); end
Step 3: compile the functions of MATLAB** Note: only matlab function can be compiled here, not general functions m script.
There are two ways to use the mcc command:
1.mcc -W cpplib:fun_imshow -T link:lib fun_imshow.m -C 2.mcc -W cpplib:fun_imshow -T link:lib fun_imshow.m
The first method will generate a separate toolbox used by Matlab ctf file, you need to ctf files and dll file together with the generated exe file directory; The second method will not be compiled separately ctf file, just put dll file to exe file directory. We use the second method, and the generated files are as follows:
The only file we need is fun_imshow.dll,fun_imshow.h,fun_imshow.lib these three.
4. Configure VS2010
Step 1: create an empty project, add a new source file, and input a simple main() function to generate it in advance. At this time, an additional Debug folder will be added in the project directory.
Note: we use 32-bit Matlab, which needs to be debugged under Win32
Step 2: add fun_imshow.h,fun_ imshow. Copy lib to cpp files in the same directory, fun_ imshow. Copy DLL to the Debug directory (Note: there may be multiple Debug directories under the project directory, which should be copied to the Debug directory where. exe is generated)
Step 3: right click the project file and select Properties → \rightarrow →VC++ Directories
- Include Directories: matlabroot\extern\include
- Library Directories: matlabroot\extern\lib\win32\microsoft
matlabroot is the installation directory of the native Matlab.
Step 4: Properties → \rightarrow →Linker → \rightarrow →Input
Additional Dependencies: mclmcrrt.lib,libmat.lib,libmx.lib
So far, the environment configuration of VS2010 calling Matlab library is completed.
5. Testing
The following test procedures are provided
#include "fun_imshow.h" #include <iostream> #pragma comment(lib, "fun_imshow.lib") using namespace std; int main() { bool isOk = 0;//Judge whether the dynamic library is initialized successfully if (!mclInitializeApplication(NULL, 0)) { cout << "Could not initialize the application.\n"; return -1; } cout << "isOk = " << isOk << endl;// 0 isOk = fun_imshowInitialize(); // Dynamic library initialization succeeded cout << "isOk = " << isOk << endl;// 1 //system("pause"); fun_imshow("C:/Users/Admin/Desktop/aaa.jpg"); //Call matlab function mclWaitForFiguresToDie(NULL); //Wait for the image to be displayed. The image cannot be displayed without this sentence fun_imshowTerminate(); //Close dynamic library mclTerminateApplication(); return 0; }
Just modify fun_ The image display result can be output by inputting the image path of imshow() function.
Possible problems
- Mclmcrrtx not found_ xx. dll
mclmcrrtx_xx.dll is a file located in the tutime folder under the Matlab installation directory. We have this file after normal installation, and this file can be found through our path environment variable during program compilation. First, check whether the path environment variable of the computer contains the path of this file. If not, add the folder path of this file and restart the computer. Generally, after installing Matlab, it will be automatically added to us. If we compile after installing Matlab for the first time, the system still cannot find this file and need to restart the computer.
- 0xC0000005: an access violation occurred while reading position 0x00000000
This error is an exception thrown when using the 64 bit compilation environment, because we are using a dll file compiled by 64 bit Matlab, which is incompatible with Win32. At this time, the program can be compiled successfully, but cannot be executed. We just need to press Ctrl + Alt + e (or Debug) → \rightarrow → Exceptions) uncheck Win32 Exceptions.
supplement
Convert the C + + array data into the picture data format readable by Matlab (refer to Liu Wei's proficient in Matlab and C/C + + mixed programming)
The test procedures provided are as follows:
function fun_imshow(I) imshow(I); end
#include "fun_imshow.h" #include <iostream> #include <vector> #pragma comment(lib, "fun_imshow.lib") using namespace std; int main() { const int rows = 1000; const int columns = 1000; double* a; a = new double[rows*columns]; for(int i=0; i<rows*columns;i++){ a[i] = 0; } bool isOk = 0;//Judge whether the dynamic library is initialized successfully if (!mclInitializeApplication(NULL, 0)) { cout << "Could not initialize the application.\n"; return -1; } cout << "isOk = " << isOk << endl;// 0 isOk = fun_imshowInitialize(); // Dynamic library initialization succeeded cout << "isOk = " << isOk << endl;// 1 system("pause"); mwArray data(columns,rows,mxDOUBLE_CLASS); data.SetData(a,rows*columns); delete a; fun_imshow(data); //Call matlab function mclWaitForFiguresToDie(NULL); //Wait for the image to be displayed. The image cannot be displayed without this sentence fun_imshowTerminate(); //Close dynamic library mclTerminateApplication(); return 0; }
Realize the processing and display of any form of picture data by calling the dll generated by Matlab, as long as the picture data type is converted to mwarray type.
Note: Matlab data is read by column