LoginSignup
4
2

More than 3 years have passed since last update.

Raspberry Pi3にOpenCV4をインストールする

Posted at

OpenCV4.2.0をインストールする。

■環境
 - Raspberry Pi 3
  - g++ 4.9.2
  - Python3.4.2

■前提条件
以下をインストール済み
 - Git
 - cmake
 - GNU Make

■手順
1.メモリ確保に失敗しないようにswap領域を増やす

$ sudo nano /etc/dphys-swapfile

 CONF_SWAPSIZE100から1024に変更する。

/etc/dphys-swapfile
CONF_SWAPSIZE=1024

 変更を反映する。

$ sudo /etc/init.d/dphys-swapfile restart

2.依存ファイルをインストールする

$ sudo apt update
$ sudo apt install build-essential cmake git pkg-config libgtk-3-dev "libcanberra-gtk*"
$ sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev
$ sudo apt install libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev opencl-headers

3.opencvのソースファイルをダウンロードする

$ git clone --branch 4.2.0 https://github.com/opencv/opencv.git
$ git clone --branch 4.2.0 https://github.com/opencv/opencv_contrib.git

4.ビルド用ディレクトリを作成する

$ mkdir <path-to-opencv>/opencv/build
$ cd <path-to-opencv>/opencv/build

5.設定ファイルを作成する

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=OFF \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D ENABLE_NEON=ON \
    -D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic \
    -D ENABLE_VFPV3=ON \
    -D BUILD_TESTS=OFF \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D OPENCV_EXTRA_MODULES_PATH=/<path-to-opencv>/opencv_contrib/modules \
    -D BUILD_EXAMPLES=OFF ..

 Configuring doneGenerating doneと表示され、処理が正常に終了したことを確認する。

6.コンパイルする

$ make -j4

7.コンパイルしたファイルをinstallする

$ sudo make install

8.正常にインストールされたことを確認する

  • C++
$ pkg-config --modversion opencv4
Output
4.2.0
  • Python3
$ python3 -c "import cv2; print(cv2.__version__)"
Output
4.2.0

■参考リンク
How to Install OpenCV on Raspberry Pi 3
https://linuxize.com/post/how-to-install-opencv-on-raspberry-pi/

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2