Ubuntu(x86)安裝OpenCV的方法
參考:https://www.cnblogs.com/arkenstone/p/6490017.html
1 安裝依賴
$ sudo apt-get install build-essential $ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev $ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev # 處理影象所需的包 $ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev $ sudo apt-get install libxvidcore-dev libx264-dev # 處理視訊所需的包 $ sudo apt-get install libatlas-base-dev gfortran # 優化opencv功能 $ sudo apt-get install ffmpeg
2.下載opencv3.2.0
這裡需要下載opencv和opencv_contrib(後者會在cmake配置的時候用到),這是因為opencv3以後SIFT和SURF之類的屬性被移到了contrib中
$ wget https://github.com/opencv/opencv/archive/3.2.0.zip # 從github上直接下載或者clone也可
$ wget https://github.com/opencv/opencv_contrib/archive/3.2.0.zip
3 編譯opencv
a.無NVIDIA CUDA版本
將上述opencv包解壓,然後cmake配置屬性
$ cd opencv-3.2.0 $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.2.0/modules -D PYTHON_EXCUTABLE=/usr/bin/python3 -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_GTK=ON -D WITH_OPENGL=ON -D BUILD_EXAMPLES=ON .. $ make -j24 $ sudo make install
b NVIDIA CUDA版本
注意有幾個地方要修改
CUDA_ARCH_BIN="5.3",注意這塊很重要,要找到自己對應顯示卡的值,根據官網來查詢,如果沒有設定對,會報計算力錯誤nvcc fatal : Unsupported gpu architecture 'compute_**'
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.2.0/modules -D PYTHON_EXCUTABLE=/usr/bin/python3 -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D CUDA_ARCH_BIN="5.3" -D CUDA_ARCH_PTX="" -D CUDA_FAST_MATH=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_GTK=ON -D WITH_OPENGL=ON -D BUILD_EXAMPLES=ON ..
如果在cmake過程中報downloading protobuf-cpp-3.1.tar.gz.的錯誤,則手動下載即可(下載路徑),下載完之後放在/opencv_contrib/modules/dnn/.download/bd5e3eed635a8d32e2b99658633815ef/v3.1.0路徑下,然後重新cmake即可。
編譯過程中又報新的錯誤,提示如下
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_nppi_LIBRARY (ADVANCED)
linked by target "opencv_cudev" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/cudev
linked by target "opencv_cudev" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/cudev
linked by target "opencv_test_cudev" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/cudev/test
linked by target "opencv_core" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/core
linked by target "opencv_core" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/core
linked by target "opencv_test_core" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/core
linked by target "opencv_perf_core" in directory /home/lthpc/workspace_zong/opencv3.2/opencv-3.2.0/modules/core
……
查詢資料後,是cuda的原因,解決方法如下:
先找到FindCUDA.cmake檔案,路徑是在/opencv-3.2.0/cmake/FindCUDA.cmke
a.開啟找到行
find_cuda_helper_libs(nppi)
修改為
find_cuda_helper_libs(nppial)
find_cuda_helper_libs(nppicc)
find_cuda_helper_libs(nppicom)
find_cuda_helper_libs(nppidei)
find_cuda_helper_libs(nppif)
find_cuda_helper_libs(nppig)
find_cuda_helper_libs(nppim)
find_cuda_helper_libs(nppist)
find_cuda_helper_libs(nppisu)
find_cuda_helper_libs(nppitc)
b.找到行
set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")
修改為
set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")
c.找到行
unset(CUDA_nppi_LIBRARY CACHE)
修改為
unset(CUDA_nppial_LIBRARY CACHE)
unset(CUDA_nppicc_LIBRARY CACHE)
unset(CUDA_nppicom_LIBRARY CACHE)
unset(CUDA_nppidei_LIBRARY CACHE)
unset(CUDA_nppif_LIBRARY CACHE)
unset(CUDA_nppig_LIBRARY CACHE)
unset(CUDA_nppim_LIBRARY CACHE)
unset(CUDA_nppist_LIBRARY CACHE)
unset(CUDA_nppisu_LIBRARY CACHE)
unset(CUDA_nppitc_LIBRARY CACHE)
cuda9中有一個單獨的halffloat(cuda_fp16.h)標頭檔案,也應該被包括在opencv的目錄裡
將標頭檔案cuda_fp16.h新增至 opencv\modules\cudev\include\opencv2\cudev\common.hpp,即在common.hpp中新增
#include <cuda_fp16.h>
然後重新生成即可,執行成功後會提示如下
-- Configuring done
-- Generating done
配置完之後make
$ make -j24
$ sudo make install
安裝完之後,執行python,匯入cv2,檢視是否成功即可