Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to install OpenCV 3.1 on Raspbian Jessie (Lite)

Installing OpenCV 3.1 on Raspbian Jessie

Prerequisites

  • Keep your system up to date:
    • $ sudo apt-get update
    • $ sudo apt-get upgrade
    • $ sudo reboot
  • Make sure you've got an internet connection.
  • Make sure you've got
    • wget
    • unzip

Installation

Run the script install-opencv.sh

#!/usr/bin/env bash
OPENCV_VERSION="3.1.0"
OPENCV_URL="https://github.com/Itseez/opencv/archive/${OPENCV_VERSION}.zip"
OPENCV_PACKAGE_NAME="opencv-${OPENCV_VERSION}"
OPENCV_CONTRIB_URL="https://github.com/Itseez/opencv_contrib/archive/${OPENCV_VERSION}.zip"
OPENCV_CONTRIB_PACKAGE_NAME="opencv_contrib-${OPENCV_VERSION}"
PREFIX="${PREFIX:-/usr/local}"
MAKEFLAGS="${MAKEFLAGS:--j 4}"
install_build_dependencies() {
local build_packages="build-essential git cmake pkg-config"
local image_io_packages="libjpeg-dev libtiff5-dev libjasper-dev \
libpng12-dev"
local video_io_packages="libavcodec-dev libavformat-dev \
libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev"
local gtk_packages="libgtk2.0-dev"
local matrix_packages="libatlas-base-dev gfortran"
local python_dev_packages="python2.7-dev python3-dev python-pip python3-pip"
sudo apt-get install -y $build_packages $image_io_packages $gtk_packages \
$video_io_packages $matrix_packages $python_dev_packages
}
install_global_python_dependencies() {
sudo pip install virtualenv virtualenvwrapper
}
install_local_python_dependences() {
pip install numpy
}
download_packages() {
wget -c -O "${OPENCV_PACKAGE_NAME}.zip" "$OPENCV_URL"
wget -c -O "${OPENCV_CONTRIB_PACKAGE_NAME}.zip" "$OPENCV_CONTRIB_URL"
}
unpack_packages() {
# unzip args:
# -q = quiet
# -n = never overwrite existing files
unzip -q -n "${OPENCV_PACKAGE_NAME}.zip"
unzip -q -n "${OPENCV_CONTRIB_PACKAGE_NAME}.zip"
}
setup_virtualenv() {
export WORKON_HOME="$HOME/.virtualenvs"
source /usr/local/bin/virtualenvwrapper.sh
mkvirtualenv -p python3 cv
workon cv
install_local_python_dependences
}
build() {
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX="$PREFIX" \
-D INSTALL_C_EXMAPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH="$HOME/$OPENCV_CONTRIB_PACKAGE_NAME/modules" \
-D BUILD_EXAMPLES=ON \
..
make ${MAKEFLAGS}
}
install() {
sudo make install
sudo ldconfig
}
log() {
local msg="$1"; shift
local _color_bold_yellow='\e[1;33m'
local _color_reset='\e[0m'
echo -e "\[${_color_bold_yellow}\]${msg}\[${_color_reset}\]"
}
main() {
log "Installing build dependencies..."
install_build_dependencies
log "Downloading OpenCV packages..."
download_packages
log "Unpacking OpenCV packages..."
unpack_packages
log "Installing global python deps..."
install_global_python_dependencies
log "Setting up local python environment..."
setup_virtualenv
log "Building OpenCV..."
cd "$OPENCV_PACKAGE_NAME"
mkdir build
cd build
build
echo "Installing OpenCV..."
install
}
main
@tuchs

This comment has been minimized.

Show comment
Hide comment
@tuchs

tuchs Dec 25, 2015

Typo in line 66? When I tried to install opencv manually and enable the c examples cmake failed in samples/gpu/CMakeLists.txt, looks like a bug in there.

tuchs commented Dec 25, 2015

Typo in line 66? When I tried to install opencv manually and enable the c examples cmake failed in samples/gpu/CMakeLists.txt, looks like a bug in there.

@willprice

This comment has been minimized.

Show comment
Hide comment
@willprice

willprice Dec 25, 2015

Hey @tuchs, yeah, bit buggy isn't it! I've just uploaded my most recent revision which seems to now build. Let me know if you've got any further problems.

Owner

willprice commented Dec 25, 2015

Hey @tuchs, yeah, bit buggy isn't it! I've just uploaded my most recent revision which seems to now build. Let me know if you've got any further problems.

@korud

This comment has been minimized.

Show comment
Hide comment
@korud

korud Jan 11, 2016

i have problem. not objet makefile installing opencv.

korud commented Jan 11, 2016

i have problem. not objet makefile installing opencv.

@hexagon-robotics

This comment has been minimized.

Show comment
Hide comment
@hexagon-robotics

hexagon-robotics Jan 14, 2016

Thank you!,

I could install without any problems.

pi@raspberrypi:~ $ python
Python 2.7.9 (default, Mar 8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import cv2
cv2.version
'3.1.0'

Thank you!,

I could install without any problems.

pi@raspberrypi:~ $ python
Python 2.7.9 (default, Mar 8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import cv2
cv2.version
'3.1.0'

@andreasringdal

This comment has been minimized.

Show comment
Hide comment
@andreasringdal

andreasringdal Jan 25, 2016

If you got a fresh rasbian image you might have to do a
sudo apt-get install cmake

If you got a fresh rasbian image you might have to do a
sudo apt-get install cmake

@freePerro

This comment has been minimized.

Show comment
Hide comment
@freePerro

freePerro Jan 26, 2016

This is with python 3.x.x bindings?

This is with python 3.x.x bindings?

@danbornman

This comment has been minimized.

Show comment
Hide comment
@danbornman

danbornman Mar 11, 2016

Does this include FFMEG? I don't see it in the dependencies section.

Does this include FFMEG? I don't see it in the dependencies section.

@timbuktu031

This comment has been minimized.

Show comment
Hide comment
@timbuktu031

timbuktu031 Apr 1, 2016

hi~
I use raspberry pi 1 B.

MAKEFLAGS="${MAKEFLAGS:--j 4}"

Pi model 1 B can't use option '--j 4'.
How can I change this line?

hi~
I use raspberry pi 1 B.

MAKEFLAGS="${MAKEFLAGS:--j 4}"

Pi model 1 B can't use option '--j 4'.
How can I change this line?

@seaspac

This comment has been minimized.

Show comment
Hide comment
@seaspac

seaspac Apr 3, 2016

I am using this script but getting following errors.

"-- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
CMake Error at cmake/OpenCVModule.cmake:295 (message):
No extra modules found in folder: /root/opencv_contrib-3.1.0/modules

Please provide path to 'opencv_contrib/modules' folder.
Call Stack (most recent call first):
modules/CMakeLists.txt:7 (ocv_glob_modules)"
in the End I am getting following message

"-- Configuring incomplete, errors occurred!
See also "/home/pi/opencv-3.1.0/build/CMakeFiles/CMakeOutput.log".
See also "/home/pi/opencv-3.1.0/build/CMakeFiles/CMakeError.log".
Makefile:8280: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1"

any idea what should I do

seaspac commented Apr 3, 2016

I am using this script but getting following errors.

"-- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
CMake Error at cmake/OpenCVModule.cmake:295 (message):
No extra modules found in folder: /root/opencv_contrib-3.1.0/modules

Please provide path to 'opencv_contrib/modules' folder.
Call Stack (most recent call first):
modules/CMakeLists.txt:7 (ocv_glob_modules)"
in the End I am getting following message

"-- Configuring incomplete, errors occurred!
See also "/home/pi/opencv-3.1.0/build/CMakeFiles/CMakeOutput.log".
See also "/home/pi/opencv-3.1.0/build/CMakeFiles/CMakeError.log".
Makefile:8280: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1"

any idea what should I do

@geotheory

This comment has been minimized.

Show comment
Hide comment
@geotheory

geotheory May 27, 2016

This fails for me with various errors - detailed here in the end of build report and CMakeError.log. This is a fresh clean Jessie with nothing else installed, running on an A+. Any idea what's causing it?

This fails for me with various errors - detailed here in the end of build report and CMakeError.log. This is a fresh clean Jessie with nothing else installed, running on an A+. Any idea what's causing it?

@luisomoreau

This comment has been minimized.

Show comment
Hide comment
@luisomoreau

luisomoreau Jun 9, 2016

You might need to set -D INSTALL_C_EXAMPLES=OFF (rather than ON ) in the cmake command according to pyimagesearch.com

You might need to set -D INSTALL_C_EXAMPLES=OFF (rather than ON ) in the cmake command according to pyimagesearch.com

@fake-name

This comment has been minimized.

Show comment
Hide comment
@fake-name

fake-name Jun 30, 2016

This seems to assume the script will be run in ~/, which isn't always valid. I had to hand-fudge the cmake call a bit. Maybe document that assumption?

This seems to assume the script will be run in ~/, which isn't always valid. I had to hand-fudge the cmake call a bit. Maybe document that assumption?

@theBerryman

This comment has been minimized.

Show comment
Hide comment
@theBerryman

theBerryman Nov 30, 2016

fresh pi3, fresh jessie ... no go...

Configuring incomplete, errors occurred!
See also "/home/pi/Downloads/c216fcbeba8d14ad1138-6e9024162b2645989d5eca6db19f81df49a6accd/opencv-3.1.0/build/CMakeFiles/CMakeOutput.log".
See also "/home/pi/Downloads/c216fcbeba8d14ad1138-6e9024162b2645989d5eca6db19f81df49a6accd/opencv-3.1.0/build/CMakeFiles/CMakeError.log".
make: *** No targets specified and no makefile found. Stop.
Installing OpenCV...
make: *** No rule to make target 'install'. Stop.

fresh pi3, fresh jessie ... no go...

Configuring incomplete, errors occurred!
See also "/home/pi/Downloads/c216fcbeba8d14ad1138-6e9024162b2645989d5eca6db19f81df49a6accd/opencv-3.1.0/build/CMakeFiles/CMakeOutput.log".
See also "/home/pi/Downloads/c216fcbeba8d14ad1138-6e9024162b2645989d5eca6db19f81df49a6accd/opencv-3.1.0/build/CMakeFiles/CMakeError.log".
make: *** No targets specified and no makefile found. Stop.
Installing OpenCV...
make: *** No rule to make target 'install'. Stop.

@FrankBau

This comment has been minimized.

Show comment
Hide comment
@FrankBau

FrankBau Apr 6, 2017

Hi, the https://github.com/Itseez/ organization seems to be outdated, I get "301 Moved Permanently". You might want to use https://github.com/opencv/ instead. hth Frank

FrankBau commented Apr 6, 2017

Hi, the https://github.com/Itseez/ organization seems to be outdated, I get "301 Moved Permanently". You might want to use https://github.com/opencv/ instead. hth Frank

@mrlunk

This comment has been minimized.

Show comment
Hide comment
@mrlunk

mrlunk Apr 10, 2017

Urr...
How do I get into the environment where CV is loaded ? (noob here.)
'workon cv' din't work...

import cv2
ImportError: No module named 'cv2'

mrlunk commented Apr 10, 2017

Urr...
How do I get into the environment where CV is loaded ? (noob here.)
'workon cv' din't work...

import cv2
ImportError: No module named 'cv2'

@crissando

This comment has been minimized.

Show comment
Hide comment
@crissando

crissando Apr 13, 2017

Configuring incomplete, errors occurred!
See also "/home/ubuntu/Documentos/WINDOWS10/opencv-3.0.0/build/CMakeFiles/CMakeOutput.log".
See also "/home/ubuntu/Documentos/WINDOWS10/opencv-3.0.0/build/CMakeFiles/CMakeError.log".

alguien que me pueda ayudar con este problema en ubuntu

Configuring incomplete, errors occurred!
See also "/home/ubuntu/Documentos/WINDOWS10/opencv-3.0.0/build/CMakeFiles/CMakeOutput.log".
See also "/home/ubuntu/Documentos/WINDOWS10/opencv-3.0.0/build/CMakeFiles/CMakeError.log".

alguien que me pueda ayudar con este problema en ubuntu

@aa110711

This comment has been minimized.

Show comment
Hide comment
@aa110711

aa110711 Apr 26, 2017

Urr...
How do I get into the environment where CV is loaded ?

'workon cv' din't work...

import cv2
ImportError: No module named 'cv2'

Urr...
How do I get into the environment where CV is loaded ?

'workon cv' din't work...

import cv2
ImportError: No module named 'cv2'

@aa110711

This comment has been minimized.

Show comment
Hide comment
@aa110711

aa110711 Apr 26, 2017

pls help us!!!! its urgent

pls help us!!!! its urgent

@aa110711

This comment has been minimized.

Show comment
Hide comment
@aa110711

aa110711 Apr 26, 2017

i try install opencv 3 based on steps provided in pyimagesearch.com

i try install opencv 3 based on steps provided in pyimagesearch.com

@aa110711

This comment has been minimized.

Show comment
Hide comment
@aa110711

aa110711 Apr 26, 2017

but i get error as stated above

but i get error as stated above

@mapodev

This comment has been minimized.

Show comment
Hide comment
@mapodev

mapodev May 21, 2017

I get this error

[ 49%] Built target opencv_imgcodecs
[ 49%] Linking CXX shared library ../../lib/libopencv_videoio.so
/usr/bin/ld: /usr/local/lib/libx264.a(common.o): relocation R_ARM_MOVW_ABS_NC against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libx264.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:254: recipe for target 'lib/libopencv_videoio.so.3.2.0' failed
make[2]: *** [lib/libopencv_videoio.so.3.2.0] Error 1
CMakeFiles/Makefile2:5596: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2
Installing OpenCV...
[  3%] Built target libwebp
[  8%] Built target IlmImf
[ 11%] Built target carotene_objs
[ 12%] Built target tegra_hal
[ 12%] Built target opencv_core_pch_dephelp
[ 13%] Built target pch_Generate_opencv_core
[ 16%] Built target opencv_core
[ 17%] Built target opencv_ts_pch_dephelp
[ 17%] Built target pch_Generate_opencv_ts
[ 17%] Built target opencv_imgproc_pch_dephelp
[ 17%] Built target pch_Generate_opencv_imgproc
[ 21%] Built target opencv_imgproc
[ 21%] Built target opencv_imgcodecs_pch_dephelp
[ 21%] Built target pch_Generate_opencv_imgcodecs
[ 22%] Built target opencv_imgcodecs
[ 22%] Built target opencv_videoio_pch_dephelp
[ 22%] Built target pch_Generate_opencv_videoio
[ 22%] Linking CXX shared library ../../lib/libopencv_videoio.so
/usr/bin/ld: /usr/local/lib/libx264.a(common.o): relocation R_ARM_MOVW_ABS_NC against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libx264.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:254: recipe for target 'lib/libopencv_videoio.so.3.2.0' failed
make[2]: *** [lib/libopencv_videoio.so.3.2.0] Error 1
CMakeFiles/Makefile2:5596: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

mapodev commented May 21, 2017

I get this error

[ 49%] Built target opencv_imgcodecs
[ 49%] Linking CXX shared library ../../lib/libopencv_videoio.so
/usr/bin/ld: /usr/local/lib/libx264.a(common.o): relocation R_ARM_MOVW_ABS_NC against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libx264.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:254: recipe for target 'lib/libopencv_videoio.so.3.2.0' failed
make[2]: *** [lib/libopencv_videoio.so.3.2.0] Error 1
CMakeFiles/Makefile2:5596: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2
Installing OpenCV...
[  3%] Built target libwebp
[  8%] Built target IlmImf
[ 11%] Built target carotene_objs
[ 12%] Built target tegra_hal
[ 12%] Built target opencv_core_pch_dephelp
[ 13%] Built target pch_Generate_opencv_core
[ 16%] Built target opencv_core
[ 17%] Built target opencv_ts_pch_dephelp
[ 17%] Built target pch_Generate_opencv_ts
[ 17%] Built target opencv_imgproc_pch_dephelp
[ 17%] Built target pch_Generate_opencv_imgproc
[ 21%] Built target opencv_imgproc
[ 21%] Built target opencv_imgcodecs_pch_dephelp
[ 21%] Built target pch_Generate_opencv_imgcodecs
[ 22%] Built target opencv_imgcodecs
[ 22%] Built target opencv_videoio_pch_dephelp
[ 22%] Built target pch_Generate_opencv_videoio
[ 22%] Linking CXX shared library ../../lib/libopencv_videoio.so
/usr/bin/ld: /usr/local/lib/libx264.a(common.o): relocation R_ARM_MOVW_ABS_NC against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libx264.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:254: recipe for target 'lib/libopencv_videoio.so.3.2.0' failed
make[2]: *** [lib/libopencv_videoio.so.3.2.0] Error 1
CMakeFiles/Makefile2:5596: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

@darshisen

This comment has been minimized.

Show comment
Hide comment
@darshisen

darshisen May 21, 2017

Guys - If you got stuck here - follow these steps: https://imaginghub.com/projects/144-installing-opencv-3-on-raspberry-pi-3/documentation

Pay careful attention to this path: OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules

Make sure it points to where yours is.

Best,
Darshi

Guys - If you got stuck here - follow these steps: https://imaginghub.com/projects/144-installing-opencv-3-on-raspberry-pi-3/documentation

Pay careful attention to this path: OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules

Make sure it points to where yours is.

Best,
Darshi

@avinashpatel57

This comment has been minimized.

Show comment
Hide comment
@avinashpatel57

avinashpatel57 Dec 28, 2017

Nice script. It installed opencv for me without any error. Thanks

Nice script. It installed opencv for me without any error. Thanks

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