歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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

 

庫目錄:(1):mydll.lib所在目錄。

       (2):matlab的lib目錄。

Library files

D:\New Project\手寫體數字識別\QpPrj\distrib

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

 

1.2:在連接器-》輸入-》附加依賴項 

mclmcrrt.lib

libmx.lib

libmat.lib

mclmcr.lib

QpPrj.lib

 

將編譯好的dll復制到VC工程的Debug或者Release目錄下,以使得dll可以被找到。還要把編譯生成的QpPrj.h文件拷貝到VC工程裡

 

代碼實現:

#include <iostream>
#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.

\

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 ;

 

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

 

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

 

%% 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

Ubuntu Server上安裝Matlab http://www.linuxidc.com/Linux/2014-09/106242.htm

Matlab與C/C++聯合編程之從Matlab調用C/C++代碼 http://www.linuxidc.com/Linux/2012-08/68148.htm

二分類SVM方法Matlab實現 http://www.linuxidc.com/Linux/2013-05/84050.htm

Matlab中的取整函數fix, floor, ceil與round http://www.linuxidc.com/Linux/2013-10/91161.htm

Matlab編譯cuda的.cu文件 http://www.linuxidc.com/Linux/2014-04/100675.htm

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

Matlab 坐標圖動畫,動態顯示數據 http://www.linuxidc.com/Linux/2016-03/129056.htm

Ubuntu 14.04安裝Matlab2012a過程 http://www.linuxidc.com/Linux/2015-12/126297.htm

Copyright © Linux教程網 All Rights Reserved