1. Download

Download the two zip packages and unzip them.

2. Cmake

1
2
cd opencv-4.2.0
mkdir build && cd build

If you are simply using the C++ version of opencv for cpu, you can use the following cmake command

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/home/test/opt/opencv \
-D WITH_CUDA=OFF \
-D WITH_IPP=OFF \
-D OPENCV_EXTRA_MODULES_PATH=/home/test/opencv/opencv_contrib-4.2.0/modules \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_opencv_python3=OFF \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_hdf=OFF \
-D BUILD_EXAMPLES=ON ..

make -j48
make install

After installation, there will be an OpenCVConfig.cmake file in /home/test/opt/opencv/lib64/cmake/opencv4/ which tells cmake where to find the opencv libraries we compiled ourselves.

2.1 Adding to the system environment

Add the following variables to ~/.bashrc.

1
2
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/home/findhao/opt/opencv4.5.0/lib64/pkgconfig
export LD_LIBRARY_PATH=/home/findhao/opt/opencv4.5.0/lib64/:$LD_LIBRARY_PATH

3. Compile the program using opencv

smooth.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
using namespace cv;
/// Global Variables
int DELAY_CAPTION = 1500;
int DELAY_BLUR = 100;
int MAX_KERNEL_LENGTH = 31;
Mat src; Mat dst;
char window_name[] = "Smoothing Demo";
int main( int argc, char ** argv )
{
    /// Load the source image
    const char* filename = argc >=2 ? argv[1] : "lena.jpg";
    src = imread( "/home/test/tmp/test/lena.jpg", IMREAD_COLOR );
    if (src.empty())
    {
        printf(" Error opening image\n");
        printf(" Usage:\n %s [image_name-- default lena.jpg] \n", argv[0]);
        return EXIT_FAILURE;
    }
    GaussianBlur( src, dst, Size( 13, 13 ), 0, 0 );
    imwrite("out.jpg", dst);
    return 0;
}

CMakeList.txt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# cmake needs this line
cmake_minimum_required(VERSION 3.1)

# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

# Define project name
project(smooth)

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
set(OpenCV_DIR /home/test/opt/opencv/lib/cmake/opencv4/)
find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    config: ${OpenCV_DIR}")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

add_definitions("-Wall -g")
# Declare the executable target built from your sources
add_executable(smooth smooth.cpp)

# Link your application with OpenCV libraries
target_link_libraries(smooth PRIVATE ${OpenCV_LIBS})

Just compile the smooth program with cmake and run it.


Referenece ttps://www.findhao.net/academic/2570.html