OpenCV 3 Setup in Buildroot - cu-ecen-aeld/buildroot-assignments-base GitHub Wiki
This is a guide to setup OpenCV 3 on Raspberry Pi 4, but also can be generalize to other platform.
- Raspberry Pi 4
- Keyboard
- UVC camera (I used Logitech C270 webcam)
- Display (Optional, you can view image directly on Raspberry Pi)
Ubuntu 16.04 or +
git clone https://github.com/chwe3468/buildroot-assignments-base
cd buildroot-assignments-base
git checkout hw-rpi-br-opencv-2020.02
- If you are using the platform and camera specified above. Please use the instruction below:
- Run build.sh the first time to git clone buildroot submodule and to import aesd_rpi_defconfig to buildroot
- Go to Linux-menuconfig: Camera Driver section to make changes to linux kernel configuration.
- Run build.sh the second time to build buildroot image
- Go straight to Test Your Image section.
- If plan to use other platform or hardware, please follow the instruction below.
- Run the following command remove current aesd_rpi_defconfig(so that you can set up you own from scratch)
rm base_external/configs/aesd_rpi_defconfig
- Go to Buildroot Setup Section to start configuring your own buildroot
Buildroot Setup(I used 2020.02, feel free to use any newer version. Make sure the version you select has buildroot/configs/raspberrypi4_defconfig or other Raspberry Pi hardware you want to use):
git clone git://git.buildroot.net/buildroot
cd buildroot
checkout 2020.02
cd ..
Now runs build.sh the first time to import raspberrypi4_defconfig to buildroot. Also run save_config.sh to save the .config file to base_external/configs/aesd_def_config
./build.sh
./save_config.sh
Now the basic setup is finish. Lets jumps into the configuration change to the defconfig.
cd buildroot
make menuconfig
First we need to change the C library we used to glibc, because most of the packages we will use are only supported by glibc
Toolchain -> C library -> glibc
Optionally, you can change the password or name of the system
System Configuration -> System Banner
System Configuration -> Root Password
Now we need to enable root file system overlay,
System Configuration -> Root filesystem overlay directories
Enter ../base_external/rootfs_overlay and hit ok
Now go to
Filesystem image -> exact size
Enter 500M, and hit OK, because the packages we will be installing are large. (500M is larger than what it requires)
Before we enable Opencv Option. We need to have a graphic driver first. I select a software graphics driver for this. However, you might want to use hardware graphics driver if you want to. It is recommended to use this software driver to get the system to work first.
Enable
Target packages -> Graphic libraries and applications -> mesa3d
Also goes into mesa3d, and enable
Target packages -> Graphic libraries and applications -> mesa3d -> Gallium swrast driver
Gallium swrast is the software graphics driver we used. Read more options about mesa drivers mesa driver
Also enable support for OpenGL
Target packages -> Graphic libraries and applications -> mesa3d -> OpenGL EGL
Target packages -> Graphic libraries and applications -> mesa3d -> OpenGL ES
Now the basic graphics driver is setup, we need to the dependent package required by OpenCV, which is documented on this page OpenCV Requirement
So what we need additionally are GTK, Python, ffmepg
For GTK, use libgtk3
Target packages -> libraries-> Graphics -> libgtk3
You will see Wayland backend is enabled by default, but they are not necessary since we won’t use its desktop GUI. Go ahead and disable it.
For python, I chose python3
Target packages -> Interpreter languages and scripting -> python3
For ffmpeg, it should be already enabled, located at
Target packages -> Audio and video applications -> ffmepg
Now, enable opencv3
Target packages -> libraries-> Graphics -> opencv3
Now a basic OpenCV3 is set up. You might use OpenCV2 if you want to do everything in C language instead of C++. It it recommended to use C++. However, you will need more tweak to get your makefile to work later.
Now we need to add more modules to OpenCV3. I will recommend flann, highgui, imgcodecs, imgproc, videoio, jpeg20000, jpeg, png, ffmepg support, v4l support. These modules should fulfill your basic usage. You might want to add objdetect if you want to use it in the future.
Target packages -> libraries-> Graphics -> opencv3 -> flann
Target packages -> libraries-> Graphics -> opencv3 -> highgui
Target packages -> libraries-> Graphics -> opencv3 -> imgcodecs
Target packages -> libraries-> Graphics -> opencv3 -> imgproc
Target packages -> libraries-> Graphics -> opencv3 -> videoio
Target packages -> libraries-> Graphics -> opencv3 -> video
Target packages -> libraries-> Graphics -> opencv3 -> jpeg20000
Target packages -> libraries-> Graphics -> opencv3 -> jpeg
Target packages -> libraries-> Graphics -> opencv3 -> png
Target packages -> libraries-> Graphics -> opencv3 -> ffmepg support
Target packages -> libraries-> Graphics -> opencv3 -> v4l support
Now we can setup some useful graphics application for image/video viewing in command prompt.
For image viewing, I recommend fbv. You might also want to use fswebcam if you just want to get an image directly from /dev/video0
Target packages -> Graphic libraries and applications -> fbv
Target packages -> Graphic libraries and applications -> fswebcam
For video capturing(not viewing), you can just use ffmpeg
For video viewing, I haven’t find anything in buildroot good for this.
Now we finish menuconfig
We need to edit linux-menuconfig because (USB video class)UVC drvier for the Logitech webcam is not enabled by default
make linux-menuconfig
This should be a long process, make sure you have internet connection on your ubuntu machine.
The following tweak is required for Logitech’s UVC camera so far as I know. If you are going to use other camera from different vendor. Please enable your corresponding driver in linux-menuconfig. You can find your driver in this link, and look for the driver with the same USB ID. (or you can just enable all of them and have a larger image)
Once done go to
Device Driver -> Multimedia support
Change the <M>
to <*>
by using space on your keyboard
Device Driver -> Multimedia support -> Media USB Adapter -> USB Video Class (UVC)
Change the <M>
to <*>
by using space on your keyboard
Device Driver -> Multimedia support -> Media USB Adapter -> GSPCA based webcams
Change the <M>
to <*>
by using space on your keyboard
For detail about GSPCA based webcams, go to GSPCA
After all these, you can build the system now.
make
Once done, you can write your image to your SD card, connect keyboard and webcam to your and power it up
To run the example program
cd /home
./capture
An image called cap.png should be created in the /home. To view the image use
fbv cap.png
Alteranatively you can capture and view image with
fswebcam --save fswebcam_cap.jpeg
fbv fswebcam_cap.jpeg
For demo, this is the image I took:
The first image is the command I put
The second image is the image taken by my Raspberry Pi 4
To find the source code and makefile for it please visit aesd-cam
Optionally, you can enable WiFi on your Rasberry Pi module. Thanks to [this link] (https://blog.crysys.hu/2018/06/enabling-wifi-and-converting-the-raspberry-pi-into-a-wifi-ap)
You only need to follow the Enabling Wi-Fi section of the article. AP setting is not needed.