Mixed programming of C/C + + matlab

Source: Internet
Author: User

Matlab has a wealth of functions, simple programming. However, in some cases, the MATLAB program is slow to execute. C + + compiler executes the program faster, more difficult to program than MATLAB. Therefore, there is a solution, is to use MATLAB to implement our experimental program, in C + + to achieve a more time-consuming part of the MATLAB program, from the MATLAB program to call C + + program to achieve acceleration.

Visual C + + 2015

1 : Configuring the Environment

1.1: In the VC + + directory

Include directory: (1): The directory where the generated mydll.h is located.

(2): the Include directory within MATLAB.

Include files

D:\Program Files\matlab\r2012a\extern\include

Library directory: (1): directory where the Mydll.lib is located.

(2): The lib directory of Matlab.

Library files

D:\New project\ handwritten numeral recognition \qpprj\distrib

D:\Program Files\matlab\r2012a\extern\lib\win64\microsoft

1.2: Additional Dependencies in connector-"input-"

Mclmcrrt.lib

Libmx.lib

Libmat.lib

Mclmcr.lib

QpPrj.lib

put the compiled DLL Copy to VC Debug of the project or Release directory to make the DLL can be found. and copy the compiled QpPrj.h file into the VC project.

Code implementation:

#include <iostream> #pragma comment (lib, "QpPrj.lib") #include "QpPrj.h" #include "mclmcr.h" #include "matrix.h" #i Nclude "Mclcppclass.h" using namespace Std;int main (int argc, char* argv[]) {//Initialize if (! Qpprjinitialize ()) {printf ("Could not initialize!"); return-1;} 1. Call Myadddouble A = 6;double B = 9;double c;//Allocate memory space for the variable Mwarray mwA (1, 1, mxdouble_class); The size of the matrix (all Maltab only one variable, that is, the matrix, in order to be in line with the CPP variable, set to 1*1 Matrix, Mxdouble_class represents the variable precision) Mwarray MwB (1, 1, mxdouble_class); Mwarray MwC (1, 1, mxdouble_class);//Set data, call the SetData function inside the class to assign a value Mwa.setdata (&a, 1); Mwb.setdata (&b, 1);//Usin  G My Add, drop my own Write function//Call example: extern lib_qpprj_cpp_api void Mw_call_conv myadd (int nargout, mwarray& C, const mwarray& A, const mwarray& B); Myadd (1, MwC, MwA, MwB);//Get data, call the Get function inside the class get function return value c = Mwc.get (1, 1);p rintf ("C is%f\n", c);//2. Call testchar//Exter n lib_qpprj_cpp_api void Mw_call_conv testchar (int nargout, mwarray& result, const mwarray& CHAR0) Double d = 4;dou Ble e;//allocates memory space for variables Mwarray mwinput (1, 1, mxdouble_class), Mwarray mwoutput (1, 1, mxdouble_class); Mwinput.setdata (&d, 1) ; Testchar (1, Mwoutput, mwinput); e = Mwoutput.get (1, 1);p rintf ("E is%f\n", e); char training_result_path[] = "D:\\new projec t\\ handwritten numeral recognition "; char digital_img_path[] =" d:\\hh\\t_4.jpg "; char training_result_file[] =" Training_result_200_trees "; Double f;cout << training_result_path << endl;cout << digital_img_path << endl;cout << Trai Ning_result_file << endl;//allocates memory space for variables Mwarray recognition_result (1, 1, mxdouble_class); Invocation example: extern lib_qpprj_cpp_api void Mw_call_conv digital_recogn_9 (int nargout, mwarray& recognition_result, const mwarray& Training_result_path, const mwarray& digital_img_path, const mwarray& training_result_file); Digital_recogn_9 (1, Recognition_result, Training_result_path, Digital_img_path, training_result_file); f = Recognition_result. Get (1, 1);//The program terminating the call Qpprjterminate ();//Terminate MCRMCLTERMINATEAPPLIcation (); return 0;} 

  

... Mwmcr::evaluatefunction error ...

Error using Predict (line 85)

Systems of UInt32 class cannot is used with the "predict" command. Convert the system to a identified model first, such as by using the "IDSS" command.

\

Matlab codeDIGITAL_RECOGN_9.M

function [Recognition_result]

= Digital_recogn_9 (Training_result_path, Digital_img_path, Training_result_file)

1 Training_result_path

Training Library path ' D:\New project\ handwritten digit recognition '

2 Digital_img_path

Incoming picture path ' D:\hh\t_4.jpg '

3 Training_result_file

Training library filename ' training_result_200_trees '

Invocation Example:

Digital_recogn_9 (' D:\New project\ handwritten numeral recognition ', ' D:\hh\t_4.jpg ', ' training_result_200_trees ')

function [Recognition_result] = Digital_recogn_9 (Training_result_path, Digital_img_path, Training_result_file)

Input_number = Imread (Digital_img_path);

%%%%%%%%%%%% Image Trsformation

Input_number = 255-rgb2gray (Input_number);

Threshold_noise = 35;

for II = 1:size (input_number,1)

for JJ = 1:size (input_number,2)

if input_number (II, JJ) < Threshold_noise

Input_number (ii, JJ) = 0;

End

End

End

%%%%%%%%%%%%%%%%%%%%%%%%

Projecting_width = SUM (input_number, 1);

Projecting_height = SUM (Input_number, 2);

%%%%%%% cut off the boundary

Boundary_width = zeros (Size (projecting_width));

Boundary_height = zeros (Size (projecting_height));

Offset_width = 3;

Offset_height = 3;

AA = Size (boundary_width, 2)-offset_width + 1;

Boundary_width (1, OFFSET_WIDTH:AA) = 1;

bb = Size (boundary_height, 1)-offset_height + 1;

Boundary_height (OFFSET_HEIGHT:BB, 1) = 1;

Projecting_width = projecting_width. * BOUNDARY_WIDTH;

Projecting_height = projecting_height. * Boundary_height;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Threshold_width = max (projecting_width)/Size (projecting_width, 2);

Threshold_height = max (projecting_height)/Size (projecting_height, 1);

S1 = (Projecting_width > Threshold_width);

New_width = SUM (S1);

S2 = (Projecting_height > Threshold_height);

new_height = SUM (s2);

trimming_img = Zeros (new_height, new_width);

trimming_img = Uint8 (trimming_img);

Counter_height = 1;

for Ii=1: (Size (Input_number, 1)-1)

%%%%%%% Select qualified rows

if S2 (ii, 1) ==true

Counter_height = Counter_height +1;

End

%%%%%%% Select qualified columns

Counter_width = 1;

for jj=1: (Size (Input_number, 2)-1)

If S1 (1, JJ) ==true

Counter_width = Counter_width +1;

End

%%%%% copy pixels to new image

if S2 (ii, 1) = = True | | S1 (1, JJ) = = True

S3 = Input_number (ii, JJ);

Trimming_img (counter_height, counter_width) = S3;

End

End

End

%%%%%%%%%% Flatten the image

edge_length = 16; percent 16*16=256

trimming_img = Imresize (trimming_img, [Edge_length, Edge_length]);

%% comment out temporarily

%%imshow (TRIMMING_IMG);

Flatten_img = Reshape (trimming_img, 1, 256);

%%%%%%%%%%%%%%%%%%%%%

CD (Training_result_path)

%% add semicolon, training library filename ' training_result_200_trees '

Training_result = Load (training_result_file);

Xtest = Training_result. Xtest;

MDL = TRAINING_RESULT.MDL;

%%%%%%%%%%%%%%%%%%%%%

Image_set = zeros (Size (Xtest));

Image_set = Uint8 (Image_set);

for ii=1:size (Xtest, 1)

Image_set (ii,:) = Flatten_img (:,:);

End

%%%% Train and Predict Using a single classification Tree

Xtest = double (image_set);

%%ypred = Predict (MDL, Xtest);

ypred = Predict (MDL, Xtest);

% Confmat_bag = Confusionmat (ytest,ypred);

Percent Recognition_result = ypred (n);

Recognition_result = ypred (a);

End

MYADD.M

function [C] = Myadd (A, b)

%untitled Summary of this function goes here

% detailed explanation goes here

c = a + B;

End

MYCHAR.M

function [Result] = MyChar (str)

%untitled2 Summary of this function goes here

% detailed explanation goes here

result = STR;

End

Mixed programming of C/C + + matlab

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.