Ubuntu18.04下安裝openCV4.1.2
注意:
在整個安裝程序中,包括后期QT的使用中不要出現中文路徑,會很坑的,
一、下載openCV4.1.2
可以再官網進行下載https://opencv.org/releases/->選擇Sources下載原始碼然后進行編譯安裝
也可以在https://github.com/opencv/opencv/tree/4.1.2下載原始碼進行編譯安裝
也可以在百度云盤下載安裝https://pan.baidu.com/s/1LIqIvyA6BjjAy1pkC-Mg0w,
二、更新Ubuntu18.04apt源為阿里源(如果已更新或者其他源可以自己嘗試),
// 1.備份/etc/apt/sources.list
1 cp /etc/apt/sources.list /etc/apt/sources.list.bak// 2.打開/etc/apt/sources.list
1 sudo vim /etc/apt/sources.list// 3.洗掉檔案內所有內容
// 4.將一下內容復制進/etc/apt/sources.list檔案中
1 deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse 2 deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse 3 deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse 4 deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse 5 deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse 6 deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse 7 deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse 8 deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse 9 deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse10 deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
// 5.更新源
1 sudo apt-get update2 sudo apt-get upgrade
三、解決ubuntu1804無法安裝libjasper-dev問題(速度會非常的慢,一定要耐心等待)
// 1.檢測是否有問題
1 apt-get install libjasper-dev
如果有報錯資訊:errorE: unable to locate libjasper-dev則繼續解決一下問題,沒有則可跳過第三步,
// 2.解決方案
1 sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"2 sudo apt update3 sudo apt install libjasper1 libjasper-dev
添加一個源來重新安裝,其中,libjasper1是新的依賴項,
但是上述命令會可能產生新的問題,無法找到add-apt-repository問題
// 3.解決add-apt-repository問題
1 apt-get -y install software-properties-common2 apt-get -y install apt-file3 apt-get update
四、添加openCV安裝的相關準備
//1.安裝cmake(如果已經安裝過可以忽略)
1 sudo apt-get install cmake
// 2.安裝openCV相關依賴(如果剛才第三步失敗或者跳過的話可能 libjasper-dev 會報錯)
1 sudo apt-get install build-essential libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
事實上,OpenCV的依賴項眾多,在cmake階段檢查依賴項是否安裝,調整自己的功能,因此,可根據專案需要調整安裝依賴項,
五、安裝openCV
//1.將下載的opencv-4.1.2.zip拷貝到主目錄下,并解壓縮

// 2.打開終端并切換至主目錄下的openCV-4.1.2
1 cd /home/DepZcb/openCV-4.1.2
// 3.在openCV-4.1.2下創建一個名為build目錄,并切換到這個目錄下
1 mkdir build2 cd ./build
// 4.執行cmake,生成makefile檔案
1 cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..命令說明:
OpenCV4默認不生成.pc檔案,OPENCV_GENERATE_PKGCONFIG=ON才會生成,該編譯選項開啟生成opencv4.pc檔案,支持pkg-config功能,
可用-D CMAKE_INSTALL_PREFIX指定安裝目錄,如果指定安裝目錄則默認各部分分別安裝在/usr/local/目錄的include/ bin/ lib/3個檔案夾下.
.. 為上一級目錄,
// 5.執行makefile檔案(有些人說上一步不會生成makefile,我在虛擬機和純系統中都試過,在deepin下安裝也試過沒有出現類似問題,但出現了一堆其他問題,感徑訓是源沒有配置好)
1 sudo make -j[N]說明:-j[n]我的電腦是四核四執行緒,可以運行make -j4,如果是八核可用make -j8,如果不想編譯時影響作業,可以用-j3,建議虛擬機的同學就直接運行sudo make就行.
這一步事件很長的,大概需要半個小時往上,請耐心等待,
// 6.進行make install安裝
1 sudo make install六、C++環境配置
// 1.先在/etc/ld.so.conf.d/檔案夾下新建一個opencv4.conf,然后再檔案中寫入/usr/local/lib,
1 cd /etc/ld.so.conf.d/2 sudo touch opencv4.conf3 sudo sh -c 'echo "/usr/local/lib" > opencv4.conf'入代碼片
// 2.更新pkg-config
1 sudo ldconfig// 3.查看安裝
1 pkg-config --libs opencv42 pkg-config --cflags opencv4
// 4.配置bash
1 sudo vim /etc/bash.bashrc 2 //在末尾添加3 PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig4 export PKG_CONFIG_PATH
//5.更新bash
1 sudo source /etc/bash.bashrc2 //激活配置然后更新database3 sudo updatedb
可能會遇到問題sudo: source: command not found,這時可如下2步處理(如圖):
1> sudo -s
2> source /etc/profile
安裝完成
七、可能出現的問題
// 1.QT5 Core編譯錯誤

1 CMake Warning at cmake/OpenCVFindLibsGUI.cmake:18 (find_package): 2 By not providing "FindQt5Core.cmake" in CMAKE_MODULE_PATH this project has 3 asked CMake to find a package configuration file provided by "Qt5Core", but 4 CMake did not find one. 5 6 Could not find a package configuration file provided by "Qt5Core" with any 7 of the following names: 8 9 Qt5CoreConfig.cmake 10 qt5core-config.cmake 11 12 Add the installation prefix of "Qt5Core" to CMAKE_PREFIX_PATH or set 13 "Qt5Core_DIR" to a directory containing one of the above files. If 14 "Qt5Core" provides a separate development package or SDK, be sure it has 15 been installed. 16 Call Stack (most recent call first): 17 CMakeLists.txt:466 (include) 18 19 CMake Warning at cmake/OpenCVFindLibsGUI.cmake:19 (find_package): 20 By not providing "FindQt5Gui.cmake" in CMAKE_MODULE_PATH this project has 21 asked CMake to find a package configuration file provided by "Qt5Gui", but 22 CMake did not find one. 23 24 Could not find a package configuration file provided by "Qt5Gui" with any 25 of the following names: 26 27 Qt5GuiConfig.cmake 28 qt5gui-config.cmake 29 30 Add the installation prefix of "Qt5Gui" to CMAKE_PREFIX_PATH or set 31 "Qt5Gui_DIR" to a directory containing one of the above files. If "Qt5Gui" 32 provides a separate development package or SDK, be sure it has been 33 installed. 34 Call Stack (most recent call first): 35 CMakeLists.txt:466 (include) 36 37 38 CMake Warning at cmake/OpenCVFindLibsGUI.cmake:20 (find_package): 39 By not providing "FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project 40 has asked CMake to find a package configuration file provided by 41 "Qt5Widgets", but CMake did not find one. 42 43 Could not find a package configuration file provided by "Qt5Widgets" with 44 any of the following names: 45 46 Qt5WidgetsConfig.cmake 47 qt5widgets-config.cmake 48 49 Add the installation prefix of "Qt5Widgets" to CMAKE_PREFIX_PATH or set 50 "Qt5Widgets_DIR" to a directory containing one of the above files. If 51 "Qt5Widgets" provides a separate development package or SDK, be sure it has 52 been installed. 53 54 Call Stack (most recent call first): 55 56 CMakeLists.txt:466 (include) 57 58 CMake Warning at cmake/OpenCVFindLibsGUI.cmake:21 (find_package): 59 60 By not providing "FindQt5Test.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Qt5Test", but 61 62 CMake did not find one. 63 64 Could not find a package configuration file provided by "Qt5Test" with any 65 66 of the following names: 67 68 Qt5TestConfig.cmake 69 qt5test-config.cmake 70 71 Add the installation prefix of "Qt5Test" to CMAKE_PREFIX_PATH or set 72 "Qt5Test_DIR" to a directory containing one of the above files. If 73 "Qt5Test" provides a separate development package or SDK, be sure it has 74 been installed. 75 Call Stack (most recent call first): 76 CMakeLists.txt:466 (include) 77 78 79 CMake Warning at cmake/OpenCVFindLibsGUI.cmake:22 (find_package): 80 By not providing "FindQt5Concurrent.cmake" in CMAKE_MODULE_PATH this 81 project has asked CMake to find a package configuration file provided by 82 "Qt5Concurrent", but CMake did not find one. 83 84 Could not find a package configuration file provided by "Qt5Concurrent" 85 with any of the following names: 86 87 Qt5ConcurrentConfig.cmake 88 qt5concurrent-config.cmake 89 90 Add the installation prefix of "Qt5Concurrent" to CMAKE_PREFIX_PATH or set 91 "Qt5Concurrent_DIR" to a directory containing one of the above files. If 92 "Qt5Concurrent" provides a separate development package or SDK, be sure it 93 has been installed. 94 Call Stack (most recent call first): 95 CMakeLists.txt:466 (include) 96 97 qmake: could not exec '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No such file or directory 98 CMake Error at /usr/share/cmake-2.8/Modules/FindQt4.cmake:1386 (message): 99 Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x100 Call Stack (most recent call first):101 cmake/OpenCVFindLibsGUI.cmake:34 (find_package)102 CMakeLists.txt:466 (include)103 104 -- Configuring incomplete, errors occurred!105 See also "/home/*******/OpenCV/opencv-2.4.9/build/CMakeFiles/CMakeOutput.log".106 See also "/home/*******/OpenCV/opencv-2.4.9/build/CMakeFiles/CMakeError.log".107 108 Qt5Core-CMake did not find oneView Code
解決辦法:安裝qt5-default
1 sudo apt-get install qt5-default
八、python環境的配置
// 1.安裝python和numpy
1 sudo apt-get install python-dev python-numpy python3-dev python3-numpy #如果已經安裝過,則該步驟省略
// 2.驗證
$ python3>>> import cv2>>> print(cv2.__version__)'4.1.1'>>>
九、卸載openCV
如果安裝出現了百度不能解決的問題,最好的辦法就是卸載重新裝一次(第一次安裝時前前后后卸載了四五次),
// 1. 洗掉openCV4.conf
1 cd /etc/ld.so.conf.d/2 sudo rm opencv4.conf
// 2.卸載
1 cd2 cd ./OpenCV-4.1.1/build3 sudo make uinstall
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/82052.html
標籤:C++
上一篇:2019 ICPC 銀川站
下一篇:c++實作檔案復制并修改相應屬性

