Ubuntu 编译OpenCV详细教程

编译OpenCV差不多有50次了,我把我的经验写下来,给大家一个参考。安装CUDA可参考我另一篇博客:Ubuntu 安装CUDA。

一、下载地址

OpenCV:https://github.com/opencv/opencv/releases

OpenCV contrib: https://github.com/opencv/opencv_contrib/releases

  • OpenCV 3.2只支持CUDA 8.0,OpenCV 3.4支持CUDA 9.0、CUDA 8.0。Ubuntu 16.04自带的显卡驱动支持CUDA 8.0;Ubuntu 18.04自带的显卡驱动支持CUDA 9.0。
  • opencv_contrib的版本应和OpenCV的版本号相同。
  • 下载的时候下载“Source Code”,也就是源代码。

二、安装依赖

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为例

  • 解压“opencv-3.4.2.zip ”
  • 进入文件夹“opencv-3.4.2”
  • 把”opencv_contrib-3.4.2.zip“解压到文件夹“opencv-3.4.2”
  • 在目录“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。

执行上面的命令的时候,可能需要等待一段时间,因为还要下载一些东西。

  • cmake没有错误时
make -j 12 # 12要替换成你的CPU的核心数量
  • 安装
sudo make install

三、使用

新建目录Hello-OpenCV,并进入该目录

  • 编写项目的CMakeLists.txt
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})
  • main.cpp
#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编译成功了。

 

你可能感兴趣的:(OpenCV)