matlab opencv混合编程(“mex找不到指定模块”)

使用mex 可以将c/cpp程序变成matlab的接口函数,提升效率,通用性的配置方式这里不再累述,主要介绍使用opencv库的混合编程





在matlab下mex -setup选择编译器


#include <iostream>
#include <string>

#include <opencv/cv.h>
#include <opencv/highgui.h>

#include "mex.h"

// Matlab entry point function
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
    // Check if the input argument is legal
    if ( nrhs != 1 || !mxIsChar( prhs[0] ) )
        mexErrMsgTxt("An image name should be given.\n");

    // Get the name of the image
    int nStringLen;
    nStringLen = mxGetNumberOfElements(prhs[0]);
    std::string szImageName;
    szImageName.resize( nStringLen + 1 );

    mxGetString( prhs[0], &szImageName[0], nStringLen + 1 );

    // Read the image from file
    cv::Mat image;
    image = cv::imread( szImageName );

    // Show the image if it is successfully read from disk
    if ( !image.empty() )
        cv::imshow( "Test Mex OpenCV", image );
        mexErrMsgTxt("The specified image does not exist.\n");

使用mex编译 需要制定opencv头文件、静态库文件的目录,已经要链接的静态库的名称,注意opencv的版本号

mex OpenCVShowImage.cpp -ID:\opencv\build\include 
-LD:\opencv\build\x64\vc10\lib -lopencv_core243 -lopencv_imgproc243 -lopencv_highgui243





If MATLAB® cannot find all .dll files referenced by a MEX file, it
cannot load the MEX file. MATLAB displays the following error message:

Invalid MEX-file : The specified module could not be
found. where mexfilename is the module with the dependency error. This
module cannot find its dependent libraries. To resolve this error,
find the names of the dependent libraries, and determine if they are
present on your system and on the system path.

On Windows® systems, to find library dependencies, use the third-party
product Dependency Walker. Dependency Walker is a free utility that
scans any 32-bit or 64-bit Windows module and builds a hierarchical
tree diagram of all dependent modules. For each module found, it lists
all the functions exported by that module, and which of those
functions are called by other modules. Download the Dependency Walker
utility from the website See
for information on using the Dependency Walker.

For .dll files that the MEX file linked against when it was built, the
.dll files must be on the system path or in the same folder as the MEX

MEX files might require additional libraries that are not linked to
the MEX file. Failure to find one of these explicitly loaded libraries
might not prevent a MEX file from loading, but prevents it from
working correctly. The search path used to find these explicitly
loaded libraries is controlled by the code that loads the libraries
and might not include the folder that contains the MEX file. Consult
the library documentation on proper installation locations.

Possible reasons for failure include:

MATLAB version incompatibility Missing compiler runtime libraries. If
your system does not have the same compiler that built the MEX file,
see the Microsoft® MSDN® website for information about Visual C++®
Redistributable Packages. Missing or incorrectly installed specialized
runtime libraries. Contact your MEX file or library vendor.

个人分类: 杂七杂八
想对作者说点什么? 我来说一句