Linux matlab 混合,C/C++与Matlab混合编程

Matlab 拥有丰富的功能,编程简单。不过,有些情况下,Matlab程序的执行速度比较慢。C/C++编译执行的程序速度比较快,编程难度上比Matlab要高一些。因此存在一种方案,就是使用Matlab实现我们的实验程序,用C/C++来实现Matlab程序中比较耗时的部分,从Matlab程序中调用C/C++的程序以实现加速。

Visual C++ 2015

1:配置环境

1.1:在vc++目录中

包含目录:(1):生成的mydll.h所在目录。

(2):matlab 内的include目录。

Include files

D:\Program Files\MATLAB\R2012a\extern\include

9462ecee4741937737f64ab92e2c53ad.png

库目录:(1):mydll.lib所在目录。

(2):matlab的lib目录。

Library files

D:\New Project\手写体数字识别\QpPrj\distrib

D:\Program Files\MATLAB\R2012a\extern\lib\win64\microsoft

fd1b9214e90603ac12628aadc07ea7b4.png

1.2:在连接器-》输入-》附加依赖项

mclmcrrt.lib

libmx.lib

libmat.lib

mclmcr.lib

QpPrj.lib

a8f9346fec9c63c499a0cbd2c9bfcd08.png

将编译好的dll复制到VC工程的Debug或者Release目录下,以使得dll可以被找到。还要把编译生成的QpPrj.h文件拷贝到VC工程里

代码实现:

#include

#pragma comment(lib,"QpPrj.lib")

#include "QpPrj.h"

#include "mclmcr.h"

#include "matrix.h"

#include "mclcppclass.h"

using namespace std;

int main(int argc, char* argv[])

{

// 初始化

if (!QpPrjInitialize())

{

printf("Could not initialize !");

return -1;

}

// 1.调用MyAdd

double a = 6;

double b = 9;

double c;

// 为变量分配内存空间

mwArray mwA(1, 1, mxDOUBLE_CLASS); // 1,1表示矩阵的大小(所有maltab只有一种变量,就是矩阵,为了和Cpp变量接轨,设置成1*1的矩阵,mxDOUBLE_CLASS表示变量的精度)

mwArray mwB(1, 1, mxDOUBLE_CLASS);

mwArray mwC(1, 1, mxDOUBLE_CLASS);

// set data,调用类里面的SetData函数给类赋值

mwA.SetData(&a, 1);

mwB.SetData(&b, 1);

// using my add,掉我自己写的函数

// 调用示例: 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,调用类里面的Get函数获取取函数返回值

c = mwC.Get(1, 1);

printf("c is %f\n", c);

// 2.调用TestChar

// extern LIB_QpPrj_CPP_API void MW_CALL_CONV TestChar(int nargout, mwArray& result, const mwArray& char0)

double d = 4;

double e;

// 为变量分配内存空间

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);

printf("e is %f\n", e);

char training_result_path[] = "D:\\New Project\\手写体数字识别";

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 << training_result_file << endl;

// 为变量分配内存空间

mwArray recognition_result(1, 1, mxDOUBLE_CLASS);

// 调用示例: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);

// 终止调用的程序

QpPrjTerminate();

// terminate MCR

mclTerminateApplication();

return 0;

}

... MWMCR::EvaluateFunction error ...

Error using predict (line 85)

Systems of uint32 class cannot be used with the "predict" command. Convert the system to an identified model first, such as by using the "idss" command.

ff7174c653b57821f50846ad288f82ea.png\

matlab代码

digital_recogn_9.m

function  [recognition_result]

= digital_recogn_9( training_result_path , digital_img_path , training_result_file )

1 training_result_path

训练库路径  ' D:\New Project\手写体数字识别'

2 digital_img_path

传入的图片路径  ' D:\hh\t_4.jpg '

3 training_result_file

训练库文件名  ' training_result_200_trees '

调用示例:

digital_recogn_9('D:\New Project\手写体数字识别','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 ;

forii = 1:size(input_number,1)

forjj = 1:size(input_number,2)

ifinput_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 ;

forii=1 : (size( input_number , 1 ) - 1)

%%%%%%%  select qualified rows

ifs2( ii , 1 )==true

counter_height  = counter_height +1 ;

end

%%%%%%%  select qualified columns

counter_width =1 ;

forjj=1 : (size( input_number , 2 ) - 1)

ifs1( 1 , jj )==true

counter_width  = counter_width +1 ;

end

%%%%% copy pixels to new image

ifs2( 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 ;%% 16*16=256

trimming_img = imresize( trimming_img , [edge_length , edge_length] ) ;

%%暂时注释 掉

%%imshow( trimming_img ) ;

flatten_img = reshape( trimming_img , 1 , 256 ) ;

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

cd(  training_result_path )

%%加上分号,训练库文件名 ' 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  );

forii=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);

%% recognition_result = ypred(1,1);

recognition_result = ypred(1,1);

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

Linux Matlab服务器进一步改造成Application Server(应用程序服务器) http://www.linuxidc.com/Linux/2014-09/106340.htm

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值