1、前置工作
先安装相关依赖
sudo apt install -y cmake g++ wget unzip
创建安装目录并进入目录
cd /root
mkdir opencv_install_package
下载opencv4.8.0的源代码 , --no-check-certificate
表示禁用ssl/tls证书验证
cd /root/opencv_install_package
wget --no-check-certificate -O opencv.zip https://github.com/opencv/opencv/archive/4.8.0.zip
解压源代码
unzip opencv.zip
创建构建目录
mkdir -p /root/opencv_install_package/build
创建安装目录
mkdir -p /usr/local/opencv4.8.0
2、生成Makefile
设置安装目录为 /usr/local/opencv4.8.0
cd /root/opencv_install_package/build
cmake ../opencv-4.8.0 -DCMAKE_INSTALL_PREFIX=/usr/local/opencv4.8.0
结果展示以下信息表示成功
root@PAw9033927:~/opencv_install_package/build# cmake ../opencv-4.8.0 -DCMAKE_INSTALL_PREFIX=/usr/local/opencv4.8.0
-- 'Release' build type is used by default. Use CMAKE_BUILD_TYPE to specify build type (Release or Debug)
-- The CXX compiler identification is GNU 11.4.0
-- The C compiler identification is GNU 11.4.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- ocv_init_download: OpenCV source tree is not fetched as git repository. 3rdparty resources will be downloaded from github.com by default.
-- Detected processor: x86_64
-- Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE) (Required is at least version "2.7")
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.10.12", minimum required is "3.2")
-- Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is exact version "3.10.12")
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
-- Looking for ccache - not found
-- Performing Test HAVE_CXX_FSIGNED_CHAR
-- Performing Test HAVE_CXX_FSIGNED_CHAR - Success
-- Performing Test HAVE_C_FSIGNED_CHAR
-- Performing Test HAVE_C_FSIGNED_CHAR - Success
-- Performing Test HAVE_CXX_W
-- Performing Test HAVE_CXX_W - Success
-- Performing Test HAVE_C_W
-- Performing Test HAVE_C_W - Success
-- Performing Test HAVE_CXX_WALL
-- Performing Test HAVE_CXX_WALL - Success
-- Performing Test HAVE_C_WALL
-- Performing Test HAVE_C_WALL - Success
············ 中间部分省略
-- highgui: using builtin backend: NONE
-- Found 'misc' Python modules from /root/opencv_install_package/opencv-4.8.0/modules/python/package/extra_modules
-- Found 'mat_wrapper;utils' Python modules from /root/opencv_install_package/opencv-4.8.0/modules/core/misc/python/package
-- Found 'gapi' Python modules from /root/opencv_install_package/opencv-4.8.0/modules/gapi/misc/python/package
--
-- General configuration for OpenCV 4.8.0 =====================================
-- Version control: unknown
--
-- Platform:
-- Timestamp: 2024-06-18T08:08:59Z
-- Host: Linux 4.4.0-19041-Microsoft x86_64
-- CMake: 3.23.0
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/gmake
-- Configuration: Release
--
-- CPU/HW features:
-- Baseline: SSE SSE2 SSE3
-- requested: SSE3
-- Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
-- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
-- SSE4_1 (18 files): + SSSE3 SSE4_1
-- SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
-- FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
-- AVX (8 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
-- AVX2 (37 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
-- AVX512_SKX (8 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ standard: 11
-- C++ Compiler: /usr/bin/c++ (ver 11.4.0)
-- C++ flags (Release): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /usr/bin/cc
-- C flags (Release): -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
-- Linker flags (Debug): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
-- ccache: NO
-- Precompiled headers: NO
-- Extra dependencies: dl m pthread rt
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching ts video videoio
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: java python2 python3
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: NO
--
-- GUI: NONE
-- GTK+: NO
-- VTK support: NO
--
-- Media I/O:
-- ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
-- JPEG: libjpeg-turbo (ver 2.1.3-62)
-- WEBP: build (ver encoder: 0x020f)
-- PNG: build (ver 1.6.37)
-- TIFF: build (ver 42 - 4.2.0)
-- JPEG 2000: build (ver 2.5.0)
-- OpenEXR: build (ver 2.3.0)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: NO
-- FFMPEG: NO
-- avcodec: NO
-- avformat: NO
-- avutil: NO
-- swscale: NO
-- avresample: NO
-- GStreamer: NO
-- v4l/v4l2: YES (linux/videodev2.h)
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Intel IPP: 2021.8 [2021.8.0]
-- at: /root/opencv_install_package/build/3rdparty/ippicv/ippicv_lnx/icv
-- Intel IPP IW: sources (2021.8.0)
-- at: /root/opencv_install_package/build/3rdparty/ippicv/ippicv_lnx/iw
-- VA: NO
-- Lapack: NO
-- Eigen: NO
-- Custom HAL: NO
-- Protobuf: build (3.19.1)
-- Flatbuffers: builtin/3rdparty (23.5.9)
--
-- OpenCL: YES (no extra features)
-- Include path: /root/opencv_install_package/opencv-4.8.0/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python (for build): /usr/bin/python3
--
-- Java:
-- ant: NO
-- Java: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local/opencv4.8.0
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /root/opencv_install_package/build
3、构建opencv(编译)
cmake --build .
结尾处显示 100% 就表示成功
····省略前面部分
[100%] Built target opencv_interactive-calibration
[100%] Building CXX object apps/version/CMakeFiles/opencv_version.dir/opencv_version.cpp.o
[100%] Linking CXX executable ../../bin/opencv_version
[100%] Built target opencv_version
[100%] Building CXX object apps/model-diagnostics/CMakeFiles/opencv_model_diagnostics.dir/model_diagnostics.cpp.o
[100%] Linking CXX executable ../../bin/opencv_model_diagnostics
[100%] Built target opencv_model_diagnostics
检查构建结果
root@PAw9033927:~/opencv_install_package/build# ll lib
total 192384
drwxr-xr-x 1 root root 4096 Jun 18 16:41 ./
drwxr-xr-x 1 root root 4096 Jun 18 17:09 ../
lrwxrwxrwx 1 root root 24 Jun 18 16:35 libopencv_calib3d.so -> libopencv_calib3d.so.408*
-rwxr-xr-x 1 root root 2655136 Jun 18 16:35 libopencv_calib3d.so.4.8.0*
lrwxrwxrwx 1 root root 26 Jun 18 16:35 libopencv_calib3d.so.408 -> libopencv_calib3d.so.4.8.0*
lrwxrwxrwx 1 root root 21 Jun 18 16:14 libopencv_core.so -> libopencv_core.so.408*
-rwxr-xr-x 1 root root 15348008 Jun 18 16:14 libopencv_core.so.4.8.0*
lrwxrwxrwx 1 root root 23 Jun 18 16:14 libopencv_core.so.408 -> libopencv_core.so.4.8.0*
lrwxrwxrwx 1 root root 20 Jun 18 16:31 libopencv_dnn.so -> libopencv_dnn.so.408*
-rwxr-xr-x 1 root root 8261376 Jun 18 16:31 libopencv_dnn.so.4.8.0*
lrwxrwxrwx 1 root root 22 Jun 18 16:31 libopencv_dnn.so.408 -> libopencv_dnn.so.4.8.0*
lrwxrwxrwx 1 root root 27 Jun 18 16:33 libopencv_features2d.so -> libopencv_features2d.so.408*
-rwxr-xr-x 1 root root 1079424 Jun 18 16:33 libopencv_features2d.so.4.8.0*
lrwxrwxrwx 1 root root 29 Jun 18 16:33 libopencv_features2d.so.408 -> libopencv_features2d.so.4.8.0*
lrwxrwxrwx 1 root root 22 Jun 18 16:24 libopencv_flann.so -> libopencv_flann.so.408*
-rwxr-xr-x 1 root root 720216 Jun 18 16:24 libopencv_flann.so.4.8.0*
lrwxrwxrwx 1 root root 24 Jun 18 16:24 libopencv_flann.so.408 -> libopencv_flann.so.4.8.0*
lrwxrwxrwx 1 root root 21 Jun 18 16:41 libopencv_gapi.so -> libopencv_gapi.so.408*
-rwxr-xr-x 1 root root 5966968 Jun 18 16:41 libopencv_gapi.so.4.8.0*
lrwxrwxrwx 1 root root 23 Jun 18 16:41 libopencv_gapi.so.408 -> libopencv_gapi.so.4.8.0*
root@PAw9033927:~/opencv_install_package/build# ll bin
total 180264
drwxr-xr-x 1 root root 4096 Jun 18 16:48 ./
drwxr-xr-x 1 root root 4096 Jun 18 17:09 ../
-rwxr-xr-x 1 root root 34368 Jun 18 16:48 opencv_annotation*
-rwxr-xr-x 1 root root 177712 Jun 18 16:48 opencv_interactive-calibration*
-rwxr-xr-x 1 root root 22912 Jun 18 16:48 opencv_model_diagnostics*
-rwxr-xr-x 1 root root 1126552 Jun 18 16:36 opencv_perf_calib3d*
-rwxr-xr-x 1 root root 5015904 Jun 18 16:24 opencv_perf_core*
-rwxr-xr-x 1 root root 1530112 Jun 18 16:33 opencv_perf_dnn*
-rwxr-xr-x 1 root root 1145936 Jun 18 16:34 opencv_perf_features2d*
-rwxr-xr-x 1 root root 6368168 Jun 18 16:48 opencv_perf_gapi*
-rwxr-xr-x 1 root root 552496 Jun 18 16:34 opencv_perf_imgcodecs*
-rwxr-xr-x 1 root root 6439432 Jun 18 16:27 opencv_perf_imgproc*
-rwxr-xr-x 1 root root 1138144 Jun 18 16:37 opencv_perf_objdetect*
-rwxr-xr-x 1 root root 746128 Jun 18 16:28 opencv_perf_photo*
-rwxr-xr-x 1 root root 1287624 Jun 18 16:38 opencv_perf_stitching*
-rwxr-xr-x 1 root root 1664888 Jun 18 16:39 opencv_perf_video*
-rwxr-xr-x 1 root root 692568 Jun 18 16:34 opencv_perf_videoio*
-rwxr-xr-x 1 root root 3026192 Jun 18 16:36 opencv_test_calib3d*
-rwxr-xr-x 1 root root 20886360 Jun 18 16:23 opencv_test_core*
-rwxr-xr-x 1 root root 7604488 Jun 18 16:32 opencv_test_dnn*
-rwxr-xr-x 1 root root 1708808 Jun 18 16:33 opencv_test_features2d*
-rwxr-xr-x 1 root root 676808 Jun 18 16:24 opencv_test_flann*
-rwxr-xr-x 1 root root 17514072 Jun 18 16:46 opencv_test_gapi*
-rwxr-xr-x 1 root root 503296 Jun 18 16:36 opencv_test_highgui*
-rwxr-xr-x 1 root root 1700568 Jun 18 16:34 opencv_test_imgcodecs*
-rwxr-xr-x 1 root root 8080040 Jun 18 16:26 opencv_test_imgproc*
-rwxr-xr-x 1 root root 1223200 Jun 18 16:28 opencv_test_ml*
-rwxr-xr-x 1 root root 1628464 Jun 18 16:37 opencv_test_objdetect*
-rwxr-xr-x 1 root root 951192 Jun 18 16:28 opencv_test_photo*
-rwxr-xr-x 1 root root 850448 Jun 18 16:38 opencv_test_stitching*
-rwxr-xr-x 1 root root 1418800 Jun 18 16:38 opencv_test_video*
-rwxr-xr-x 1 root root 1880576 Jun 18 16:34 opencv_test_videoio*
-rwxr-xr-x 1 root root 42744 Jun 18 16:48 opencv_version*
-rwxr-xr-x 1 root root 55256 Jun 18 16:48 opencv_visualisation*
root@PAw9033927:~/opencv_install_package/build# ll | grep .cmake
-rw-r--r-- 1 root root 9522 Jun 18 16:08 CPackConfig.cmake
-rw-r--r-- 1 root root 9934 Jun 18 16:08 CPackSourceConfig.cmake
-rw-r--r-- 1 root root 2025 Jun 18 16:08 CTestTestfile.cmake
-rw-r--r-- 1 root root 418 Jun 18 16:08 OpenCVConfig-version.cmake
-rw-r--r-- 1 root root 15818 Jun 18 16:08 OpenCVConfig.cmake
-rw-r--r-- 1 root root 10695 Jun 18 16:09 OpenCVModules.cmake
-rw-r--r-- 1 root root 12239 Jun 18 16:08 cmake_install.cmake
-rw-r--r-- 1 root root 1204 Jun 18 16:08 cmake_uninstall.cmake
-rw-r--r-- 1 root root 2677 Jun 18 16:08 opencv_python_config.cmake
4、安装
安装到用户级别(可选)
cmake --install .
安装到系统级别
make install
5、验证安装结果
在build目录下执行以下命令
root@PAw9033927:~/opencv_install_package/build# ./bin/opencv_version
4.8.0
通过cmake项目 + cpp代码进行验证安装结果
在 home 目录下创建 cmake_opencv_demo
目录,在该目录下创建 CMakeLists.txt
文件,内容如下:
cmake_minimum_required(VERSION 3.15)
project(opencv_demo LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(OpenCV_DIR /root/opencv_install_package/build)
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV_LIBS: ${OpenCV_LIBS}")
add_executable(opencv_demo main.cpp)
target_include_directories(opencv_demo PRIVATE ${OpenCV_INCLUDE_DIRS})
target_link_libraries(opencv_demo ${OpenCV_LIBS})
在创建一个 main.cpp文件
#include <iostream>
#include <opencv2/opencv.hpp>
int main() {
std::cout << "OpenCV version: " << CV_VERSION << std::endl;
return 0;
}
然后,在cmake_opencv_demo
目录下执行以下命令来构建c++项目,
# -S 指定源码位置, -B 指定构建输出目录,若目录不存在,会自动创建
cmake -S . -B build
执行以下命令生成可执行文件
root@PAw9033927:/home/cmake_opencv_demo# cmake --build build
[ 50%] Building CXX object CMakeFiles/opencv_demo.dir/main.cpp.o
[100%] Linking CXX executable opencv_demo
[100%] Built target opencv_demo
执行
root@PAw9033927:/home/cmake_opencv_demo# cd build/
root@PAw9033927:/home/cmake_opencv_demo/build# pwd
/home/cmake_opencv_demo/build
root@PAw9033927:/home/cmake_opencv_demo/build# ./opencv_demo
OpenCV version: 4.8.0