Skip to content

Instantly share code, notes, and snippets.

@demid5111
Last active July 27, 2024 21:39
Show Gist options
  • Save demid5111/634a7b1a11b9522ac25f9584a9ef1ef8 to your computer and use it in GitHub Desktop.
Save demid5111/634a7b1a11b9522ac25f9584a9ef1ef8 to your computer and use it in GitHub Desktop.
Instructions for installing opencv on Ubuntu 18.04

Install OpenCV 4.5 on Ubuntu 18.04

Pre-configured OpenCV for Python from PyPi

  1. open the terminal
  2. check Python3 installation: python3 --version
  3. go to official website to learn details: https://pypi.org/project/opencv-python/
  4. choose the most complete package and run: python3 -m pip install opencv-contrib-python often you will not find the pip installed by default, therefore use the corresponding steps from the following instructions to install it (https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv/)
  5. check installation by entering the Python REPL: python3
  6. import the library: import cv2

Build OpenCV for Python and C++ from sources

  1. Check pre-requisites: cmake and the compilers gcc, g++ should be installed
  2. We will use the following link: https://docs.opencv.org/master/d2/de6/tutorial_py_setup_in_ubuntu.html as the basis for our instructions
  3. Let's open the most recent release of opencv to the date of this video capturing: https://github.com/opencv/opencv/releases/tag/4.5.1
  4. Create a tmp folder for all archives: mkdir ~/opencv4.5-tmp && cd ~/opencv4.5-tmp
  5. We need to download opencv sources: wget https://github.com/opencv/opencv/archive/4.5.1.zip -O opencv.zip
  6. We need to download opencv-contrib sources: wget https://github.com/opencv/opencv_contrib/archive/4.5.1.zip -O opencv_contrib.zip
  7. Unzip the opencv files: unzip opencv.zip
  8. Unzip the opencv-contrib files: unzip opencv_contrib.zip
  9. Move the files to simple directories: mv opencv-4.5.1/ opencv
  10. Move opencv-contrib files to simple directories: mv opencv_contrib-4.5.1/ opencv_contrib
  11. Make build directory: cd opencv && mkdir build && cd build
  12. Copy and run the following command. Install cmake if it is not available on the system.
    cmake -D CMAKE_BUILD_TYPE=DEBUG \
          -D CMAKE_INSTALL_PREFIX=~/opencv4.5-custom \
          -D OPENCV_EXTRA_MODULES_PATH=~/opencv4.5-tmp/opencv_contrib/modules \
          -D OPENCV_GENERATE_PKGCONFIG=ON \
          -D BUILD_EXAMPLES=ON ..
  13. Make the project:
    make -j4
    
  14. Install opencv:
    sudo make install
    
  15. Ensure that it is updated in the library storage:
    sudo ldconfig
    

Configure C++ project to work with opencv

  1. Open your editor of choice (VSCode in my case)
  2. Create a folder "~/projects/HelloOpenCV"
  3. Paste the main.cpp code to the main.cpp
  4. Firstly, let's try to compile our application with g++ as usual:
    g++ -Wall -std=c++11 -o main main.cpp
    
    We see that it cannot find our library headers
  5. For that we need to provide path to the headers and linker flags. The best way to find them all in one place is to use the utility module pkg-config. Remember we provided an additional argument to our cmake generation? So, let's execute the following:
    export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/home/parallels/opencv4.5-custom/lib/pkgconfig
    pkg-config --cflags --libs opencv4
    
  6. Add all flags to compilation command:
    g++ -Wall -std=c++11 -o main main.cpp $(pkg-config --cflags --libs opencv4)
    
    or a more explicit version for our particular sample:
    g++ -Wall -std=c++11 -o main main.cpp \
       -I/home/parallels/opencv4.5-custom/include/opencv4 \
       -L/home/parallels/opencv4.5-custom/lib \
       -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_core
    
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>
#include "iostream"
int showImage() {
std::string path = "img/test.jpeg";
cv::Mat img = cv::imread(path);
imshow("Portrait", img);
cv::waitKey(0);
return 0;
}
int showWebCameraContent() {
// open the first webcam plugged in the computer
cv::VideoCapture camera(0);
if (!camera.isOpened()) {
std::cerr << "ERROR: Could not open camera" << std::endl;
return 1;
}
// create a window to display the images from the webcam
cv::namedWindow("Webcam");
// this will contain the image from the webcam
cv::Mat frame;
// display the frame until you press a key
while (1) {
// capture the next frame from the webcam
camera >> frame;
// show the image on the window
cv::imshow("Webcam", frame);
// wait (10ms) for a key to be pressed
if (cv::waitKey(10) >= 0) {
break;
}
}
return 0;
}
int main(int, char**) {
showImage();
// showWebCameraContent();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment