- Mastering OpenCV 4
- Roy Shilkrot David Millán Escrivá
- 663字
- 2021-07-02 14:47:38
Installing OpenCV on an embedded device
There is a very easy way to install OpenCV and all its dependencies on a Debian-based embedded device such as Raspberry Pi:
sudo apt-get install libopencv-dev
However, that might install an old version of OpenCV from one or two years ago.
To install the latest version of OpenCV on an embedded device such as Raspberry Pi, we need to build OpenCV from the source code. First, we install a compiler and build system, then libraries for OpenCV to use, and finally OpenCV itself. Note that the steps for compiling OpenCV from source on Linux are the same whether you are compiling for desktop or for embedded systems. A Linux script, install_opencv_from_source.sh, is provided with this book; it is recommended you copy the file onto your Raspberry Pi (for example, with a USB flash stick) and run the script to download, build, and install OpenCV, including potential multi-core CPU and ARM NEON SIMD optimizations (depending on hardware support):
chmod +x install_opencv_from_source.sh
./install_opencv_from_source.sh
It's highly recommended to build and run a few OpenCV samples every time you install OpenCV, so when you have problems building your own code, at least you will know whether the problem is the OpenCV installation or a problem with your code.
Let's try to build the simple edge sample program. If we try the same Linux command to build it from OpenCV 2, we get a build error:
cd ~/opencv-4.*/samples/cpp
g++ edge.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui
-o edge
/usr/bin/ld: /tmp/ccDqLWSz.o: undefined reference to symbol '_ZN2cv6imreadERKNS_6StringEi'
/usr/local/lib/libopencv_imgcodecs.so.4..: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
The second to last line of that error message tells us that a library was missing from the command line, so we simply need to add -lopencv_imgcodecs in our command next to the other OpenCV libraries we linked to. Now, you know how to fix the problem anytime you are compiling an OpenCV 3 program and you see that error message. So, let's do it correctly:
cd ~/opencv-4.*/samples/cpp
g++ edge.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui
-lopencv_imgcodecs -o edge
It worked! So, now you can run the program:
./edge
Hit Ctrl + C on your keyboard to quit the program. Note that the edge program might crash if you try running the command in an SSH Terminal and you don't redirect the window to display on the device's LCD screen. So, if you are using SSH to remotely run the program, add DISPLAY=:0 before your command:
DISPLAY=:0 ./edge
You should also plug a USB webcam into the device and test that it works:
g++ starter_video.cpp -lopencv_core -lopencv_imgproc
-lopencv_highgui -lopencv_imgcodecs -lopencv_videoio \
-o starter_video
DISPLAY=:0 ./starter_video 0
Note: if you don't have a USB webcam, you can test using a video file:
DISPLAY=:0 ./starter_video ../data/768x576.avi
Now that OpenCV is successfully installed on your device, you can run the Cartoonifier applications we developed earlier. Copy the Cartoonifier folder onto the device (for example, by using a USB flash stick, or using scp to copy files over the network). Then, build the code just like you did for the desktop:
cd ~/Cartoonifier
export OpenCV_DIR="~/opencv-3.1.0/build"
mkdir build
cd build
cmake -D OpenCV_DIR=$OpenCV_DIR ..
make
And run it:
DISPLAY=:0 ./Cartoonifier
And if all is fine, we will see a window with our application running as follows:

- DevOps with Kubernetes
- ReSharper Essentials
- Pandas Cookbook
- 深入淺出Prometheus:原理、應用、源碼與拓展詳解
- OpenCV for Secret Agents
- JavaScript前端開發與實例教程(微課視頻版)
- Web全棧工程師的自我修養
- JS全書:JavaScript Web前端開發指南
- MySQL數據庫管理與開發實踐教程 (清華電腦學堂)
- Building Minecraft Server Modifications
- 大學計算機基礎(第2版)(微課版)
- 基于SpringBoot實現:Java分布式中間件開發入門與實戰
- PHP與MySQL權威指南
- 高效使用Greenplum:入門、進階與數據中臺
- Python物理建模初學者指南(第2版)