1. 下载

https://github.com/opencv/opencv/releases

https://github.com/opencv/opencv_contrib/releases

下载两个压缩包解压出来。

2. Cmake

cd opencv-4.2.0
mkdir build && cd build

如果只是单纯使用cpu的C++版本的opencv,可以使用以下cmake命令

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

安装完成后,在/home/test/opt/opencv/lib/cmake/opencv4/会有一个OpenCVConfig.cmake文件,这个文件可以告诉cmake去哪里找我们自己编译的opencv库。

2.1 添加到系统环境

~/.bashrc中添加如下变量:

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/home/findhao/opt/opencv4.5.0/lib/pkgconfig
export LD_LIBRARY_PATH=/home/findhao/opt/opencv4.5.0/lib/:$LD_LIBRARY_PATH

3. 编译使用opencv的程序

smooth.cpp

#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

# 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})

cmake编译smooth程序运行即可。

Reference

opencv 源码编译安装

Ubuntu下多个版本OpenCV管理(Multiple Opencv version)

Ubuntu安装OpenCV


文章版权归 FindHao 所有丨本站默认采用CC-BY-NC-SA 4.0协议进行授权|
转载必须包含本声明,并以超链接形式注明作者 FindHao 和本文原始地址:
https://www.findhao.net/academic/2570.html

Comments

comments powered by Disqus