Skip to main content

Installing OpenCV on Manjaro

Let's talk about installing OpenCV on Manjaro Linux. OpenCV, which stands for Open Source Computer Vision Library, is a powerful tool for image and video analysis, offering a wide range of algorithms and real-time image processing capabilities. It's widely used in academia and industry for tasks such as facial recognition, object detection, and more. Manjaro, known for its simplicity and user-friendliness, is an excellent platform for developing with OpenCV.

In this tutorial, we'll walk through the process of setting up OpenCV on your Manjaro system, ensuring you have all the necessary tools to start experimenting with computer vision. We'll cover the installation via the package manager, building from source, and setting up the development environment. We'll also discuss some common pitfalls and how to avoid them.

Prerequisites

Before we begin, make sure you have the following:

  • A Manjaro Linux installation (any desktop environment will do)
  • Access to the terminal (you can find it in your applications menu)
  • Basic knowledge of using the terminal and package managers

Installation via Package Manager

The easiest way to install OpenCV on Manjaro is through the package manager, pacman. Manjaro's repositories are regularly updated with the latest versions of software, so you can expect a relatively recent version of OpenCV.

Step 1: Update Your System

First, ensure your system is up-to-date:

sudo pacman -Syu

Step 2: Install OpenCV

Now, install OpenCV and its extra modules:

sudo pacman -S opencv opencv-samples

The opencv-samples package includes a variety of example programs that demonstrate how to use OpenCV.

Step 3: Verify Installation

To verify that OpenCV is installed correctly, you can run one of the sample programs. For example:

opencv_version

You should see output similar to this:

version    4.5.1

Step 4: Test an Example

Let's run a simple OpenCV program to display an image:

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
cv::Mat image = cv::imread("path_to_your_image.jpg");
if (image.empty()) {
std::cout << "Error: Could not open or find the image" << std::endl;
return -1;
}

cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
cv::imshow("Display window", image);
cv::waitKey(0); // Wait for a keystroke in the window
return 0;
}

Compile this code using g++:

g++ `pkg-config --cflags --libs opencv4` example.cpp -o example

Run the compiled program:

./example

A window should pop up displaying the image you specified.

Building OpenCV from Source

If you need the latest features or want to customize the build options, you can compile OpenCV from source.

Step 1: Install Dependencies

Install the necessary dependencies:

sudo pacman -S base-devel cmake git pkg-config numpy

Step 2: Download OpenCV

Clone the OpenCV repository:

cd ~
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout tags/4.5.1

Step 3: Build OpenCV

Create a build directory and generate the Makefile:

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

Now, compile and install OpenCV:

make -j$(nproc)
sudo make install

Step 4: Test the Installation

Repeat the steps from the package manager installation to verify that OpenCV is working correctly.

Setting Up the Development Environment

For development purposes, you might want to set up an IDE or a code editor like Visual Studio Code or CLion. Ensure that you configure the build tasks to include the OpenCV include directories and libraries.

Example: Configuring Visual Studio Code

  1. Install Visual Studio Code.
  2. Open your project folder in VSCode.
  3. Create a tasks.json file in the .vscode directory with the following content:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with g++",
"type": "shell",
"command": "g++",
"args": [
`pkg-config --cflags --libs opencv4`,
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always"
},
"problemMatcher": "$gcc"
}
]
}
  1. Create a launch.json file in the .vscode directory for debugging:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build with g++",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
  1. Write your OpenCV code, build, and debug within VSCode.

Common Pitfalls

  • Incorrect paths: Ensure that the paths to images or videos in your code are correct.
  • Missing dependencies: If you encounter issues during the build process, you may be missing some dependencies. Install them and rebuild.
  • Version mismatch: Make sure that the OpenCV version you're using is compatible with the code examples you're following.

You now have OpenCV installed on your Manjaro system, whether you chose the simplicity of the package manager or the customizability of building from source. With your development environment set up, you're ready to dive into the world of computer vision.