User Tools

Site Tools


Installing OpenCV4 on Ubuntu 18 and adding it to ROS

Author: Leonardo Georgescu, Dongbin Kim
Email: [email protected]
Date: Last Edited 07-02-2021 by Dongbin Kim
Keywords: Dynamixel, Gantry, Ubuntu 18, OpenCV, ROS

This tutorial shows how to install OpenCV in Ubuntu 18. The tutorial is based on this page which is in Korean Opencv Update.

Installation Guide

1. Check to see if OpenCV is already installed:

dasl@dasl-XPS-8900:</fc>~$ pkg-config --modversion opencv

2. If you see it already installed delete the previous version by executing the following commands:

dasl@dasl-XPS-8900:~$ sudo apt-get purge libopencv* python-opencv
dasl@dasl-XPS-8900:~$ sudo apt-get autoremove

3. Delete all the existing OpenCV libraries:

dasl@dasl-XPS-8900:~$ sudo find /usr/local/ -name "*opencv*" -exec rm {} \;

4. Update and upgrade packages on Ubuntu:

dasl@dasl-XPS-8900:~$ sudo apt-get update 
dasl@dasl-XPS-8900:~$ sudo apt-get upgrade

5. Install build-essential and cmake packages:

dasl@dasl-XPS-8900:~$ sudo apt-get install build-essential cmake

6. Install pkg-config:

dasl@dasl-XPS-8900:~$ sudo apt-get install pkg-config

7. Install packages for recording images in a certain format:

dasl@dasl-XPS-8900:~$ sudo apt-get install libjpeg-dev libtiff5-dev libpng-dev
dasl@dasl-XPS-8900:~$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libxvidcore-dev libx264-dev libxine2-dev

8. Install drivers to capture real-time video capture on Linux:

dasl@dasl-XPS-8900:~$ sudo apt-get install libv4l-dev v4l-utils

9. Install GStreamer:

dasl@dasl-XPS-8900:~$ sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

10. Use GUI to show images:

dasl@dasl-XPS-8900:~$ sudo apt-get install libgtk2.0-dev

11. Install OpenGL libraries:

dasl@dasl-XPS-8900:~$ sudo apt-get install mesa-utils libgl1-mesa-dri libgtkgl2.0-dev libgtkglext1-dev

12. Install libraries to optimize OpenCV:

dasl@dasl-XPS-8900:~$ sudo apt-get install libatlas-base-dev gfortran libeigen3-dev

13. Install Python dev packages:

dasl@dasl-XPS-8900:~$ sudo apt-get install python2.7-dev python3-dev python-numpy python3-numpy

14. Create OpenCV directory then move on:

dasl@dasl-XPS-8900:~$ mkdir opencv
dasl@dasl-XPS-8900:~$ cd opencv

15. Download and unzip OpenCV 4.2:

dasl@dasl-XPS-8900:~/opencv$ wget -O
dasl@dasl-XPS-8900:~/opencv$ unzip

16. Download and unzip OpenCV source code:

dasl@dasl-XPS-8900:~/opencv$ wget -O
dasl@dasl-XPS-8900:~/opencv$ unzip

17. Version “opencv-4.2.0” should show when checking to see it was installed correctly:

dasl@dasl-XPS-8900:~/opencv$ ls -d */

18. Create a build directory which takes care of compilation:

dasl@dasl-XPS-8900:~/opencv$ cd opencv-4.0.1/
dasl@dasl-XPS-8900:~/opencv/opencv-4.2.0$ mkdir build
dasl@dasl-XPS-8900:~/opencv/opencv-4.2.0$ cd build

19. Utilize 'cmake' to configure OpenCV complie settings

-D WITH_1394=OFF \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.2.0/modules \

if you see the message like below, you successfully finished configuration

  1. Configuring done
  2. Generating done
  3. Build files have been written to: /home/webnautes/opencv/opencv-4.2.0/build

You may have hard time doing the job here, if you are in trouble, please let us know, or look at the link the top of the tutorial. Everything is explained in Korean, but you will understand if you run “Translate this page” in Google Chrome

20. Installation should be successful. Then, check how many cores you have available on the computer:

dasl@dasl-XPS-8900:~$ lscpu

21. If you have more than 4 cores, then use the following command:

dasl@dasl-XPS-8900:~/opencv/opencv-4.2.0/build$ time make -j4

22. Install Output:

dasl@dasl-XPS-8900:~/opencv/opencv-4.2.0/build$ sudo make install

23. Check that “/usr/local/lib” is an output. If it's an output then work is all done.

dasl@dasl-XPS-8900:~/opencv/opencv-4.2.0/build$ cat /etc/*

24. Install CV Bridge which works between ROS and CV.

dasl@dasl-XPS-8900:~$ sudo apt-get install ros-melodic-cv-bridge

25. Install cheese to verity in ubuntu if your camera works. Running the commands below should allow you to view camera feedback. If it does not work, then it's an issue related to the drivers of your camera which need to be installed.

dasl@dasl-XPS-8900:~$ sudo apt-get install cheese
dasl@dasl-XPS-8900:~$ cheese

26. To view image on camera in ros:

dasl@dasl-XPS-8900:~$ sudo apt-get install ros-melodic-image-view

27. This will be used to publish the camera feedback. To be able to subscribe we must be able to publish the data. Instill the video stream package first:

dasl@dasl-XPS-8900:~$ sudo apt-get install ros-melodic-video-stream-opencv

28. Sometimes the camera feedback is too bright or if you want to change the values manually install the software below and run it.

dasl@dasl-XPS-8900:~$ sudo apt-get install guvcview
dasl@dasl-XPS-8900:~$ guvcview

29. Once the camera is calibrated, you can check

26. Adding OpenCV to ROS

installing_opencv4_on_ubuntu_18.txt · Last modified: 2021/07/02 23:17 by dongbinkim