Ubuntu 16.04 + OpenCV3.4.1+Python3.5 编译安装及测试代码

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 python3-dev python3-scipy python3-numpy  python3-pandas python3-matplotlib python3-sklearn

2. 下载代码

$ mkdir opencv_tools; cd opencv_tools
$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git
$ cp -r opencv_contrib opencv

3. 编译

$ mkdir release; cd release
$ 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=OFF \
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
-D PYTHON_EXECUTABLE=/usr/lib/python3 \
-D BUILD_opencv_xfeatures2d=OFF \
-D BUILD_EXAMPLES=ON ..
$ make -j6
  • 以PYTHON开始宏对应的路径与whereis python3输出的路径保持一致
$ whereis python3

4. 安装配置

$ sudo make install   # 安装
$ sudo make uninstall # 卸载
$ sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'   # 添加库路径
$ sudo ldconfig  # 更新系统库
  • 在/etc/bash.bashrc 末尾增加以下内容:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH  
  • 再执行以下命令
$ sudo source /etc/bash.bashrc
$ sudo updatedb   #激活配置然后更新database

5. C++代码测试

  • src/test.cpp
#include 
#include 

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{

    if ( argc != 2 )
    {
        cout << "Need to load a picture..." << endl;
        return -1;
    }
    Mat image;
    image = imread( argv[1], 1 );
    if ( image.empty() )
    {
        cout<<"No image data!"<< endl;
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

  • CMakeLists.txt
# project name
PROJECT(opencv_test)

# requirement of cmake version
cmake_minimum_required(VERSION 3.5)

# set the directory of executable files
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${opencv_test_SOURCE_DIR}/build/bin)

# find required opencv
find_package(OpenCV REQUIRED)

# directory of opencv headers
include_directories(${OpenCV_INCLUDE_DIRS})

# name of executable file and path of source file
add_executable(opencv_test src/test.cpp)

# directory of opencv library
link_directories(${OpenCV_LIBRARY_DIRS})

# opencv libraries
target_link_libraries(opencv_test ${OpenCV_LIBS} )
  • 编译运行
$ mkdir build; cd build;
$ cmake ..
$ make
$ ./test ../1.jpg

5. Python代码测试

  • test.py
import numpy as np
from matplotlib import pyplot as plt
import cv2
print ('OpenCV Version:' + cv2.__version__)

# cv2.IMREAD_COLOR:加载一个彩色图片。图片的透明度会被忽略,这个是默认标志,BGR模式
# cv2.IMREAD_GRAYSCALE:用灰度模式加载图片
# cv2.IMREAD_UNCHANGED:包含alpha通道的方式加载图片
# Load an color image in grayscale
img = cv2.imread('1.jpg', cv2.IMREAD_COLOR)
emptyImage = np.zeros(img.shape, np.uint8)
emptyImage2 = img.copy()
emptyImage3=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

cv2.imshow("EmptyImage", emptyImage)
cv2.imshow("Image", img)
cv2.imshow("EmptyImage2", emptyImage2)
cv2.imshow("EmptyImage3", emptyImage3)
cv2.imwrite("./cat2.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 5])
cv2.imwrite("./cat3.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
cv2.imwrite("./cat.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
cv2.imwrite("./cat2.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

cv2.waitKey (0)
cv2.destroyAllWindows()

# k = cv2.waitKey(0)
# print('k=' + str(k));
# if k == 27:                 # wait for ESC key to exit
#     cv2.destroyAllWindows()
# elif k == ord('s'):        # wait for 's' key to save and exit
#     cv2.imwrite('messigray.png', img)
#     cv2.destroyAllWindows()

  • 测试
$ python3 test.py

参考

  • https://docs.opencv.org/trunk/d7/d9f/tutorial_linux_install.html
  • https://blog.csdn.net/Int93/article/details/79056179
  • https://www.cnblogs.com/Shuqing-cxw/p/9195303.html

你可能感兴趣的:(常用工具)