This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Read our privacy policy

Setting Up the Environment and Testing OpenCV 4.4.0 on openEuler

Aug 13, 2021

After downloading and installing OpenCV 4.4.0 on openEuler, you need to set up the environment.

Setting Up the Environment

The following describes how to set up the environment.

  1. Add the library path. (Create the opencv.conf file.)

Run the following command:

vi /etc/ld.so.conf.d/opencv.conf

Add the following information to the file:

/usr/local/lib   // Path set during OpenCV installation

Save the settings and exit.

  1. Add environment variables.

Run the following command:

vi /etc/profile

Add the following information at the end of the file:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

Update environment variables.

source /etc/profile
  1. You can also use the following method for environment setup.

Run the following command:

vi /etc/bash.bashrc

Add the following information at the end of the file:

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

Update environment variables.

source  /etc/bash.bashrc
  1. Update the system database cache.

Run the following command:

ldconfig
  1. Check whether OpenCV is installed.

Run the following command:

pkg-config --cflags opencv // Header file path

-I/usr/local/include/opencv4

pkg-config --libs opencv // Installation library path

-L/usr/local/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core

pkg-config opencv --modversion

4.4.0

If the preceding information is returned, the installation is successful.

Demo Test

OpenCV integrates many test demos. You can run these demos to check whether OpenCV configuration is complete.

Run the following command to go to the samples folder:

cd opencv-4.4.0/samples

You can see samples in various languages.

android directx opencl swift winrt
CMakeLists.example.in dnn opengl tapi winrt_universal
CMakeLists.txt gpu openvx va_intel wp8
cpp hal python _winpack_build_sample.cmd
data java samples_utils.cmake _winpack_run_python_sample.cmd

Run the following command to access example_cmake of C++:

cd /cpp/example_cmake

If your host is not equipped with a camera, you may modify the example.cpp program before compilation.

The following is the test code for converting a color image to grayscale image.

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using std::cout;
using std::endl;
int main()
{
    std::string image_path = samples::findFile("test.jpg");
    Mat img = imread(image_path, IMREAD_COLOR);
    if(img.empty())
    {
        std::cout << "Could not read the image: " << image_path << std::endl;
        return 1;
    }
    Mat gray_img;
    cvtColor(img, gray_img, COLOR_BGR2GRAY);
    bool success = imwrite("test_gray.jpg", gray_img);
    cout << success << endl;
    return 0;
}

The images before and after the test are as follows:

test test_gray