Skip to content


Installing OpenCV 4.7.0 in Ubuntu 22.04 LTS

OpenCV is the most popular computer vision library in the world, widely used in research and industry for more than twenty years. The latest version available at the time of writing is 4.7.0, and it comes with many new features and bug fixes across all modules.

We’re also going to include the extra modules so that we have access to the latest research in other areas such as the RGBD module for depth cameras. If you’re planning to buy a new depth camera, make sure to check this post to help you decide which one to get.

We’re going to be using Ubuntu 22.04 LTS for this guide, but it should be similar for other versions of Ubuntu. At the end of this tutorial, you will be able to make computer vision applications with OpenCV using either C++ or Python, and the extra modules will be available to you.

Let’s begin. The first step is to make sure you have everything up to date:

sudo apt-get update
sudo apt-get upgrade

Now let’s grab some dependencies:

sudo apt-get install build-essential cmake python3-numpy python3-dev python3-tk libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libdc1394-dev libeigen3-dev libgtk-3-dev libvtk7-qt-dev

Time to grab the source code of OpenCV and the extra modules, configure it and compile it:

mkdir ~/opencv
cd ~/opencv
wget https://github.com/opencv/opencv/archive/refs/tags/4.7.0.tar.gz
tar -xvzf 4.7.0.tar.gz
rm 4.7.0.tar.gz
wget https://github.com/opencv/opencv_contrib/archive/refs/tags/4.7.0.tar.gz
tar -xvzf 4.7.0.tar.gz
rm 4.7.0.tar.gz
cd opencv-4.7.0
mkdir build
cd build
cmake -D WITH_TBB=ON -D BUILD_opencv_apps=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON .. -DCMAKE_BUILD_TYPE=RELEASE -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.7.0/modules
make -j`nproc`
sudo make install
echo '/usr/local/lib' | sudo tee --append /etc/ld.so.conf.d/opencv.conf
sudo ldconfig
echo 'PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig' | sudo tee --append ~/.bashrc
echo 'export PKG_CONFIG_PATH' | sudo tee --append ~/.bashrc
source ~/.bashrc

Now you should have OpenCV correctly installed, and you should be able to start building your computer vision applications in either C++ or Python. Let’s get you started with some examples.

We’re going to create a C++ application based on one of the examples included in the extra modules of OpenCV. It’s an application that generates Fine-Grained saliency based on this paper. And we’re going to use CMake to configure it so that you can use the same project structure later for your own applications. The first step is to grab the sample files:

cd ~
mkdir saliency
cd saliency
cp ../opencv/opencv_contrib-4.7.0/modules/saliency/samples/computeSaliency.cpp .
cp ../opencv/opencv-4.7.0/samples/data/Megamind.avi .

We’re now going to create a new text file in that same directory called CMakeLists.txt so that cmake can understand how to build it.

nano CMakeLists.txt

Paste this content into the file, save it(Ctrl+O) and then exit(Ctrl+X).

cmake_minimum_required (VERSION 3.0.2)
project (saliency)

find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
include_directories( . )

add_executable (${PROJECT_NAME} computeSaliency.cpp )
target_link_libraries (${PROJECT_NAME} ${OpenCV_LIBS})

And now we’re ready to build the application:

mkdir build
cd build
cmake ..
make

We can now run the application. It expects as arguments the type of saliency (FINE_GRAINED), a video file (../Megamind.avi), and a frame number to use (23):

./saliency FINE_GRAINED ../Megamind.avi 23
Original frame on the left, Fine-Grained Saliency calculated on the right.

You are now ready to start making your own computer vision application in C++.

Now let’s see how we can run a Python example as well:

mkdir ~/python_app
cd ~/python_app
cp ../opencv/opencv-4.7.0/samples/python/gaussian_mix.py .
python3 gaussian_mix.py
An example of the Expectation–Maximisation algorithm with a Gaussian Mixture Model in Python

Now you should be able to build your own computer vision applications, with either C++ or Python.

Enjoyed the tutorial?

Posted in Computer Vision, Open Source, OpenCV.

Tagged with , , , , , .


Overview of current Luxonis Oak cameras, or which one you should get for your next computer vision application

Microsoft stopped manufacturing the Kinect in 2017. Google stopped manufacturing Project Tango in 2018 in favour of ARCore. Intel started winding down their RealSense cameras in 2021 to focus on their main business. Thankfully, we have Luxonis stepping up and producing the next generation of perceptual cameras for robotics and computer vision applications.

Luxonis offers perceptual cameras for basically three types of applications: visible spectrum inference(VSI), VSI plus passive infrared stereo, and VSI plus active infrared stereo. Let’s see each one in detail. Note: I’m using referral links in this post, so if you buy something I might receive a commission.

Visible spectrum inference (VSI) cameras

These cameras have a single RGB sensor, just like a normal camera, but have the ability to perform optimised neural network inference inside the camera with a specialised chip such as the Myriad X VPU. For example if your application is to detect or classify objects, visual tracking, or anything related to doing inference in the visual spectrum, then this type of cameras should be enough for you. They have the least amount of power consumption, smallest size, and they’re also the cheapest ones.

An example of VSI, the object was classified as a Potted Plant with 99.51% certainty.

For this section the standard camera would be the Oak-1. If you don’t have any special requirements, this should be the one you get. There are some variants that you might want to consider in case you need something special: If you want to have a wider field of view, you can get the Oak-1 W. If you’re looking for higher resolution, then you should get the Oak-1 Max. If you’re on a budget, you can get the OAK-1-Lite Auto-Focus which is basically the same but with a cheaper sensor, and if you’re putting this on a vibrating environment, such as a drone for example, grab the OAK-1-Lite Fixed-Focus.

VSI plus passive infrared stereo cameras

These cameras can do everything that the VSI cameras can do but also come with two additional fast infrared sensors with global shutter that are used to estimate depth through disparity matching inside the camera. The nice thing about this is that your host device can simply use the depth information, all the heavy computation is done inside the camera itself. And of course you can also use those additional infrared sensors however you like, not only for depth estimation, but for example for applications where doing inference in the near-infrared spectrum would be more effective than VSI.

An example of depth estimation using passive infrared stereo. Whiter pixels are closer to the camera.

For this section the standard camera would be the Oak-D S2 Auto-Focus. If you don’t have any special requirements, this should be the one you get. There are some variants that you might want to consider in case you need something special. If you’re planning to use this on a small device like a Raspberry Pi, then you should either get the Oak-D(this was the original camera from the Kickstarter project) as it comes with a built-in barrel jack connector for external power(some devices like the Raspberry Pi cannot deliver the required power over USB only), or buy a separate Y-Adapter for your camera. If you’re putting the camera in a vibrating environment, such as a car for example, then you might want to get the Oak-D S2 Fixed-Focus, although note that this only affects the RGB camera. If you’re on a budget, or want to have a lighter camera by sacrificing some infrared resolution, you can get either the Oak-D Lite Auto-Focus, or the Oak-D Lite Fixed-Focus. Again, the auto or fixed focus only applies to the RGB camera.

VSI plus active infrared stereo cameras

These cameras can do everything that the VSI plus passive infrared stereo cameras can do but also come with an infrared laser dot projector for active stereo and an infrared illumination LED for night vision. This means that you will get a much better depth estimation for uniform areas such as walls for example, as passive stereo relies on matching the features of the scene only, but blank walls have very few distinctive features. By projecting a known pattern in infrared (invisible to the human eye and the RGB camera), the extra features in the infrared frames help the camera to get a better depth estimation in those cases.

Active stereo can produce better depth maps in certain cases. Image by luxonis

Another thing you can do with this camera is to use the infrared illumination LED to be able to “see in the dark” as the RGB camera(or your eyes) would not be able to see anything, but both infrared cameras would be completely illuminated. This is useful for applications that need to do inference in the near-infrared spectrum with no natural light.

When there’s no natural light, the infrared illumination LED makes IR images clear. Image by luxonis

For this section the standard camera would be the Oak-D Pro Auto-Focus. If you don’t have any special requirements, this should be the one you get. There are some variants that you might want to consider in case you need something special. If you’re planning to use the camera in a vibrating environment (such as a car for example), then you might want to grab the Oak-D Pro Fixed-Focus (note that the fixed focus only applies to the RGB camera). If you’re interested in a wider field of view, then you should get the Oak-D-Pro Wide.

Industrial usage

Finally, if you’re going to use these cameras in industrial applications, Luxonis offers them in a more ruggedised version and with a Power over Ethernet (PoE) connection. The features of the cameras are otherwise the same as already discussed. You can check the industrial version of these cameras here.

Conclusion

And that’s it, you should now be able to decide which Oak camera is best for your particular application. If you want some help starting out with the installation of the software, check out this post.

Enjoyed the article?

Posted in Computer Vision, IoT, Open Source, OpenCV.