Install latest OpenCV with Python, Java and C++ examples on Ubuntu/Debian
If you are interested in compiling the latest version of OpenCV (currently OpenCV 4.x.x) for ARM based SBCs or x86 computers then this project will show you how. You should be experienced with Linux, OpenCV and Python (or Java or C++) to make the most of this project. I have created a set of scripts that automate the install process. The scripts support Ubuntu 22.04 and probably other distributions. x86, x86_64, ARMV7 and ARMV8 are currently working.
The image above is a screenshot of a video frame that has been processed by Motion Detector. Motion is bounded by green boxes and pedestrians by blue boxes.
If you plan on processing only video or image files then you can skip this section. Live video will allow you to create smart camera applications that react to a live video stream (versus a streaming only camera). You will need to select a USB camera that works under Linux and has the proper resolution.
sudo usermod -a -G video username
sudo apt install uvcdynctrl v4l-utils
sudo reboot
lsusb
Bus 003 Device 002: ID 1871:0142 Aveo Technology Corp.
uvcdynctrl -f
Pixel format: YUYV (YUYV 4:2:2; MIME type: video/x-raw-yuv)
v4l2-ctl --list-formats-ext
sudo apt install git
cd ~/
git clone --depth 1 https://github.com/sgjava/install-opencv.git
sudo su -
dd if=/dev/zero of=tmpswap bs=1024 count=1M
chmod 600 tmpswap
mkswap tmpswap
swapon tmpswap
free
This is probably the easiest way to install everything, but you can follow the individual steps below to build or rebuild individual components. There are values you can change in the individual scripts, so read them over. Skip the rest of the individual scripts below if you run this.
cd ~/install-opencv/scripts
config.sh
and make changes as neededinstall-opencv.sh
and change version as needed../install.sh
top
to monitor until build completescd ~/install-opencv/scripts
./install-java.sh
java -version
ant -version
Patches jdhuff.c to remove "Invalid SOS parameters for sequential JPEG" warning and jdmarker.c to remove "Corrupt JPEG data: xx extraneous bytes before marker 0xd9" warning. These will fill up the logs if not muted.
cd ~/install-opencv/scripts
./install-libjpeg-turbo.sh
top
to monitor until build completesSometimes all you need is a live video feed without further processing. This section will be what you are looking for. It also makes sense to move the UVC processing into a different Linux process or thread from the main CV code.
I'm running this on a test LAN and not securing mjpg-streamer. In production you will want to use a user and password with mjpg-streamer. You will also want to put it behind a secure proxy if you are accessing it from the Internet.
cd ~/install-opencv/scripts
./install-mjpg-streamer.sh
v4l2-ctl --list-formats
mjpg_streamer -i "/usr/local/lib/mjpg-streamer/input_uvc.so -y" -o "/usr/local/lib/mjpg-streamer/output_http.so -w /usr/local/www"
mjpg_streamer -i "/usr/local/lib/mjpg-streamer/input_uvc.so" -o "/usr/local/lib/mjpg-streamer/output_http.so -w /usr/local/www"
http://yourhost:8080/?action=stream
and you should see the video stream.cd ~/install-opencv/scripts
./install-opencv.sh
top
to monitor until build completesTo run Java programs in Eclipse you need add the OpenCV library.
To run compiled class (Canny for this example) from shell:
cd ~/install-opencv/opencv-java
java -Djava.library.path=/home/<username>/opencv/build/lib -cp /home/<username>/opencv/build/bin/opencv-430.jar:bin com.codeferm.opencv.Canny
To run Python programs in Eclipse you need PyDev installed.
-m cProfile -s time
is used to profile.
To create a new C++ project in Eclipse you need to install CDT plugin first (or use the Eclipse IDE for C/C++ Developers). I'm using the Eclipse IDE for Java EE Developers, so I installed the CDT plugin.
To run C++ example projects
To create new C++ project
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
// Create black empty image
Mat image = Mat::zeros(240, 320, CV_8UC3);
// Draw circles
for (int a = 0; a < 10; a = a + 1) {
circle(image, Point(160, 120), 10.0 * a, Scalar(128, 128, 255), 2, 8);
}
// Draw lines
for (int a = 0; a < 15; a = a + 1) {
line(image, Point(2 + a * a, 40), Point(318, 40 + a * a), Scalar(0, 255, 0),
2, 8);
}
// Draw text
putText(image, "C++ Drawing example", Point(18, 20), FONT_HERSHEY_SIMPLEX,
0.8, Scalar(255, 255, 255), 2);
// Show image
imshow("Drawing example", image);
// Wait for key press
waitKey();
return 0;
}
Copyright (c) Steven P. Goldsmith
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.