To perform the image processing we will install OpenCV on the Raspberry PI. OpenCV is a free graphics library that makes image processing relatively easy. With OpenCV, it is possible to process images from the Pi Camera V2.
Material
- Raspberry PI avec une distribution Linux (par exemple Raspbian).
Code
Open a terminal and type these commands:
## Désinstallation de Wolfram et libreoffice
sudo apt-get -y purge wolfram-engine
sudo apt-get -y purge libreoffice*
sudo apt-get -y clean
sudo apt-get -y autoremove
sudo apt -y update
sudo apt -y upgrade
From this line we will create a .sh file which we will make executable so that all the commands copied into the .sh file run one after the other.
Go to your desktop. Right click on ‘New file’ and give the following name: install.sh
Open this file by double clicking on it. By default it will open with the basic Raspbian text editor.
Copy all the following lines of code into this file. Running these lines will retrieve the files from a GIT directory, create the dependencies and compile OpenCV on your RaspberryPi.
sudo apt-get -y remove x264 libx264-dev
## Install dependencies
sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm
sudo apt-get -y install git gfortran
sudo apt-get -y install libjpeg8-dev libjasper-dev libpng12-dev
sudo apt-get -y install libtiff5-dev
sudo apt-get -y install libtiff-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get -y install libxine2-dev libv4l-dev
cd /usr/include/linux
sudo ln -s -f ../libv4l1-videodev.h videodev.h
cd $cwd
sudo apt-get -y install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get -y install libgtk2.0-dev libtbb-dev qt5-default
sudo apt-get -y install libatlas-base-dev
sudo apt-get -y install libmp3lame-dev libtheora-dev
sudo apt-get -y install libvorbis-dev libxvidcore-dev libx264-dev
sudo apt-get -y install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get -y install libavresample-dev
sudo apt-get -y install x264 v4l-utils
# Optional dependencies
sudo apt-get -y install libprotobuf-dev protobuf-compiler
sudo apt-get -y install libgoogle-glog-dev libgflags-dev
sudo apt-get -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
sudo apt-get -y install python3-dev python3-pip
sudo -H pip3 install -U pip numpy
sudo apt-get -y install python3-testresources
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout $cvVersion
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout $cvVersion
cd ..
cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \-D CMAKE_INSTALL_PREFIX=$cwd/installation/OpenCV-"$cvVersion" \-D INSTALL_C_EXAMPLES=ON \-D INSTALL_PYTHON_EXAMPLES=ON \-D WITH_TBB=ON \-D WITH_V4L=ON \-D OPENCV_PYTHON3_INSTALL_PATH=$cwd/OpenCV-$cvVersion-py3/lib/python3.5/site-packages \-D WITH_QT=ON \-D WITH_OPENGL=ON \-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \-D BUILD_EXAMPLES=ON ..
make -j$(nproc)
sudo make install
sudo sed -i 's/CONF_SWAPSIZE=1024/CONF_SWAPSIZE=100/g' /etc/dphys-swapfile
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
Once copied, run the following command to make the file just created executable:
chmod +x install.sh
Now we will run this file with the following command:
./install.sh
The installation may take a few hours on RaspberryPi3, you can leave it running without any worries.
Once the commands are finished, you just have to type the last commands in the terminal:
pip3 install opencv-python
sudo apt install libqtgui4
sudo apt install libqt4-test
pip install opencv-contrib-python==4.1.0.25
Once the installation is done, to verify that the installation was successful, create a python file with only the following line of code:
$ import cv2
Run the file with :
python3 NOM_DU_FICHIER
compilation terminated.
make[2]: *** [samples/cpp/CMakeFiles/example_cpp_detect_mser.dir/build.make:63: samples/cpp/CMakeFiles/example_cpp_detect_mser.dir/detect_mser.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:28339: samples/cpp/CMakeFiles/example_cpp_detect_mser.dir/all] Error 2
make: *** [Makefile:163: all] Error 2
I tried to build, but I get an error
[ 98%]
/home/pi/opencv/samples/cpp/detect_mser.cpp:28:10: fatal error: GL/glu.h: No such file or directory
#include
^~~~~~~~~~
compilation terminated.
make[2]: *** [samples/cpp/CMakeFiles/example_cpp_detect_mser.dir/build.make:63: samples/cpp/CMakeFiles/example_cpp_detect_mser.dir/detect_mser.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:28339: samples/cpp/CMakeFiles/example_cpp_detect_mser.dir/all] Error 2
make: *** [Makefile:163: all] Error 2
where is the mistake?
try running the command
sudo apt-get install libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev
hi I have this error. can you help?
Configuring Project
Working directory:
/tmp/pip-install-t4r_4m30/opencv-python_ae79e7448ad14114ac7974dcf06507fb/_skbuild/linux-armv7l-3.7/cmake-build
Command:
cmake /tmp/pip-install-t4r_4m30/opencv-python_ae79e7448ad14114ac7974dcf06507fb/opencv -G ‘Unix Makefiles’ -DCMAKE_INSTALL_PREFIX:PATH=/tmp/pip-install-t4r_4m30/opencv-python_ae79e7448ad14114ac7974dcf06507fb/_skbuild/linux-armv7l-3.7/cmake-install -DPYTHON_EXECUTABLE:FILEPATH=/home/pi/.virtualenvs/opencv_2/bin/python -DPYTHON_VERSION_STRING:STRING=3.7.3 -DPYTHON_INCLUDE_DIR:PATH=/usr/include/python3.7m -DPYTHON_LIBRARY:FILEPATH=/usr/lib/arm-linux-gnueabihf/libpython3.7m.so -DSKBUILD:INTERNAL=TRUE -DCMAKE_MODULE_PATH:PATH=/tmp/pip-build-env-di0lpdqe/overlay/lib/python3.7/site-packages/skbuild/resources/cmake -DPYTHON3_EXECUTABLE=/home/pi/.virtualenvs/opencv_2/bin/python -DPYTHON3_INCLUDE_DIR=/usr/include/python3.7m -DPYTHON3_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython3.7m.so -DBUILD_opencv_python3=ON -DBUILD_opencv_python2=OFF -DBUILD_opencv_java=OFF -DOPENCV_PYTHON3_INSTALL_PATH=python -DINSTALL_CREATE_DISTRIB=ON -DBUILD_opencv_apps=OFF -DBUILD_opencv_freetype=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_DOCS=OFF -DPYTHON3_LIMITED_API=ON -DBUILD_OPENEXR=ON -DCMAKE_BUILD_TYPE:STRING=Release
Traceback (most recent call last):
An error occurred while building with CMake.
Command:
cmake –build . –target install –config Release —
Source directory:
/tmp/pip-install-t4r_4m30/opencv-python_ae79e7448ad14114ac7974dcf06507fb
Working directory:
/tmp/pip-install-t4r_4m30/opencv-python_ae79e7448ad14114ac7974dcf06507fb/_skbuild/linux-armv7l-3.7/cmake-build
Please see CMake’s output for more information.
Hi,
Which raspberry pi and which OS do you have?
I need opencv in cpp in rpi 3b