手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

MATLAB中如何调用C语言程序

时间:2024-10-15 13:46:38

1、准备好C语言程序,一般情况下要清楚C语言的入口函数,比如,如下的C语言函数:ntuple_list linedetect(image_double image)

2、上述C语言函数中,linedetect为函数名,ntuple_list为输出,image为输入。C语言的源文件包含".h"和".c"文件。如:linedetect.h文件和linedetect.c文件。

3、编写mexfunction函数。mexfunction函数为C语言与MATLA幞洼踉残B语言的接口函数。调用实例在mylinedetect.c揍茏壅混文件中,文件内容如下:#include "mex.h"#include "matrix.h"#include "linedetect.h"#include "string.h"void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ int M; int N; double * pImg; ntuple_list p; struct image_double_s img_s; if(nrhs==0) { mexPrintf("error input\n"); return; } M = mxGetM(prhs[0]); N = mxGetN(prhs[0]); pImg = mxGetPr(prhs[0]); img_s.data = pImg; img_s.xsize = M; img_s.ysize = N; p=lsd(&img_s); plhs[1] = mxCreateDoubleMatrix(1,3,mxREAL); *(mxGetPr(plhs[1])+0) = p->size; *(mxGetPr(plhs[1])+1) = p->max_size; *(mxGetPr(plhs[1])+2) = p->dim; plhs[0] = mxCreateDoubleMatrix(p->dim,p->max_size,mxREAL); memcpy(mxGetPr(plhs[0]),p->values,(p->max_size)*(p->dim)*sizeof(double));}

4、在MATLAB中调用mex指令编译相关文件,将C语言编译为MEX文件,如下所示。mex mylinedetect.c linedetect.c编译完成后,生成mylinedetect.mexw32或mylinedetect.mexw64文件,此文件即mex文件,用于MATLAB与C语言接口函数

5、编译完成之后,编写MATLAB函数,调用MEX文件。如下所示。load trees;%以MEX文件的形式调用编译完成的C语言函数[o1,o2]=mylinedetect(double(X).');......

6、输出结果,上述linedetect函数完成图像中直线检测功能,带入MATLAB中调用后,形成如下结果。

MATLAB中如何调用C语言程序
© 手抄报圈