Skip to content

Instantly share code, notes, and snippets.

@clamytoe
Last active September 19, 2023 09:50
Show Gist options
  • Save clamytoe/3424d0201bba0073ff1313e80ffc6328 to your computer and use it in GitHub Desktop.
Save clamytoe/3424d0201bba0073ff1313e80ffc6328 to your computer and use it in GitHub Desktop.
Script to build OpenCV 3.4.2 from source into an Anaconda virtual environment on a Linux Mint 19 machine.
#!/usr/bin/zsh
###############################################################################
# buildOpenCV.zsh - #
# #
# Script to build OpenCV 3.4.2 from source into an Anaconda virtual #
# environment on a Linux Mint 19 machine. #
###############################################################################
# Install requirements
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake unzip pkg-config
sudo apt-get install libjpeg-dev libpng-dev libtiff-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 libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3-dev
# Move to Downloads folder
cd ~/Downloads
wget -O opencv.zip -c https://github.com/opencv/opencv/archive/3.4.2.zip
wget -O opencv_contrib.zip -c https://github.com/opencv/opencv_contrib/archive/3.4.2.zip
unzip opencv.zip
unzip opencv_contrib.zip
# Create virtual environment
conda create -y --name cv numpy
conda activate cv
# Generate make files and install
cd opencv-3.4.2
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=~/anaconda3/envs/cv \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv_contrib-3.4.2/modules \
-D PYTHON_EXECUTABLE=~/anaconda3/envs/cv/bin/python \
-D BUILD_EXAMPLES=ON ..
make -j4
make install
ldconfig -n ~/anaconda3/envs/cv/lib
# Verify that it worked
python -c "import cv2; print(cv2.__version__)"
# Cleanup
cd ~/Downloads
rm opencv.zip opencv_contrib.zip
rm -rf opencv-3.4.2 opencv_contrib-3.4.2
cd
# Deactivate virtual environment
conda deactivate
@clamytoe
Copy link
Author

I've only tested the cmake section and have not ran the whole thing from scratch. If anyone tries it and it works, please let me know. Also, if something doesn't work, let me know as well and I will revisit the script.

Thanks!

@c-r-p
Copy link

c-r-p commented Jul 21, 2018

Thank you for posting this.
I used your instructions (found in the comments on pyimagesearch.com) to compile for Miniconda3 on Ubuntu 18.04, and all appears to be working.

@suresh88
Copy link

Hi Clamytoe,

I am trying to build the opencv with anaconda3 and I am facing the below error:
/home/opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:653:20: fatal error: boostdesc_bgm.i: No such file or directory
#include "boostdesc_bgm.i"
^~~~~~~~~~~~~~~~~
Any help is greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment