學習筆記:自己編譯安裝OpenCV+測試opencv安裝是否成功
阿新 • • 發佈:2019-01-30
分享 install with build efi www inux strong 操作
跟著猿人學python,我的學習筆記。本次的配置在ubuntu中成功安裝了opencv,並通過以下方法測試成功了。現將具體的安裝及測試過程整理出來分享給大家。
1. 安裝編譯依賴的軟件包
# 安裝讀寫不同圖片類型的庫: sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev # 安裝讀取視頻格式的庫: sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev # 安裝GTK庫以使用OpenCV的GUI: sudo apt-get install libgtk2.0-dev # 安裝用來優化一些OpenCV內部函數的庫,比如矩陣操作: sudo apt-get install libatlas-base-dev gfortran liblapacke-dev
2. 編譯OpenCV
下載並解壓源碼:
wget https://github.com/opencv/opencv/archive/3.2.0.tar.gz -O opencv.tar.gz wget https://github.com/opencv/opencv_contrib/archive/3.2.0.tar.gz -O opencv_contrib.tar.gz tar zxf opencv.tar.gz tar zxf opencv_contrib.tar.gz
編譯:
cd opencv-3.2.0 mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=OFF -D INSTALL_PYTHON_EXAMPLES=OFF -D WITH_IPP=OFF -DBUILD_opencv_dnn=OFF -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib-3.2.0/modules -D BUILD_EXAMPLES=OFF .. make -j4 # 編譯時間好長啊,這還是四核一起編譯呢
安裝
編譯沒有出錯的話,就可以安裝了:
sudo make install sudo ldconfig
以上命令會安裝OpenCV的python模塊到:
(py3.6) veelion@gtx:~/opencv/opencv-3.2.0/build$ ls /usr/local/lib/python3.6/site-packages/ cv2.cpython-36m-x86_64-linux-gnu.so
把以上.so文件鏈接到py3.6虛擬環境下:
(py3.6) veelion@gtx:~$ cd ~/.virtualenvs/py3.6/lib/python3.6/site-packages/ (py3.6) veelion@gtx:~/.virtualenvs/py3.6/lib/python3.6/site-packages$ ln -s /usr/local/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
測試OpenCV的安裝
veelion@gtx:~$ workon py3.6 (py3.6) veelion@gtx:~$ python Python 3.6.1 (default, Mar 22 2017, 10:05:53) [GCC 5.4.1 20170304] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ ‘3.2.0‘
文章來自於我的學習筆記www.yuanrenxue.com
學習筆記:自己編譯安裝OpenCV+測試opencv安裝是否成功