Skip to content
Snippets Groups Projects

OpenCV Compile Script

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Jonathan Miller

    This script is used to setup OpenCV on a raspberry pi.

    Set it up using the command

    curl -# https://git.mst.edu/snippets/50/raw > opencv_setup.sh && chmod +x opencv_setup.sh
    Edited
    opencv_setup.sh 1.50 KiB
    #!/bin/bash
    
    # Variables
    INSTALL_PREFIX=/usr/local
    COMPILE_DIR=~/.build/opencv_compile
    JOBS=3
    
    # Change directories to wherever you want to compile opencv
    mkdir -p $COMPILE_DIR
    cd $COMPILE_DIR
    
    echo -e "\n\n[WARNING] Make sure to install the correct dependiencies: build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev\n\n"
    echo -e "[INFO] Compile directory: $COMPILE_DIR"
    echo -e "[INFO] Install directory: $INSTALL_PREFIX"
    
    # Clone the two projects
    echo "[INFO] I am cloning 2 repo's at once so the output might be a little buggy"
    git clone --depth 1 https://github.com/opencv/opencv.git &
    git clone --depth 1 https://github.com/opencv/opencv_contrib.git
    
    # Wait for jobs to finish
    wait
    
    # Change directories into the OpenCV folder
    cd opencv
    # Go into the build folder
    mkdir build
    cd build
    
    # Setup the build scripts
    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ -DBUILD_opencv_java=OFF -DBUILD_opencv_python=OFF -DOPENCV_ENABLE_NONFREE=YES ..
    
    
    # Show warnings
    echo -e '\n\n[WARNING] Make sure you have more than the default amount of memory and swap memory by default on a pi (1 GB and 100 MB)'
    echo -e 'I recommend 1 Gb of swap space. A good example of how to manipulate swap can be found here:'
    echo -e 'https://wiki.archlinux.org/index.php/swap#Swap_file\n\n'
    
    # Compile everything
    make -j$JOBS
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment