最近需要读取视频,MATLAB居然不支持特定格式,徘徊在把代码改成python和配置MATLAB去调用opencv之间,最后选了后者。我也出现过失败,装了第二次。过程中一定要看仔细,如果遇到错误找找相同类型的帖子有没有说明。
条件:已经安装了MATLAB2018a,VS2017
MATLAB命令行中mex -setup可执行
下载Windows版本的cmake:
https://cmake.org/download/
下载了opencv3.4.1:
https://github.com/opencv/opencv/archive/3.4.1.zip
下载了opencv_contrib3.4.1:
https://github.com/opencv/opencv_contrib/archive/3.4.1.zip
下载mexopencv3.4.1
https://github.com/kyamagu/mexopencv
OpenCV路径设置:
D:\SoftWare\OpenCV3.4.1_contib
D:\SoftWare\OpenCV3.4.1
然后开始通过生成一个VS解决方案去搭建上述环境资源。通过cmake-gui.exe,按照如下步骤:
1)设置源文件路径
2)设置目标文件路径
后来为了简洁美观,有点小调整,文件目录及路径为:
选定源文件路径和生成文件路径:
3)点击Configure,选择vs编译器,中间出了点小插曲,卸载了一下cmake,然后重装了一下,在tools里面选择编译器
BUILD_DOCS
, BUILD_EXAMPLES
, BUILD_PACKAGE
, BUILD_PERF_TESTS
, BUILD_TESTS
, BUILD_JAVA
BUILD_opencv_apps
, BUILD_opencv_cuda*
, BUILD_opencv_cudev
, BUILD_opencv_js
, BUILD_opencv_java*
, BUILD_opencv_python*
, BUILD_opencv_ts
, BUILD_opencv_viz
, BUILD_opencv_world
opencv组中设置如下:
with组中,撤销选定如下内容:
WITH_CUDA
, WITH_CUFFT
, WITH_CUBLAS
, WITH_NVCUVID
, WITH_MATLAB
, WITH_VTK
添加环境变量
D:\SoftWare\dev\build\install\x64\vc15\bin
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
打开MATLAB
>> cd('D:\SoftWare\dev\mexopencv')
>> addpath('D:\SoftWare\dev\mexopencv')
>> addpath('D:\SoftWare\dev\mexopencv\opencv_contrib')
>> mexopencv.make('opencv_path','D:\SoftWare\dev\build\install','opencv_contrib',true)
如果报错:
错误使用 mex
MEX 参数 '-R2017b-largeArrayDims' 未知。
打开mexopencv中的make.m文件
找到如下内容:ctrl+r
% real/imaginary storage format for complex arrays
% if ~mexopencv.isOctave() && ~verLessThan('matlab', '9.4')
% % keep using the "separate complex storage", as opposed to the
% % "interleaved complex storage" introduced in R2018a
% % (see MX_HAS_INTERLEAVED_COMPLEX)
% mex_flags = ['-R2017b' mex_flags];
% end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------完成之后,输入cv.getBuildInformation()
若出现类似以下信息,恭喜,安装完毕!
General configuration for OpenCV 3.4.1 =====================================
Version control: unknown
Extra modules:
Location (extra): D:/SoftWare/dev/opencv_contrib/modules
Version control (extra): unknown
Platform:
Timestamp: 2019-05-05T04:38:34Z
Host: Windows 10.0.17134 AMD64
CMake: 3.13.4
CMake generator: Visual Studio 15 2017 Win64
CMake build tool: D:/SoftWare/VisualStudio2017/MSBuild/15.0/Bin/MSBuild.exe
MSVC: 1916
CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (3 files): + SSSE3 SSE4_1
SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (9 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
C/C++:
Built as dynamic libs?: YES
C++11: YES
C++ Compiler: D:/SoftWare/VisualStudio2017/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe (ver 19.16.27027.1)
C++ flags (Release): /DWIN32 /D_WINDOWS /W4 /GR /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP12 /MD /O2 /Ob2 /DNDEBUG
C++ flags (Debug): /DWIN32 /D_WINDOWS /W4 /GR /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP12 /MDd /Zi /Ob0 /Od /RTC1
C Compiler: D:/SoftWare/VisualStudio2017/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
C flags (Release): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /MP12 /MD /O2 /Ob2 /DNDEBUG
C flags (Debug): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /MP12 /MDd /Zi /Ob0 /Od /RTC1
Linker flags (Release): /machine:x64 /INCREMENTAL:NO
Linker flags (Debug): /machine:x64 /debug /INCREMENTAL
ccache: NO
Precompiled headers: YES
Extra dependencies:
3rdparty dependencies:
OpenCV modules:
To be built: aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann fuzzy hdf hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
Disabled: js world
Disabled by dependency: -
Unavailable: cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv dnn_modern freetype java matlab ovis python2 sfm viz
Applications: tests perf_tests apps
Documentation: NO
Non-free algorithms: YES
Windows RT support: NO
GUI:
Win32 UI: YES
Media I/O:
ZLib: build (ver 1.2.11)
JPEG: build (ver 90)
WEBP: build (ver encoder: 0x020e)
PNG: build (ver 1.6.34)
TIFF: build (ver 42 - 4.0.9)
JPEG 2000: build (ver 1.900.1)
OpenEXR: build (ver 1.7.1)
Video I/O:
Video for Windows: YES
DC1394: NO
FFMPEG: YES (prebuilt binaries)
avcodec: YES (ver 57.107.100)
avformat: YES (ver 57.83.100)
avutil: YES (ver 55.78.100)
swscale: YES (ver 4.8.100)
avresample: YES (ver 3.7.0)
GStreamer: NO
DirectShow: YES
Parallel framework: Concurrency
Trace: YES (with Intel ITT)
Other third-party libraries:
Intel IPP: 2017.0.3 [2017.0.3]
at: D:/SoftWare/dev/build/3rdparty/ippicv/ippicv_win
Intel IPP IW: sources (2017.0.3)
at: D:/SoftWare/dev/build/3rdparty/ippicv/ippiw_win
Lapack: NO
Eigen: NO
Custom HAL: NO
Protobuf: build (3.5.1)
OpenCL: YES (no extra features)
Include path: D:/SoftWare/dev/opencv/sources/3rdparty/include/opencl/1.2
Link libraries: Dynamic load
Python 3:
Interpreter: D:/SoftWare/ANACONDA/python.exe (ver 3.7.3)
Libraries: D:/SoftWare/ANACONDA/libs/python37.lib (ver 3.7.3)
numpy: D:/SoftWare/ANACONDA/lib/site-packages/numpy/core/include (ver 1.16.2)
packages path: D:/SoftWare/ANACONDA/Lib/site-packages
Python (for build): D:/SoftWare/ANACONDA/python.exe
Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO
Install to: D:/SoftWare/dev/build/install
-----------------------------------------------------------------
建议将如下代码写成.m文件,或者写到将要调用opencv的.m文件中,因为,在实际使用时,存在无法使用cv的现象
mex -setup
cd('D:\SoftWare\dev\mexopencv')
addpath('D:\SoftWare\dev\mexopencv')
addpath('D:\SoftWare\dev\mexopencv\opencv_contrib')
mexopencv.make('opencv_path','D:\SoftWare\dev\build\install', 'opencv_contrib',true)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
使用:具体参照mexopencv中的示例文件,查看MATLAB中对应的API
% mexopencv
% Version 3.4.1 (R2018a) 01-October-2018
%
%% opencv: Main Modules
%
% core: Core Functionality
% cv.borderInterpolate - Computes the source location of an extrapolated pixel
% cv.copyMakeBorder - Forms a border around an image
% cv.add - Calculates the per-element sum of two arrays or an array and a scalar
% cv.subtract - Calculates the per-element difference between two arrays or array and a scalar
% cv.multiply - Calculates the per-element scaled product of two arrays
% cv.divide - Performs per-element division of two arrays or a scalar by an array
% cv.addWeighted - Calculates the weighted sum of two arrays
% cv.convertScaleAbs - Scales, calculates absolute values, and converts the result to 8-bit
% cv.convertFp16 - Converts an array to half precision floating number
...........
% cv.weightedMedianFilter - Applies weighted median filter to an image
% cv.GradientPaillou - Applies Paillou filter to an image
% cv.GradientDeriche - Applies Deriche filter to an image
% cv.PeiLinNormalization - Calculates an affine transformation that normalize given image using Pei/Lin Normalization
% cv.ContourFitting - Contour Fitting algorithm using Fourier descriptors
% cv.RidgeDetectionFilter - Ridge Detection Filter
% cv.BrightEdges - Bright edges detector
% cv.niBlackThreshold - Performs thresholding on input images using Niblack's technique or some of the popular variations it inspired
% cv.thinning - Applies a binary blob thinning operation, to achieve a skeletization of the input image
% cv.anisotropicDiffusion - Performs anisotropic diffusion on an image
%
% xobjdetect: Extended Object Detection
% cv.WBDetector - WaldBoost detector - Object Detection using Boosted Features
%
% xphoto: Additional Photo Processing Algorithms
% cv.inpaint2 - The function implements different single-image inpainting algorithms
% cv.SimpleWB - Simple white balance algorithm
% cv.GrayworldWB - Gray-world white balance algorithm
% cv.LearningBasedWB - More sophisticated learning-based automatic white balance algorithm
% cv.applyChannelGains - Implements an efficient fixed-point approximation for applying channel gains, which is the last step of multiple white balance algorithms
% cv.dctDenoising - The function implements simple dct-based denoising
% cv.bm3dDenoising - Performs image denoising using the Block-Matching and 3D-filtering algorithm
%