【问题标题】:Linker Error (MEX could not find the OpenCV library)链接器错误(MEX 找不到 OpenCV 库)
【发布时间】:2014-01-02 11:11:57
【问题描述】:

我正在使用 MATLAB R2013a x64 和 Win7 x64。我将 OpenCV 2.4.7.2 安装到路径 (C:\opencv)。以前round 的编译错误已解决,但现在出现链接错误。

MEX 命令输出:

>> mex -LC:\opencv\build\x64\vc11\lib -IC:\opencv\build\include\opencv -lcv -lcxcore me_HaarDetectOpenCV.cpp

Warning: MEX could not find the library "cv" specified with -l option. 
         MEX looked for a file with one of the names: 
               cv.lib 
               libcv.lib 
         MEX looked for the library in the following directories: 
               D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\LIB\amd64 
               D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\ATLMFC\LIB\amd64 
               C:\Program Files (x86)\Windows Kits\8.0\lib\win8\um\x64 
               D:\Program Files\MATLAB\R2013a\extern\lib\win64 
               D:\Program Files\MATLAB\R2013a\extern\lib\win64\microsoft 
               C:\opencv\build\x64\vc11\lib 

Warning: MEX could not find the library "cxcore" specified with -l option. 
         MEX looked for a file with one of the names: 
               cxcore.lib 
               libcxcore.lib 
         MEX looked for the library in the following directories: 
               D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\LIB\amd64 
               D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\ATLMFC\LIB\amd64 
               C:\Program Files (x86)\Windows Kits\8.0\lib\win8\um\x64 
               D:\Program Files\MATLAB\R2013a\extern\lib\win64 
               D:\Program Files\MATLAB\R2013a\extern\lib\win64\microsoft 
               C:\opencv\build\x64\vc11\lib 

     Creating library C:\Users\AsUs\AppData\Local\Temp\mex_47j5SQ\templib.x and object C:\Users\AsUs\AppData\Local\Temp\mex_47j5SQ\templib.exp 
  me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvCreateImage referenced in function mexFunction 
  me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvReleaseImage referenced in function mexFunction 
  me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvCreateMemStorage referenced in function mexFunction 
  me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvReleaseMemStorage referenced in function mexFunction 
  me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvGetSeqElem referenced in function mexFunction 
  me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvLoad referenced in function mexFunction 
  me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvEqualizeHist referenced in function mexFunction 
  me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvReleaseHaarClassifierCascade referenced in function mexFunction 
  me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvHaarDetectObjects referenced in function mexFunction 
  me_HaarDetectOpenCV.mexw64 : fatal error LNK1120: 9 unresolved externals 

    D:\PROGRA~2\MATLAB\R2013A\BIN\MEX.PL: Error: Link of 'me_HaarDetectOpenCV.mexw64' failed. 

Error using mex (line 206)
Unable to complete successfully. 
>>

如何修复错误并编译(创建 MEX 文件me_HaarDetectOpenCV.mexw64)?


我正在编辑 me_HaarDetectOpenCV.Cpp 并修复错误。

#include "D:\Program Files\MATLAB\R2013a\extern\include\mex.h"
#include "C:\opencv\build\include\opencv\cv.h" 
#include "C:\opencv\build\include\opencv\highgui.h"
#include "C:\opencv\build\include\opencv\cxcore.h"

MEX 命令输出:

>> mex me_HaarDetectOpenCV.cpp
   Creating library C:\Users\AsUs\AppData\Local\Temp\mex_7BQNq1\templib.x and object C:\Users\AsUs\AppData\Local\Temp\mex_7BQNq1\templib.exp 
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvReleaseMemStorage referenced in function mexFunction 
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvReleaseHaarClassifierCascade referenced in function mexFunction 
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvReleaseImage referenced in function mexFunction 
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvGetSeqElem referenced in function mexFunction 
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvHaarDetectObjects referenced in function mexFunction 
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvEqualizeHist referenced in function mexFunction 
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvCreateMemStorage referenced in function mexFunction 
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvCreateImage referenced in function mexFunction 
me_HaarDetectOpenCV.obj : error LNK2019: unresolved external symbol cvLoad referenced in function mexFunction 
me_HaarDetectOpenCV.mexw64 : fatal error LNK1120: 9 unresolved externals 

  D:\PROGRA~2\MATLAB\R2013A\BIN\MEX.PL: Error: Link of 'me_HaarDetectOpenCV.mexw64' failed. 

Error using mex (line 206)
Unable to complete successfully. 

【问题讨论】:

  • 请将粘贴板上的任何内容复制到问题中 - 这意味着未来的读者不会依赖可能会损坏的链接。

标签: c++ c matlab opencv mex


【解决方案1】:

这取决于 OpenCV 的版本,但对于较新的版本,您将找不到 cxcore 库。在 2.4.7 中,opencv_core247d.lib 用于调试版本,opencv_core247.lib 用于发布版本。查看您正在使用的 OpenCV 版本的文档。听起来您使用的是相当旧的指令。

This tutorial 提供了在 Windows 中使用 OpenCV 构建应用程序的说明,并列出了您通常包含的库的名称。显然,Visual Studio IDE 指令不适用。

基于未解析的外部符号,您可能应该链接以下库:

  • opencv_core247
  • opencv_imgproc247
  • opencv_objdetect247
  • opencv_features2d247
  • opencv_highgui247
  • 其他人?

这可能太多了,但不会伤害。

【讨论】:

  • 我正在使用 opencv 2.4.7.2
  • 那么你会寻找 opencv_core247.lib 或 opencv_core247d.lib。
【解决方案2】:

正如我在之前的 cmets 中提到的,您的代码使用的是 OpenCV 的旧 C 接口。最好使用较新的 C++ 接口。

也许最好展示一个完整的例子。下面的代码使用基于 Haar 的cascade classifier 运行人脸检测。我通过删除一些参数解析从您的代码中稍微简化了它。

如果您想要在 MATLAB 中使用 OpenCV 的更简单的包装器,请考虑使用 mexopencv

faces.cpp

#include "mex.h"
#include <string>
#include "opencv2/opencv.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/imgproc/imgproc.hpp"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    // validate arguments
    if (nrhs < 2) {
        mexErrMsgTxt("Wrong number of input arguments.");
    }
    if (nlhs > 1) {
        mexErrMsgTxt("Too many output arguments.");
    }
    if (!mxIsChar(prhs[0]) || mxGetM(prhs[0])!=1) {
        mexErrMsgTxt("First argument must be a string.");
    }
    if (!mxIsUint8(prhs[1]) || mxGetNumberOfDimensions(prhs[0])!=2) {
        mexErrMsgTxt("Second argument must be a uint8 grayscale image.");
    }

    // get XML cascade file name
    char *xmlfile = mxArrayToString(prhs[0]);
    cv::CascadeClassifier cascade;
    if (!cascade.load(std::string(xmlfile))) {
        mexErrMsgTxt("Failed to load cascade classifier.");
    }
    mxFree(xmlfile);

    // get grayscale image
    mwSize nrows = mxGetM(prhs[1]);
    mwSize ncols = mxGetN(prhs[1]);
    uint8_T *data = reinterpret_cast<uint8_T*>(mxGetData(prhs[1]));

    // copy into an OpenCV mat (there are better ways to do this step!)
    cv::Mat img(nrows, ncols, CV_8UC1, cv::Scalar::all(0));
    for(mwIndex c=0; c<ncols; c++) {
        for(mwIndex r=0; r<nrows; r++) {
            img.at<char>(r,c) = data[r + nrows*c];
        }
    }

    // process image before detection
    cv::equalizeHist(img, img);

    // detect faces
    std::vector<cv::Rect> faces;
    cascade.detectMultiScale(img, faces, 1.1, 4, 0, cv::Size(30,30));

    // return rectangles found to MATLAB
    plhs[0] = mxCreateDoubleMatrix(4, faces.size(), mxREAL);
    double *out = mxGetPr(plhs[0]);
    for(mwIndex i=0; i<faces.size(); i++) {
        out[i+0] = static_cast<double>(faces[i].x);
        out[i+1] = static_cast<double>(faces[i].y);
        out[i+2] = static_cast<double>(faces[i].width);
        out[i+3] = static_cast<double>(faces[i].height);
    }
}

假设您已下载 OpenCV 2.4.7 并将其解压缩到 C:\OpenCV(下面有 sourcesbuild 子目录),运行以下命令编译代码:

mex -largeArrayDims -I'C:\OpenCV\build\include' -L'C:\OpenCV\build\x64\vc11\lib'
  -lopencv_core247 -lopencv_imgproc247 -lopencv_objdetect247 faces.cpp

(根据你的编译器调整上面的库路径,我用的是VS2012)

接下来我们在 MATLAB 中测试 MEX 函数:

% some grayscale face image
img = imread('http://www.ece.rice.edu/~wakin/images/lena512.bmp');

% detect face
rect = faces('./haarcascade_frontalface_alt2.xml', img);

% show result
imshow(img)
rectangle('Position',rect(:,1), 'LineWidth',4, 'EdgeColor','g')

【讨论】:

  • 我可以编译上面的代码但不能运行它。我需要安装 mexopencv 才能运行它吗?还是我的 matlab 找不到动态库?我的错误:“使用面孔时出错!无法加载级联分类器”(我使用的是 Ubuntu 12.04,以及手动编译的 OpenCV)
  • @mcExchange: 不,上面的例子只需要 OpenCV(尽管mexopencv 确实使这个过程更容易)。至于报错信息,需要指定包含预训练级联分类器'haarcascade_frontalface_alt2.xml'的XML文件的完整路径。你可以在C:\OpenCV\sources\data\haarcascades\*.xml找到那些(有用于检测面部、眼睛、耳朵、嘴巴、鼻子等的分类器。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多