Install OpenCV on raspberry pi without monitor and keyboard

  1. install Raspbian without a mouse and a keyboard

    (1) Download Raspbian image form

    (2) Format Micro-SD card using SD Card Formatter

    (3) Use Etcher to burn Raspbian image to the Micro-SD card

    (4) Create an empty file named “ssh” in the SD card root folder to enable ssh

    (5) Create a network wifi config file named “wpa_supplicant.conf” in the SD card root folder to setup wifi

      ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

    (6) Boot the PI with the SD card, you can connect to the PI using ssh pi@raspberrypi or ssh pi@ipaddress

  2. Install VNC

    (1) The Raspbian default password is raspberry, and you can change it using passwd command

    (2) Install the latest VNC

       sudo apt-get update 
       sudo apt-get install realvnc-vnc-server 
       sudo apt-get install realvnc-vnc-viewer

    (3) You need to enable it by selecting Menu > Preferences > Raspberry Pi Configuration > Interfaces and make sure VNC is set to Enabled or runing sudo raspi-config to eable it. After that, you can use a VNC viewer to connect the Pi

  3. Install OpenCV

    (1) Before you install, you need to add more swap space in case there is no enough memory to build openCV

      sudo dd if=/dev/zero of=/swap0 bs=1M count=1024
      sudo mkswap /swap0
      sudo echo "/swap0 swap swap" >> /etc/fstab
      sudo swapon -a 

    if you cannot do echo "/swap0 swap swap" >> /etc/fstab, you can add it using vi

    (2) Install OpenCV3.4.1 using commands below:

     sudo apt-get install build-essential cmake pkg-config
     sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
     sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev        libv4l-dev
     sudo apt-get install libxvidcore-dev libx264-dev
     sudo apt-get install libgtk2.0-dev
     sudo apt-get install libatlas-base-dev gfortran
     cd ~
     wget -O
     wget -O
     cd ~/opencv-3.4.1/
     mkdir build
     cd build
         -D CMAKE_INSTALL_PREFIX=/usr/local \
         -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.1/modules \
         -D BUILD_EXAMPLES=ON ..
     make -j4
     sudo make install
     sudo ldconfig

    based on