本文以论文"Zhang Q, Shen X, Xu L, et al. Rolling guidance filter[C]//European Conference on Computer Vision. Springer International Publishing, 2014: 815-830."提供的C++程序为例(依赖OpenCV),编写matlab调用OpenCV程序,使其可以用matlab运行,而且保持C++程序的快速性。
其中参考了博文matlab调用C程序 和 Matlab与C++混合编程(依赖OpenCV)。但是这两篇博文有些地方讲的不是很清楚,在接口函数中需要将数据首先从matlab读入进C++,然后经过C++函数运行有输出后,再将输出读出给matlab,这个过程是最重要的。
首先我把OpenCV放在了F盘,每次修改完.cpp文件后,都要使用matlab编译修改才可以生效,如果没有OpenCV的话,直接运行mex mexRollingGuidanceFilter.cpp就可以,但是有OpenCV后还需要加上它才可以,具体如下(具体的关于OpenCV与Visual Stdio的初始化连接就不介绍)
mex mexRollingGuidanceFilter.cpp -IF:\opencv\build\include -LF:\opencv\build\x64\vc10\lib -lopencv_core249 -lopencv_imgproc249 -lopencv_highgui249
然后是编写matlab调用主体函数,接口程序中调用的数据就来自这个函数,这个函数的名称没有特别要求,但是函数内部调用的C++函数名称必须和.cpp名称相同,具体如下
% /***************************************************************/
% /* Function: filter
% *
% * Description: rolling guidance filter core code by using bilateral filter.
% *
% * input arguments:
% * img: input image. Accept CV_8U and CV_32F type. Accept any number of channels.
% * sigma_s: spatial sigma (default 3.0). Controlling the spatial weight of bilateral filter and also the filtering scale of rolling guidance filter.
% * sigma_r: range sigma (default 0.1). Controlling the range weight of bilateral filter.
% * iteration: the iteration number of rolling guidance (default 4).
% *