Matlab编译MEX文件遇OpenCV头文件缺失及加载失败求助
Alright, let's tackle these two linked issues you're facing with MATLAB's mex and OpenCV. I've dealt with similar problems on macOS before, so here's a step-by-step breakdown:
Fixing the Compilation Error (Missing 'opencv2/opencv.hpp')
First off, the Python 3 OpenCV you installed won't help MATLAB's mex compiler—they're completely separate environments. MATLAB doesn't automatically pick up third-party libraries like OpenCV, so you need to explicitly tell the mex command where to find OpenCV's headers and libraries.
Find your OpenCV 2.4 installation path
The error mentionslibopencv_core.2.4.dylib, so you need the 2.4 version of OpenCV (not the newer 3.x/4.x that's likely bundled with your Python). If you installed it via Homebrew, it's probably under/usr/local/opt/opencv@2(Homebrew keeps older versions with the@versionsuffix). To confirm, run this in Terminal:find /usr/local -name "opencv2/opencv.hpp" | grep 2.4This will give you the path to the include directory (e.g.,
/usr/local/opt/opencv@2/include).Compile with explicit paths
Run this mex command in MATLAB, replacing the paths with the ones you found:mex mexResize.cpp -I/usr/local/opt/opencv@2/include -L/usr/local/opt/opencv@2/lib -lopencv_core -lopencv_imgproc -lopencv_highgui-Ipoints to the folder containingopencv2/(the headers)-Lpoints to the folder with the.dyliblibraries-llinks the specific OpenCV libraries your code needs (core, imgproc, highgui are common for resizing tasks)
Fixing the Runtime dlopen Error (Missing libopencv_core.2.4.dylib)
Even if compilation succeeds, MATLAB might still fail to find the library at runtime because it doesn't check Homebrew's opt directories by default.
Verify the library exists
In Terminal, check if the library is where the error says it should be:ls /usr/local/opt/opencv/lib/libopencv_core.2.4.dylibIf this returns "No such file or directory", you probably don't have OpenCV 2.4 installed. Install it via Homebrew:
brew install opencv@2Homebrew will give you instructions to add its bin directory to your PATH—follow those to make sure the library is accessible system-wide.
Tell MATLAB where to find the library
You have two options here:- Temporary fix (per MATLAB session): Run these commands in MATLAB:
setenv('DYLD_LIBRARY_PATH', [getenv('DYLD_LIBRARY_PATH') ':/usr/local/opt/opencv@2/lib']) addpath('/usr/local/opt/opencv@2/lib') - Permanent fix (auto-load on MATLAB startup): Create or edit a
startup.mfile in your MATLAB user directory (runuserpathin MATLAB to find this folder) and add the above two lines to it.
- Temporary fix (per MATLAB session): Run these commands in MATLAB:
Fix broken library references (if needed)
Sometimes the mex file might be pointing to the wrong path for the library. To check, run this in Terminal (navigate to your mex file's directory first):otool -L mexResize.mexmaci64If you see
libopencv_core.2.4.dylibpointing to a non-existent path, useinstall_name_toolto update it:install_name_tool -change /usr/local/opt/opencv/lib/libopencv_core.2.4.dylib /usr/local/opt/opencv@2/lib/libopencv_core.2.4.dylib mexResize.mexmaci64Repeat this for any other missing libraries listed in the error.
Quick Notes for Your Environment
- MATLAB R2017b works fine with Xcode 9.2—you don't need to update either for this fix.
- Ignore your Python 3 OpenCV installation for this task; it's unrelated to MATLAB's mex compiler.
内容的提问来源于stack exchange,提问作者Wes

