概述
内容参考
- OpenCV静态库编译与链接
零、预置条件
Required Packages
- GCC 4.4.x or later
- CMake 2.8.7 or higher
- Git
- GTK+2.x or higher, including headers (libgtk2.0-dev)
- pkg-config
- Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
- ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, - - libswscale-dev
- [optional] libtbb2 libtbb-dev
- [optional] libdc1394 2.x
- [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
- [optional] CUDA Toolkit 6.5 or higher
The packages can be installed using a terminal and the following commands or by using Synaptic Manager:
[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
官方说明:
https://docs.opencv.org/4.2.0/d7/d9f/tutorial_linux_install.html
3rdparty GitHub 地址:
https://github.com/opencv/opencv_3rdparty/branches/all
一、编译库
- 下载源码解压,并添加
static_release
文件夹 - 进入
static_release
文件夹,执行命令:
sudo cmake .. -D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=/usr/local
-D BUILD_JASPER=ON
-D BUILD_JAVA=OFF
-D BUILD_JPEG=ON
-D BUILD_PERF_TESTS=OFF
-D BUILD_PNG=ON
-D BUILD_PROTOBUF=ON
-D BUILD_SHARED_LIBS=NO
-D BUILD_TESTS=OFF
-D BUILD_TIFF=ON
-D BUILD_ZLIB=ON
-D BUILD_WEBP=ON
-D BUILD_opencv_apps=OFF
-D BUILD_opencv_core=ON
-D BUILD_opencv_calib3d=ON
-D BUILD_opencv_dnn=ON
-D BUILD_opencv_features2d=ON
-D BUILD_opencv_flann=ON
-D BUILD_opencv_gapi=OFF
-D BUILD_opencv_highgui=ON
-D BUILD_opencv_imgcodecs=ON
-D BUILD_opencv_imgproc=ON
-D BUILD_opencv_java_bindings_generator=OFF
-D BUILD_opencv_js=OFF
-D BUILD_opencv_ml=ON
-D BUILD_opencv_objdetect=OFF
-D BUILD_opencv_photo=OFF
-D BUILD_opencv_python2=OFF
-D BUILD_opencv_python3=OFF
-D BUILD_opencv_python_bindings_generator=OFF
-D BUILD_opencv_stitching=OFF
-D BUILD_opencv_ts=OFF
-D BUILD_opencv_video=OFF
-D BUILD_opencv_videoio=OFF
-D WITH_GTK=OFF
-D WITH_GTK_2_X=OFF
-D WITH_LAPACK=OFF
opencv依赖第三方的库,所以当把opencv编译成静态库的时候,注意要把它运行时必要的依赖库也编译进来,opencv源码内有自己的3rdparty,主要有:libjpeg、libpng、libtiff、libzlib、libwebp、libprotobuf、ffmpeg(用到video时编译)、libgtk(用于显示界面显示,服务器部署不会用到),可以通过cmake -LA来查看配置的编译信息,再根据信息考虑把opencv的哪些module编译进来。
可能用到的其它库
sudo apt-get -y install libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264v4l-utils unzip
sudo apt-get install libgtk-3-dev
sudo apt-get -y install libavresample-dev
sudo apt-get -y install libgphoto2-dev
sudo apt-get install libopenblas-dev
sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install python3-dev python3-pip python3-tk python3-lxml python3-six
- 执行
sudo make -j4
和sudo make install
编译期间遇到的问题(处理方法参考下面链接)
1、第三方依赖无法下载成功的问题
简单操作就是去看 CMakeDownloadLog.txt 下的错误提示,然后去修改。
【OpenCV】编译OpenCV时,FFmpeg或ippicv下载不成功的解决方案,以及其它可能下载不成功问题的解决方案
二、使用库
链接时指定opencv的静态库以及依赖的库。另外需要注意头文件的问题,通过CMakeLists把opencv加入项目编译时,由于opencv编译完成后要install才会把所用的头文件集中拷贝到一个地方,所以事先将头文件拷贝到了项目的opencv目录下。
set(opencv_libs
#项目用到的库
opencv_core
opencv_dnn
opencv_imgproc
opencv_imgcodecs
opencv_ml
opencv_calib3d
opencv_highgui
zlib
libjasper
libjpeg-turbo
libpng
libtiff
IlmImf
libprotobuf
libwebp)
#编译opencv
add_subdirectory(${CMAKE_SOURCE_DIR}/3rdparty/opencv-3.14)
#增加头文件路径
include_directories(${CMAKE_SOURCE_DIR}/3rdparty/opencv-3.14/include)
#链接opencv静态库
target_link_libraries(demo ${opencv_libs} -lrt -lpthread)
最后
以上就是矮小小笼包为你收集整理的OpenCV4.2.0 在Deepin15下编译静态库的全部内容,希望文章能够帮你解决OpenCV4.2.0 在Deepin15下编译静态库所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复