opencv-4.8.0编译及使用

1 编译

        opencv的编译总体来说比较简单,但必须记住一点:opencv的版本必须和opencv_contrib的版本保持一致。例如opencv使用4.8.0,opencv_contrib也必须使用4.8.0。

        进入opencv和opencv_contrib的github页面后,默认看到的是git分支,如4.x。

        如果想要下载具体的版本,需要通过tag进行查找。

        注意,一定要下载具体的tag进行编译,不要选择branch,否则编译会报错。如opencv选择4.8.0,opencv_contrib选择4.x,编译会报下面的错误:

/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1437:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
OPENCV_HAL_IMPL_SSE_SHIFT_OP(v_uint16x8, v_int16x8, epi16, _mm_srai_epi16)
^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1427:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpuvec v_shr(const _Tpuvec& a) \
               ^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1437:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1432:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpsvec v_shr(const _Tpsvec& a) \
               ^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1438:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
OPENCV_HAL_IMPL_SSE_SHIFT_OP(v_uint32x4, v_int32x4, epi32, _mm_srai_epi32)
^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1427:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpuvec v_shr(const _Tpuvec& a) \
               ^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1438:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1432:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpsvec v_shr(const _Tpsvec& a) \
               ^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1439:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
OPENCV_HAL_IMPL_SSE_SHIFT_OP(v_uint64x2, v_int64x2, epi64, v_srai_epi64)
^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1427:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpuvec v_shr(const _Tpuvec& a) \
               ^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1439:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1432:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpsvec v_shr(const _Tpsvec& a) \
               ^
/Users/Github/opencv_contrib-4.x/modules/xphoto/src/grayworld_white_balance.cpp:290:21: error: no matching function for call to 'v_shr'
            v_sR2 = v_shr(v_mul_wrap(v_sR2, v_gainR), 8);
                    ^~~~~
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1437:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
OPENCV_HAL_IMPL_SSE_SHIFT_OP(v_uint16x8, v_int16x8, epi16, _mm_srai_epi16)
^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1427:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpuvec v_shr(const _Tpuvec& a) \
               ^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1437:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1432:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpsvec v_shr(const _Tpsvec& a) \
               ^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1438:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
OPENCV_HAL_IMPL_SSE_SHIFT_OP(v_uint32x4, v_int32x4, epi32, _mm_srai_epi32)
^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1427:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpuvec v_shr(const _Tpuvec& a) \
               ^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1438:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1432:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpsvec v_shr(const _Tpsvec& a) \
               ^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1439:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
OPENCV_HAL_IMPL_SSE_SHIFT_OP(v_uint64x2, v_int64x2, epi64, v_srai_epi64)
^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1427:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpuvec v_shr(const _Tpuvec& a) \
               ^
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1439:1: note: candidate function template not viable: requires single argument 'a', but 2 arguments were provided
/Users/Github/opencv-4.8.0/modules/core/include/opencv2/core/hal/intrin_sse.hpp:1432:16: note: expanded from macro 'OPENCV_HAL_IMPL_SSE_SHIFT_OP'
inline _Tpsvec v_shr(const _Tpsvec& a) \
               ^
/Users/Github/opencv_contrib-4.x/modules/xphoto/src/grayworld_white_balance.cpp:328:21: error: no matching function for call to 'v_shr'
            v_sB1 = v_shr(v_mul(v_sB1, v_gainB), 16);

        编译脚本如下(make -j8,使用并发编译,编译速度会快些):

make build
cd build
cmake ../ -DCMAKE_INSTALL_PREFIX=/Users/opencv-4.8.0 -DBUILD_SHARED_LIBS=OFF -DOPENCV_EXTRA_MODULES_PATH=/Users/Github/opencv_contrib-4.8.0/modules
make -j8
make install

2 使用

        编写cmake时,强烈建议使用find_package引用头文件和lib库,尤其是lib库,千万不要手动逐个引入lib库。opencv的lib库引用关系表复杂,手动引用会出现各种符号未定义。大量的符号未定义会让人忙到怀疑人生。

        完整的cmake如下所示:

#
#cmake for first_opencv
#
cmake_minimum_required(VERSION 3.28)
set(target "first_opencv")
project(${target})
set(CMAKE_CXX_STANDARD 17)
set(project_path "${CMAKE_CURRENT_SOURCE_DIR}")
file(GLOB srcs "${project_path}/src/*.cpp" "${project_path}/src/*.c")
file(GLOB incs "${project_path}/src/*.hpp" "${project_path}/src/*.h")
source_group(source ${srcs})
source_group(include ${incs})

find_package(Iconv)
set(OpenCV_DIR "/Users/libs/opencv-4.8.0/lib/cmake/opencv4")
find_package(OpenCV REQUIRED)
include_directories(${incs} ${OpenCV_INCLUDE_DIRS})

add_executable(${target} ${srcs} ${incs})

target_link_libraries(${target} ${OpenCV_LIBS})

         注意:

  • set(OpenCV_DIR "/Users/libs/opencv-4.8.0/lib/cmake/opencv4")中/Users/libs/opencv-4.8.0/lib/cmake/opencv4为OpenCVConfig.cmake所在的目录,如果不设置OpenCV_DIR,会报下面错误:
CMake Error at CMakeLists.txt:16 (find_package):
  By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "OpenCV", but
  CMake did not find one.

  Could not find a package configuration file provided by "OpenCV" with any
  of the following names:

    OpenCVConfig.cmake
    opencv-config.cmake

  Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
  "OpenCV_DIR" to a directory containing one of the above files.  If "OpenCV"
  provides a separate development package or SDK, be sure it has been
  installed.
  • find_package(Iconv)必须添加,否则会报下面的错误:
CMake Error at /Users/libs/opencv-4.8.0/lib/cmake/opencv4/OpenCVModules.cmake:371 (set_target_properties):
  The link interface of target "opencv_wechat_qrcode" contains:

    Iconv::Iconv

  but the target was not found.  Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.

Call Stack (most recent call first):
  /Users/libs/opencv-4.8.0/lib/cmake/opencv4/OpenCVConfig.cmake:133 (include)
  CMakeLists.txt:16 (find_package)

相关推荐

  1. Opencv 编译

    2024-01-14 00:24:02       38 阅读
  2. 第一章 使用CMake与VS2022编译Opencv

    2024-01-14 00:24:02       37 阅读
  3. 5.llama.cpp编译使用

    2024-01-14 00:24:02       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-14 00:24:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-14 00:24:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-14 00:24:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-14 00:24:02       20 阅读

热门阅读

  1. 4 - IO

    4 - IO

    2024-01-14 00:24:02      31 阅读
  2. 统计coco数据集标签数量

    2024-01-14 00:24:02       43 阅读
  3. C语言之数组与strlen与sizeof区别和应用

    2024-01-14 00:24:02       39 阅读
  4. 【python与物理】用类的形式设计U,R,I求解过程

    2024-01-14 00:24:02       37 阅读
  5. 基于SpringBoot使用AOP开发接口的访问日志信息

    2024-01-14 00:24:02       43 阅读
  6. node 第二十天 手写SPA前端路由,vue-router实现原理

    2024-01-14 00:24:02       46 阅读
  7. Dockerfile指令详解

    2024-01-14 00:24:02       37 阅读
  8. C语言结构体2

    2024-01-14 00:24:02       42 阅读
  9. 文件包含漏洞原理以及修复方法

    2024-01-14 00:24:02       45 阅读
  10. 11.【TypeScript 教程】函数(Function)

    2024-01-14 00:24:02       39 阅读
  11. Unity-游戏与帧

    2024-01-14 00:24:02       42 阅读