-
Notifications
You must be signed in to change notification settings - Fork 319
Home
The following instructions are meant to compile OpenCV specifically for use in
mexopencv
. OpenCV functionality that is not currently exposed or used by
mexopencv
are disabled (CUDA acceleration, OpenCL with UMat
, etc...).
If you intend to compile OpenCV for general use in your own C++ code, we
recommend following the instructions in the OpenCV documentation.
First thing is to obtain OpenCV v3.1.0 (the final version). You have a number of options to choose from:
-
download the official installer from SourceForge. This package contains both the source code and prebuilt binaries for Windows (for both x86 and x64 architectures, and select versions of Visual Studio). You should get the file
opencv-3.1.0.exe
. We need the contents of thesources
folder from this package.Note: this package doesn't include the extra
opencv_contrib
modules, so you should obtain it using one of the other options listed below. -
download the source tarballs directly from GitHub. This includes downloading these two:
- the
opencv
sources (tag3.1.0
): https://github.com/Itseez/opencv/archive/3.1.0.zip - the
opencv_contrib
sources (tag3.1.0
): https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
- the
-
clone the Git repositories from GitHub. Again there are two parts:
- the main
opencv
repository:git clone https://github.com/Itseez/opencv.git
- the extra
opencv_contrib
repository:git clone https://github.com/Itseez/opencv_contrib.git
Once you clone the repositories, you need to switch to the stable
3.1.0
tag in both (git checkout tags/3.1.0
).Note: You'll need to have access to
git
either from the command-line using msysgit or Cygwin, or using a graphical user interface like GFW, SourceTree, or SmartGit. - the main
Depending on which option you chose above, extract or move the files to some desired location. For example, set it so that you end up with these files in the following paths:
C:\OpenCV310\opencv\README.md
C:\OpenCV310\opencv_contrib\README.md
This step requires CMake and a supported C++ compiler (Visual Studio 2015 is used in the instructions below, but you could use any other version, though we recommend VS2010 at the least).
Note: Microsoft recently started offering a full and free version of Visual Studio for personal use: Visual Studio Community 2013 or 2015 (equivalent in functionality to the Professional Edition).
We begin by generating a VS solution to build the sources.
Start the cmake-gui.exe
tool, and follow these steps:
- set the source folder as
C:/OpenCV310/opencv
- set the destination folder as:
C:/OpenCV310/build
- press Configure, and choose
Visual Studio 14 2015 Win64
as compiler - under "BUILD" group, deselect the following:
-
BUILD_DOCS
,BUILD_PACKAGE
,BUILD_PERF_TESTS
,BUILD_TESTS
-
BUILD_opencv_apps
,BUILD_opencv_cuda*
,BUILD_opencv_python*
,BUILD_opencv_ts
- under "CPACK" group, deselect all
- under "OPENCV" group, set
-
OPENCV_EXTRA_MODULES_PATH
toC:/OpenCV310/opencv_contrib/modules
- under "WITH" group, deselect:
-
WITH_CUDA
,WITH_CUFFT
,WITH_EIGEN
,WITH_OPENCL*
,WITH_VTK
- press Configure again
- under the newly added "BUILD" group (displayed in red), deselect the following modules:
ccalib
cvv
hdf
matlab
sfm
structured_light
surface_matching
contrib_world
- press Configure yet another time
- press Generate
- close CMake
Next step is to actually compile OpenCV.
Open the created solution file in Visual Studio:
C:\OpenCV310\build\OpenCV.sln
, and do the following:
- switch configuration to "Release" mode (or "Debug" if you like)
- build the solution (this will take a few minutes)
- select the "INSTALL" project in the solution explorer, and build it
- close Visual Studio
The resulting folder should be: C:\OpenCV310\build\install
(size about 100MB).
Finally you need to add the bin
folder (containing the OpenCV DLLs) to the
PATH
environment variable. Following these instructions, the directory to
add is:
C:\OpenCV310\build\install\x64\vc14\bin
Go and download mexopencv
project (clone the git repo, or download ZIP file).
Say you saved it to have this path:
C:\OpenCV310\mexopencv\README.markdown
Next start MATLAB, and run the following:
>> cd('C:\OpenCV310\mexopencv')
>> addpath('C:\OpenCV310\mexopencv')
>> addpath('C:\OpenCV310\mexopencv\opencv_contrib')
>> mexopencv.make('opencv_path','C:\OpenCV310\build\install', 'opencv_contrib',true)
(It should take several minutes to finish compilation).
Assuming everything went well, you can finally verify the generated MEX-files by running this in MATLAB:
>> cv.getBuildInformation()
You should see something like:
Congratulation, now you have OpenCV working in MATLAB!
Help: Stack Overflow (MATLAB, OpenCV) | MATLAB Answers | OpenCV Answers
- Windows + MATLAB
- Windows + Octave
- Linux + MATLAB
- Linux + Octave
- macOS + MATLAB
- macOS + Octave
- Windows + MATLAB
- Linux + MATLAB
- macOS + MATLAB