Ubuntu22.04安装opencv 4.8.0版本

发布时间:2024-06-18 17:34:19 作者:yexindonglai@163.com 阅读(480)

1、前置工作

先安装相关依赖

  1. sudo apt install -y cmake g++ wget unzip

创建安装目录并进入目录

  1. cd /root
  2. mkdir opencv_install_package

下载opencv4.8.0的源代码 , --no-check-certificate 表示禁用ssl/tls证书验证

  1. cd /root/opencv_install_package
  2. wget --no-check-certificate -O opencv.zip https://github.com/opencv/opencv/archive/4.8.0.zip

解压源代码

  1. unzip opencv.zip

创建构建目录

  1. mkdir -p /root/opencv_install_package/build

创建安装目录

  1. mkdir -p /usr/local/opencv4.8.0

2、生成Makefile

设置安装目录为 /usr/local/opencv4.8.0

  1. cd /root/opencv_install_package/build
  2. cmake ../opencv-4.8.0 -DCMAKE_INSTALL_PREFIX=/usr/local/opencv4.8.0

结果展示以下信息表示成功

  1. root@PAw9033927:~/opencv_install_package/build# cmake ../opencv-4.8.0 -DCMAKE_INSTALL_PREFIX=/usr/local/opencv4.8.0
  2. -- 'Release' build type is used by default. Use CMAKE_BUILD_TYPE to specify build type (Release or Debug)
  3. -- The CXX compiler identification is GNU 11.4.0
  4. -- The C compiler identification is GNU 11.4.0
  5. -- Detecting CXX compiler ABI info
  6. -- Detecting CXX compiler ABI info - done
  7. -- Check for working CXX compiler: /usr/bin/c++ - skipped
  8. -- Detecting CXX compile features
  9. -- Detecting CXX compile features - done
  10. -- Detecting C compiler ABI info
  11. -- Detecting C compiler ABI info - done
  12. -- Check for working C compiler: /usr/bin/cc - skipped
  13. -- Detecting C compile features
  14. -- Detecting C compile features - done
  15. -- ocv_init_download: OpenCV source tree is not fetched as git repository. 3rdparty resources will be downloaded from github.com by default.
  16. -- Detected processor: x86_64
  17. -- Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE) (Required is at least version "2.7")
  18. -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.10.12", minimum required is "3.2")
  19. -- Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is exact version "3.10.12")
  20. Traceback (most recent call last):
  21. File "<string>", line 1, in <module>
  22. ModuleNotFoundError: No module named 'numpy'
  23. -- Looking for ccache - not found
  24. -- Performing Test HAVE_CXX_FSIGNED_CHAR
  25. -- Performing Test HAVE_CXX_FSIGNED_CHAR - Success
  26. -- Performing Test HAVE_C_FSIGNED_CHAR
  27. -- Performing Test HAVE_C_FSIGNED_CHAR - Success
  28. -- Performing Test HAVE_CXX_W
  29. -- Performing Test HAVE_CXX_W - Success
  30. -- Performing Test HAVE_C_W
  31. -- Performing Test HAVE_C_W - Success
  32. -- Performing Test HAVE_CXX_WALL
  33. -- Performing Test HAVE_CXX_WALL - Success
  34. -- Performing Test HAVE_C_WALL
  35. -- Performing Test HAVE_C_WALL - Success
  36. ············ 中间部分省略
  37. -- highgui: using builtin backend: NONE
  38. -- Found 'misc' Python modules from /root/opencv_install_package/opencv-4.8.0/modules/python/package/extra_modules
  39. -- Found 'mat_wrapper;utils' Python modules from /root/opencv_install_package/opencv-4.8.0/modules/core/misc/python/package
  40. -- Found 'gapi' Python modules from /root/opencv_install_package/opencv-4.8.0/modules/gapi/misc/python/package
  41. --
  42. -- General configuration for OpenCV 4.8.0 =====================================
  43. -- Version control: unknown
  44. --
  45. -- Platform:
  46. -- Timestamp: 2024-06-18T08:08:59Z
  47. -- Host: Linux 4.4.0-19041-Microsoft x86_64
  48. -- CMake: 3.23.0
  49. -- CMake generator: Unix Makefiles
  50. -- CMake build tool: /usr/bin/gmake
  51. -- Configuration: Release
  52. --
  53. -- CPU/HW features:
  54. -- Baseline: SSE SSE2 SSE3
  55. -- requested: SSE3
  56. -- Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
  57. -- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
  58. -- SSE4_1 (18 files): + SSSE3 SSE4_1
  59. -- SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
  60. -- FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
  61. -- AVX (8 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
  62. -- AVX2 (37 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
  63. -- AVX512_SKX (8 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
  64. --
  65. -- C/C++:
  66. -- Built as dynamic libs?: YES
  67. -- C++ standard: 11
  68. -- C++ Compiler: /usr/bin/c++ (ver 11.4.0)
  69. -- 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
  70. -- 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
  71. -- C Compiler: /usr/bin/cc
  72. -- 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
  73. -- 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
  74. -- Linker flags (Release): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
  75. -- Linker flags (Debug): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
  76. -- ccache: NO
  77. -- Precompiled headers: NO
  78. -- Extra dependencies: dl m pthread rt
  79. -- 3rdparty dependencies:
  80. --
  81. -- OpenCV modules:
  82. -- To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching ts video videoio
  83. -- Disabled: world
  84. -- Disabled by dependency: -
  85. -- Unavailable: java python2 python3
  86. -- Applications: tests perf_tests apps
  87. -- Documentation: NO
  88. -- Non-free algorithms: NO
  89. --
  90. -- GUI: NONE
  91. -- GTK+: NO
  92. -- VTK support: NO
  93. --
  94. -- Media I/O:
  95. -- ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
  96. -- JPEG: libjpeg-turbo (ver 2.1.3-62)
  97. -- WEBP: build (ver encoder: 0x020f)
  98. -- PNG: build (ver 1.6.37)
  99. -- TIFF: build (ver 42 - 4.2.0)
  100. -- JPEG 2000: build (ver 2.5.0)
  101. -- OpenEXR: build (ver 2.3.0)
  102. -- HDR: YES
  103. -- SUNRASTER: YES
  104. -- PXM: YES
  105. -- PFM: YES
  106. --
  107. -- Video I/O:
  108. -- DC1394: NO
  109. -- FFMPEG: NO
  110. -- avcodec: NO
  111. -- avformat: NO
  112. -- avutil: NO
  113. -- swscale: NO
  114. -- avresample: NO
  115. -- GStreamer: NO
  116. -- v4l/v4l2: YES (linux/videodev2.h)
  117. --
  118. -- Parallel framework: pthreads
  119. --
  120. -- Trace: YES (with Intel ITT)
  121. --
  122. -- Other third-party libraries:
  123. -- Intel IPP: 2021.8 [2021.8.0]
  124. -- at: /root/opencv_install_package/build/3rdparty/ippicv/ippicv_lnx/icv
  125. -- Intel IPP IW: sources (2021.8.0)
  126. -- at: /root/opencv_install_package/build/3rdparty/ippicv/ippicv_lnx/iw
  127. -- VA: NO
  128. -- Lapack: NO
  129. -- Eigen: NO
  130. -- Custom HAL: NO
  131. -- Protobuf: build (3.19.1)
  132. -- Flatbuffers: builtin/3rdparty (23.5.9)
  133. --
  134. -- OpenCL: YES (no extra features)
  135. -- Include path: /root/opencv_install_package/opencv-4.8.0/3rdparty/include/opencl/1.2
  136. -- Link libraries: Dynamic load
  137. --
  138. -- Python (for build): /usr/bin/python3
  139. --
  140. -- Java:
  141. -- ant: NO
  142. -- Java: NO
  143. -- JNI: NO
  144. -- Java wrappers: NO
  145. -- Java tests: NO
  146. --
  147. -- Install to: /usr/local/opencv4.8.0
  148. -- -----------------------------------------------------------------
  149. --
  150. -- Configuring done
  151. -- Generating done
  152. -- Build files have been written to: /root/opencv_install_package/build

3、构建opencv(编译)

  1. cmake --build .

结尾处显示 100% 就表示成功

  1. ····省略前面部分
  2. [100%] Built target opencv_interactive-calibration
  3. [100%] Building CXX object apps/version/CMakeFiles/opencv_version.dir/opencv_version.cpp.o
  4. [100%] Linking CXX executable ../../bin/opencv_version
  5. [100%] Built target opencv_version
  6. [100%] Building CXX object apps/model-diagnostics/CMakeFiles/opencv_model_diagnostics.dir/model_diagnostics.cpp.o
  7. [100%] Linking CXX executable ../../bin/opencv_model_diagnostics
  8. [100%] Built target opencv_model_diagnostics

检查构建结果

  1. root@PAw9033927:~/opencv_install_package/build# ll lib
  2. total 192384
  3. drwxr-xr-x 1 root root 4096 Jun 18 16:41 ./
  4. drwxr-xr-x 1 root root 4096 Jun 18 17:09 ../
  5. lrwxrwxrwx 1 root root 24 Jun 18 16:35 libopencv_calib3d.so -> libopencv_calib3d.so.408*
  6. -rwxr-xr-x 1 root root 2655136 Jun 18 16:35 libopencv_calib3d.so.4.8.0*
  7. lrwxrwxrwx 1 root root 26 Jun 18 16:35 libopencv_calib3d.so.408 -> libopencv_calib3d.so.4.8.0*
  8. lrwxrwxrwx 1 root root 21 Jun 18 16:14 libopencv_core.so -> libopencv_core.so.408*
  9. -rwxr-xr-x 1 root root 15348008 Jun 18 16:14 libopencv_core.so.4.8.0*
  10. lrwxrwxrwx 1 root root 23 Jun 18 16:14 libopencv_core.so.408 -> libopencv_core.so.4.8.0*
  11. lrwxrwxrwx 1 root root 20 Jun 18 16:31 libopencv_dnn.so -> libopencv_dnn.so.408*
  12. -rwxr-xr-x 1 root root 8261376 Jun 18 16:31 libopencv_dnn.so.4.8.0*
  13. lrwxrwxrwx 1 root root 22 Jun 18 16:31 libopencv_dnn.so.408 -> libopencv_dnn.so.4.8.0*
  14. lrwxrwxrwx 1 root root 27 Jun 18 16:33 libopencv_features2d.so -> libopencv_features2d.so.408*
  15. -rwxr-xr-x 1 root root 1079424 Jun 18 16:33 libopencv_features2d.so.4.8.0*
  16. lrwxrwxrwx 1 root root 29 Jun 18 16:33 libopencv_features2d.so.408 -> libopencv_features2d.so.4.8.0*
  17. lrwxrwxrwx 1 root root 22 Jun 18 16:24 libopencv_flann.so -> libopencv_flann.so.408*
  18. -rwxr-xr-x 1 root root 720216 Jun 18 16:24 libopencv_flann.so.4.8.0*
  19. lrwxrwxrwx 1 root root 24 Jun 18 16:24 libopencv_flann.so.408 -> libopencv_flann.so.4.8.0*
  20. lrwxrwxrwx 1 root root 21 Jun 18 16:41 libopencv_gapi.so -> libopencv_gapi.so.408*
  21. -rwxr-xr-x 1 root root 5966968 Jun 18 16:41 libopencv_gapi.so.4.8.0*
  22. lrwxrwxrwx 1 root root 23 Jun 18 16:41 libopencv_gapi.so.408 -> libopencv_gapi.so.4.8.0*
  1. root@PAw9033927:~/opencv_install_package/build# ll bin
  2. total 180264
  3. drwxr-xr-x 1 root root 4096 Jun 18 16:48 ./
  4. drwxr-xr-x 1 root root 4096 Jun 18 17:09 ../
  5. -rwxr-xr-x 1 root root 34368 Jun 18 16:48 opencv_annotation*
  6. -rwxr-xr-x 1 root root 177712 Jun 18 16:48 opencv_interactive-calibration*
  7. -rwxr-xr-x 1 root root 22912 Jun 18 16:48 opencv_model_diagnostics*
  8. -rwxr-xr-x 1 root root 1126552 Jun 18 16:36 opencv_perf_calib3d*
  9. -rwxr-xr-x 1 root root 5015904 Jun 18 16:24 opencv_perf_core*
  10. -rwxr-xr-x 1 root root 1530112 Jun 18 16:33 opencv_perf_dnn*
  11. -rwxr-xr-x 1 root root 1145936 Jun 18 16:34 opencv_perf_features2d*
  12. -rwxr-xr-x 1 root root 6368168 Jun 18 16:48 opencv_perf_gapi*
  13. -rwxr-xr-x 1 root root 552496 Jun 18 16:34 opencv_perf_imgcodecs*
  14. -rwxr-xr-x 1 root root 6439432 Jun 18 16:27 opencv_perf_imgproc*
  15. -rwxr-xr-x 1 root root 1138144 Jun 18 16:37 opencv_perf_objdetect*
  16. -rwxr-xr-x 1 root root 746128 Jun 18 16:28 opencv_perf_photo*
  17. -rwxr-xr-x 1 root root 1287624 Jun 18 16:38 opencv_perf_stitching*
  18. -rwxr-xr-x 1 root root 1664888 Jun 18 16:39 opencv_perf_video*
  19. -rwxr-xr-x 1 root root 692568 Jun 18 16:34 opencv_perf_videoio*
  20. -rwxr-xr-x 1 root root 3026192 Jun 18 16:36 opencv_test_calib3d*
  21. -rwxr-xr-x 1 root root 20886360 Jun 18 16:23 opencv_test_core*
  22. -rwxr-xr-x 1 root root 7604488 Jun 18 16:32 opencv_test_dnn*
  23. -rwxr-xr-x 1 root root 1708808 Jun 18 16:33 opencv_test_features2d*
  24. -rwxr-xr-x 1 root root 676808 Jun 18 16:24 opencv_test_flann*
  25. -rwxr-xr-x 1 root root 17514072 Jun 18 16:46 opencv_test_gapi*
  26. -rwxr-xr-x 1 root root 503296 Jun 18 16:36 opencv_test_highgui*
  27. -rwxr-xr-x 1 root root 1700568 Jun 18 16:34 opencv_test_imgcodecs*
  28. -rwxr-xr-x 1 root root 8080040 Jun 18 16:26 opencv_test_imgproc*
  29. -rwxr-xr-x 1 root root 1223200 Jun 18 16:28 opencv_test_ml*
  30. -rwxr-xr-x 1 root root 1628464 Jun 18 16:37 opencv_test_objdetect*
  31. -rwxr-xr-x 1 root root 951192 Jun 18 16:28 opencv_test_photo*
  32. -rwxr-xr-x 1 root root 850448 Jun 18 16:38 opencv_test_stitching*
  33. -rwxr-xr-x 1 root root 1418800 Jun 18 16:38 opencv_test_video*
  34. -rwxr-xr-x 1 root root 1880576 Jun 18 16:34 opencv_test_videoio*
  35. -rwxr-xr-x 1 root root 42744 Jun 18 16:48 opencv_version*
  36. -rwxr-xr-x 1 root root 55256 Jun 18 16:48 opencv_visualisation*
  1. root@PAw9033927:~/opencv_install_package/build# ll | grep .cmake
  2. -rw-r--r-- 1 root root 9522 Jun 18 16:08 CPackConfig.cmake
  3. -rw-r--r-- 1 root root 9934 Jun 18 16:08 CPackSourceConfig.cmake
  4. -rw-r--r-- 1 root root 2025 Jun 18 16:08 CTestTestfile.cmake
  5. -rw-r--r-- 1 root root 418 Jun 18 16:08 OpenCVConfig-version.cmake
  6. -rw-r--r-- 1 root root 15818 Jun 18 16:08 OpenCVConfig.cmake
  7. -rw-r--r-- 1 root root 10695 Jun 18 16:09 OpenCVModules.cmake
  8. -rw-r--r-- 1 root root 12239 Jun 18 16:08 cmake_install.cmake
  9. -rw-r--r-- 1 root root 1204 Jun 18 16:08 cmake_uninstall.cmake
  10. -rw-r--r-- 1 root root 2677 Jun 18 16:08 opencv_python_config.cmake

4、安装

安装到用户级别(可选)

  1. cmake --install .

安装到系统级别

  1. make install

5、验证安装结果

在build目录下执行以下命令

  1. root@PAw9033927:~/opencv_install_package/build# ./bin/opencv_version
  2. 4.8.0

通过cmake项目 + cpp代码进行验证安装结果

在 home 目录下创建 cmake_opencv_demo 目录,在该目录下创建 CMakeLists.txt 文件,内容如下:

  1. cmake_minimum_required(VERSION 3.15)
  2. project(opencv_demo LANGUAGES CXX C)
  3. set(CMAKE_CXX_STANDARD 17)
  4. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  5. set(OpenCV_DIR /root/opencv_install_package/build)
  6. find_package(OpenCV REQUIRED)
  7. message(STATUS "OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
  8. message(STATUS "OpenCV_LIBS: ${OpenCV_LIBS}")
  9. add_executable(opencv_demo main.cpp)
  10. target_include_directories(opencv_demo PRIVATE ${OpenCV_INCLUDE_DIRS})
  11. target_link_libraries(opencv_demo ${OpenCV_LIBS})

在创建一个 main.cpp文件

  1. #include <iostream>
  2. #include <opencv2/opencv.hpp>
  3. int main() {
  4. std::cout << "OpenCV version: " << CV_VERSION << std::endl;
  5. return 0;
  6. }

然后,在cmake_opencv_demo 目录下执行以下命令来构建c++项目,

  1. # -S 指定源码位置, -B 指定构建输出目录,若目录不存在,会自动创建
  2. cmake -S . -B build

执行以下命令生成可执行文件

  1. root@PAw9033927:/home/cmake_opencv_demo# cmake --build build
  2. [ 50%] Building CXX object CMakeFiles/opencv_demo.dir/main.cpp.o
  3. [100%] Linking CXX executable opencv_demo
  4. [100%] Built target opencv_demo

执行

  1. root@PAw9033927:/home/cmake_opencv_demo# cd build/
  2. root@PAw9033927:/home/cmake_opencv_demo/build# pwd
  3. /home/cmake_opencv_demo/build
  4. root@PAw9033927:/home/cmake_opencv_demo/build# ./opencv_demo
  5. OpenCV version: 4.8.0

关键字OpenCV