opencv install


install

opencv 官网
installation guide

运行环境: mac + virtualbox + ubuntu18.04 + opencv3.4


1. 依赖准备

1
2
3
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

2. 下载opencv及opencv_contrib

1
2
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

3. 下载opencv及opencv_contrib

编译debug版本,同时支持python2.7:

1
2
3
cd ~/opencv
mkdir build
cd build

ubuntu: Debug版本

1
cmake -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.4.1/modules -D PYTHON2_EXECUTABLE=/usr/bin/python -D BUILD_EXAMPLES=ON -D PYTHON_INCLUDE_DIR=/usr/include/python2.7 PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python2.7 -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so -D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/python2.7/dist-packages/numpy/core/include/ ../opencv-3.4.1

reapberry: Release版本

1
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.4.1/modules -D PYTHON2_EXECUTABLE=/usr/bin/python -D PYTHON_INCLUDE_DIR=/usr/include/python2.7 PYTHON_INCLUDE_DIR2=/usr/include/arm-linux-gnueabihf/python2.7 -D PYTHON_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython2.7.so -D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/python2.7/dist-packages/numpy/core/include/ ../opencv-3.4.1

最后

1
make -j4 && sudo make install

测试


简单打开lena图像.

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

const string WINDOW_NAME = "main_window";
const string FILE_NAME = "./lena.jpg";

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

    namedWindow(WINDOW_NAME, WINDOW_AUTOSIZE);

    Mat src = imread(FILE_NAME);

    /*
    Mat range = src(Range::all(), Range(1, 30));

    MatIterator_<Vec3b> iter = range.begin<Vec3b>();
    for(; iter != range.end<Vec3b>(); iter++){
        (*iter)[0] = 0; // B
        (*iter)[1] = 0; // G
        (*iter)[2] = rand() % 255; // R
    }
    */

    cv::imshow(WINDOW_NAME, src);

    cv::waitKey();

    destroyAllWindows();

    return 0;
}

  

makefile 如下:

1
2
3
4
LIB=-lopencv_highgui -lopencv_core -lopencv_imgproc -lopencv_imgcodecs

test: main.cpp
    g++ $^ -o $@ $(LIB)