Solved: how to complie opencv c++ in ubuntu

OpenCV is an open-source library that includes several hundreds of computer vision algorithms. You can use it to process images and videos to detect faces, identify objects, classify human actions in videos, track camera movements, track moving objects, etc. The library has more than 2500 optimized algorithms, which is a comprehensive set for computer vision tasks.

To compile OpenCV C++ in Ubuntu, you need to follow specific steps which are outlined below.

Prerequisites and Preparation

Installing OpenCV on Ubuntu requires a development environment. The development environment is the combination of software and settings that you use to create applications.

First, you need to install a compiler that supports C++. For Ubuntu, the compiler is GCC, which you can install via the Ubuntu Software Center. You also need a software library that supports GUI (graphical user interface), such as GTK.

Secondly, you need to install prerequisites libraries necessary for OpenCV:

  • Libavcodec
  • Libavformat
  • Libswscale

These libraries provide multimedia support, allowing you to read video files and images in different formats.

sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libswscale-dev

Download and Install OpenCV

Download OpenCV from the official site. Extract it and create a new directory ‘build’ within the extracted OpenCV directory.

cd ~/Downloads/opencv-xx.xx.x/
mkdir build
cd build

Now, you can compile and install OpenCV.

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install

Writing and Running OpenCV Code

Here is a simple C++ code example which loads an image, and displays it:

#include
#include

int main()
{
cv::Mat img = cv::imread(“image.jpg”,1);
cv::namedWindow( “Window”, cv::WINDOW_NORMAL);
cv::imshow(“Window”, img);
cv::waitKey(0);
return 0;
}

To compile and run your OpenCV code, you can use the following command:

g++ `pkg-config –cflags –libs opencv` example.cpp -o example
./example

Now you are ready to compile, run and develop OpenCV C++ applications on your Ubuntu.

Understanding the Code

The imread() function reads the image file from a specified location and stores it in the ‘img’ Mat object. The namedWindow() function creates a window where the image will be displayed. The imshow() function displays the image in the named window. waitKey(0) waits for the user to press any key. This is necessary to keep the program from ending immediately.

I hope the article is helpful in getting started and working swiftly with OpenCV in the Ubuntu environment. Keep practicing and exploring more functions and features of OpenCV.

Tips and Troubleshooting

Sometimes, you may encounter issues while compiling or running the code. Here are some common issues and their possible solutions:

Error during make: Make sure all the prerequisite libraries are installed correctly. Check the version compatibility.

Error opening file in imread(): Make sure the image file is in the correct location and the file path is specified accurately in the ‘imread()’ function.

Remember, practice and persistence are key when venturing into new territories such as compiling OpenCV C++ in Ubuntu. Keep exploring and happy coding!

Related posts:

Leave a Comment