Ubuntu 16.04 Python3.5.2 下安裝 OpenCV 3.4.1
1. 安裝工具和依賴項
首先升級系統(tǒng)包和軟件庫
sudo apt update
sudo apt upgrade
安裝一些工具
sudo apt install build-essential cmake pkg-config git wget
安裝numpy模塊
sudo apt install python3-pip
pip3 install numpy
安裝依賴項
sudo apt install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt install libxvidcore-dev libx264-dev
sudo apt install libgtk-3-dev
sudo apt install libatlas-base-dev gfortran
sudo apt install python3.5-dev
2 安裝OpenCV 3.4.3
下載解壓源碼包
wget https://github.com/opencv/opencv/archive/3.4.3.zip
unzip opencv-3.4.3.zip
用cmake配置屬性
cd opencv-3.4.3
mkdir -p build
cd build
開始配置
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_EXECUTABLE=/usr/bin/python3 -D PYTHON_INCLUDE_DIR=/usr/include/python3.5 -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON ../
該步成功艾船,之后輸入
make -j8
sudo make install
至此在python3下執(zhí)行
import cv2
不會報錯,則安裝成功
3 C++調用OpenCV接口
新建test.cpp
gedit test.cpp
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("test.jpg"); // 最好使用絕對路徑
imshow("test",img);
waitKey(5000);
return 0;
}
在控制臺下編譯,或者寫入Makefile
g++ test.cpp -o test `pkg-config opencv --libs`
使用如下代碼運行
./test
4 Qt Creator 調用OpenCV接口
新建控制臺工程test
在test.pro中加入
INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv \
/usr/local/include/opencv2
LIBS += /usr/local/lib/libopencv_highgui.so \
/usr/local/lib/libopencv_core.so \
/usr/local/lib/libopencv_imgproc.so
使用的main.cpp代碼如下:
#include <QCoreApplication>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(void)
{
namedWindow( "test", WINDOW_AUTOSIZE );
Mat img = imread("test.jpg", 1); // 務必使用絕對路徑戚扳,否則可能報錯
imshow("test", img);
waitKey(5000);
return 0;
}