编译OpenCV差不多有50次了,我把我的经验写下来,给大家一个参考。安装CUDA可参考我另一篇博客:Ubuntu 安装CUDA。
OpenCV:https://github.com/opencv/opencv/releases
OpenCV contrib: https://github.com/opencv/opencv_contrib/releases
Ubuntu 16.04
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev libgtk-3-dev libgtkglext1-dev libgstreamer1.0-dev libavresample-dev libgphoto2-dev libopenblas-dev libatlas-dev doxygen libgstreamer-ocaml-dev libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-base0.10-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-good1.0-dev libgstreamer0.10-dev libgstreamer1.0-dev libgstreamermm-1.0-dev tesseract-ocr-dev liblept5 libleptonica-dev liblapacke-dev python3-numpy libv4l-dev python3-numpy python3-pip python-pip libprotobuf-dev libhdf5-dev libgflags-dev
Ubuntu 18.04
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev libgtk-3-dev libgtkglext1-dev libgstreamer1.0-dev libavresample-dev libgphoto2-dev libopenblas-dev libatlas-base-dev doxygen libgstreamer-ocaml-dev libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-good1.0-dev libgstreamer1.0-dev libgstreamermm-1.0-dev libtesseract-dev liblept5 libleptonica-dev liblapacke-dev libgstreamer-opencv1.0-0 libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-good1.0-dev libgstreamerd-3-dev libgstreamermm-1.0-dev libv4l-dev libprotobuf-dev libhdf5-dev libgflags-dev python3-numpy
以编译OpenCV 3.4.2为例
mkdir release #新建文件夹
cd release #进入文件夹
不需要CUDA支持
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv342 \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.4.2/modules \
-D WITH_LIBV4L=ON \
-D WITH_CUDA=OFF \
-D WITH_TBB=ON \
-D WITH_OPENMP=ON \
-D WITH_OPENGL=ON ..
需要CUDA支持
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv342 \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.4.2/modules \
-D WITH_LIBV4L=ON \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=ON \
-D WITH_NVCUVID=ON \
-D CUDA_GENERATION=Auto \
-D WITH_TBB=ON \
-D WITH_OPENMP=ON \
-D WITH_OPENGL=ON ..
CMAKE_BUILD_TYPE指定编译类型,可选项为DEBUG和RELEASE;CMAKE_INSTALL_PREFIX指定库的安装路径;OPENCV_EXTRA_MODULES_PATH指定opencv_contrib里面的modules文件夹的路径;WITH_CUDA是是否启用CUDA支持,可选项为ON和OFF。
执行上面的命令的时候,可能需要等待一段时间,因为还要下载一些东西。
make -j 12 # 12要替换成你的CPU的核心数量
sudo make install
新建目录Hello-OpenCV,并进入该目录
cmake_minimum_required(VERSION 3.0)
project(OCSample)
set(CUDA_USE_STATIC_CUDA_RUNTIME ON) #这一句解决 cannot find -lopencv_dep_cudart
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
set(OpenCV_DIR "/usr/local/opencv343-cuda90/share/OpenCV") # 指定OpenCV安装路径来区分不同的OpenCV版本
find_package(OpenCV REQUIRED)
set(OpenCV_LIB_DIR ${OpenCV_INSTALL_PATH}/lib)
message(STATUS "OpenCV版本: ${OpenCV_VERSION}")
message(STATUS " 头文件目录:${OpenCV_INCLUDE_DIRS}")
message(STATUS " 库文件目录:${OpenCV_LIB_DIR}")
message(STATUS " 库文件列表:${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIR})
CUDA_ADD_EXECUTABLE(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})
#include
int main(int argc,char **argv){
cv::Mat src(300,500,CV_8UC3,cv::Scalar::all(255));
cv::imshow("hello-opencv",src);
cv::waitKey(0);
}
然后打开终端,输入
cmake .
make
本目录下就会生成一个可执行文件“Hello-OpenCV”,双击之,就出现一个纯白的界面,说明你的OpenCV编译成功了。