安装
如果可以安装最新版的opencv2 or opencv3,可以选择更简单的安装方式:
homebrew安装 opencv完整版
首先下载源代码source
下载地址
选择目标版本
(本文以3.3.1版本为例)
解压之后,进入解压后的文件夹
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
可以选择5个job并行(当然可以调整为更多job,或者不设置默认为一个job)
make -j5
然而我在安装3.3.1版本时遇到了问题:
/home/coie/Softwares/Libraries/opencv-3.3.1/modules/highgui/src/cap_ffmpeg_impl.hpp:1484:21: error: ‘CODEC_FLAG_GLOBAL_HEADER’ was not declared in this scope
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
^
/home/coie/Softwares/Libraries/opencv-3.3.1/modules/highgui/src/cap_ffmpeg_impl.hpp: In function ‘int icv_av_write_frame_FFMPEG(AVFormatContext*, AVStream*, uint8_t*, uint32_t, AVFrame*)’:
/home/coie/Softwares/Libraries/opencv-3.3.1/modules/highgui/src/cap_ffmpeg_impl.hpp:1512:30: error: ‘AVFMT_RAWPICTURE’ was not declared in this scope
if (oc->oformat->flags & AVFMT_RAWPICTURE) {
^
/home/coie/Softwares/Libraries/opencv-3.3.1/modules/highgui/src/cap_ffmpeg_impl.hpp: In member function ‘void CvVideoWriter_FFMPEG::close()’:
/home/coie/Softwares/Libraries/opencv-3.3.1/modules/highgui/src/cap_ffmpeg_impl.hpp:1686:35: error: ‘AVFMT_RAWPICTURE’ was not declared in this scope
if( (oc->oformat->flags & AVFMT_RAWPICTURE) == 0 )
^
/home/coie/Softwares/Libraries/opencv-3.3.1/modules/highgui/src/cap_ffmpeg_impl.hpp: In member function ‘bool CvVideoWriter_FFMPEG::open(const char*, int, double, int, int, bool)’:
/home/coie/Softwares/Libraries/opencv-3.3.1/modules/highgui/src/cap_ffmpeg_impl.hpp:1920:32: error: ‘AVFMT_RAWPICTURE’ was not declared in this scope
if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
^
In file included from /home/coie/Softwares/Libraries/opencv-3.3.1/modules/highgui/src/cap_ffmpeg.cpp:45:0:
/home/coie/Softwares/Libraries/opencv-3.3.1/modules/highgui/src/cap_ffmpeg_impl.hpp: In static member function ‘static AVStream* OutputMediaStream_FFMPEG::addVideoStream(AVFormatContext*, AVCodecID, int, int, int, double, AVPixelFormat)’:
/home/coie/Softwares/Libraries/opencv-3.3.1/modules/highgui/src/cap_ffmpeg_impl.hpp:2214:25: error: ‘CODEC_FLAG_GLOBAL_HEADER’ was not declared in this scope
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
^
modules/highgui/CMakeFiles/opencv_highgui.dir/build.make:230: recipe for target 'modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.cpp.o' failed
make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.cpp.o] Error 1
CMakeFiles/Makefile2:2349: recipe for target 'modules/highgui/CMakeFiles/opencv_highgui.dir/all' failed
解决方法:
找到刚刚下载的路径下,opencv-3.3.0/modules/videoio/src/cap_ffmpeg_impl.hpp
文件,在文件开头加上
#define AV_CODEC_FLAG_GLOBAL_HEADER (1 << 22)
#define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER
#define AVFMT_RAWPICTURE 0x0020
然后重新执行
make -j5
安装library
sudo make install
安装成功
配置环境变量
open ~/.bashrc
文件末尾添加
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
保存文件,然后刷新
source ~/.bashrc
当然,现在我们重启系统之后需要刷新一次.bashrc文件,如果不想每次都刷新,可以打开.bash_profile文件
open ~/.bash_profile
最末尾上加入如下语句:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
保存文件,然后刷新
source ~/.bash_profile
完成!
后续
没有想到运行时还是有bug
ld: library not found for -llibtbb.dylib
解决办法,打开文件
vim /usr/local/lib/pkgconfig/opencv.pc
将 Libs
这一项里所有的-llib某某某.dylib
修改为 /usr/local/lib/lib某某某.dylib
也就是
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.3.1
Libs: -L${exec_prefix}/lib -llibopencv_dnn.3.3.1.dylib -llibopencv_ml.3.3.1.dylib -llibopencv_objdetect.3.3.1.dylib -llibopencv_shape.3.3.1.dylib -llibopencv_stitching.3.3.1.dylib -llibopencv_superres.3.3.1.dylib -llibopencv_videostab.3.3.1.dylib -llibopencv_calib3d.3.3.1.dylib -llibopencv_features2d.3.3.1.dylib -llibopencv_highgui.3.3.1.dylib -llibopencv_videoio.3.3.1.dylib -llibopencv_imgcodecs.3.3.1.dylib -llibopencv_video.3.3.1.dylib -llibopencv_photo.3.3.1.dylib -llibopencv_imgproc.3.3.1.dylib -llibopencv_flann.3.3.1.dylib -llibopencv_core.3.3.1.dylib
Libs.private:
Cflags: -I${includedir_old} -I${includedir_new}
修改为
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.3.1
Libs: -L${exec_prefix}/lib /usr/local/lib/libopencv_dnn.3.3.1.dylib /usr/local/lib/libopencv_ml.3.3.1.dylib /usr/local/lib/libopencv_objdetect.3.3.1.dylib /usr/local/lib/libopencv_shape.3.3.1.dylib /usr/local/lib/libopencv_stitching.3.3.1.dylib /usr/local/lib/libopencv_superres.3.3.1.dylib /usr/local/lib/libopencv_videostab.3.3.1.dylib /usr/local/lib/libopencv_calib3d.3.3.1.dylib /usr/local/lib/libopencv_features2d.3.3.1.dylib /usr/local/lib/libopencv_highgui.3.3.1.dylib /usr/local/lib/libopencv_videoio.3.3.1.dylib /usr/local/lib/libopencv_imgcodecs.3.3.1.dylib /usr/local/lib/libopencv_video.3.3.1.dylib /usr/local/lib/libopencv_photo.3.3.1.dylib /usr/local/lib/libopencv_imgproc.3.3.1.dylib /usr/local/lib/libopencv_flann.3.3.1.dylib /usr/local/lib/libopencv_core.3.3.1.dylib
Libs.private:
Cflags: -I${includedir_old} -I${includedir_new}
然后就可以啦!
Reference
opencv
error-in-building-opencv-with-ffmpeg
ld-library-not-found-for-llibtbb-dylib