使用 CMake 和 Make 编译时 OpenCV 库错误:找不到 -lopencv_bgsegm

OpenCV library error when compiling with CMake and Make: cannot find -lopencv_bgsegm

我正在尝试构建位于 HERE 的存储库。按照说明,我创建了一个 CMakeLists,并更新了里面的目录地址。我不确定 OpenCV 路径,但找到了两个候选路径并将它们都插入了。

Cmake 很好,但是在执行 make 时出现此错误,我猜这可能是由于 openCV 路径不正确。

这里是详细的输出:

[100%] Linking CXX executable SimpleVideoSummExample
/usr/bin/cmake -E cmake_link_script CMakeFiles/SimpleVideoSummExample.dir/link.txt --verbose=1
/usr/bin/c++    -O3 -std=c++0x -std=c++0x -fopenmp  -s CMakeFiles/SimpleVideoSummExample.dir/src/utils/ShotDetector.cc.o CMakeFiles/SimpleVideoSummExample.dir/src/videoSummarization/SimpleVideoSummarizer.cc.o CMakeFiles/SimpleVideoSummExample.dir/src/utils/ColorUtils.cc.o CMakeFiles/SimpleVideoSummExample.dir/src/utils/ImageUtils.cc.o CMakeFiles/SimpleVideoSummExample.dir/examples/SimpleVideoSummExample.cc.o  -o SimpleVideoSummExample  -L/usr/local/Cellar/opencv/3.4.1_2/lib -rdynamic libsummengine.a -lopencv_imgproc -lopencv_core -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_bgsegm -lopencv_video -lopencv_objdetect -lopencv_dnn -Wl,-rpath,/usr/local/Cellar/opencv/3.4.1_2/lib
/usr/bin/ld: cannot find -lopencv_bgsegm
collect2: error: ld returned 1 exit status
CMakeFiles/SimpleVideoSummExample.dir/build.make:199: recipe for target 'SimpleVideoSummExample' failed
make[2]: *** [SimpleVideoSummExample] Error 1

问题是什么?如何解决?

这是 CMakeLists 的目录部分(Cellar 是 git 存储库的用户):

include_directories(/usr/local/Cellar/opencv/3.4.1_2/include/ /usr/local/Cellar/opencv/3.4.1_2/include/opencv/ /usr/include/opencv /usr/local/include/opencv$
link_directories(/usr/local/Cellar/opencv/3.4.1_2/lib/)
add_executable(SimpleVideoSummExample src/utils/ShotDetector.cc src/videoSummarization/SimpleVideoSummarizer.cc src/utils/ColorUtils.cc src/utils/ImageUtils$
target_link_libraries(SimpleVideoSummExample
    summengine -lopencv_imgproc
    -lopencv_core
    -lopencv_highgui
    -lopencv_videoio
    -lopencv_imgcodecs
    -lopencv_bgsegm
    -lopencv_video
    -lopencv_objdetect
    -lopencv_dnn
)

虽然您使用的 Github 存储库没有在文档中明确说明,但看​​来您不仅需要 OpenCV,还需要构建 extra OpenCV modules (hinted by this 答案)。这些 "extra" OpenCV 模块提供了缺少的 opencv_bgsegm 库。

按照 this 之类的教程,您应该包括 CMake 定义标志:

-DOPENCV_EXTRA_MODULES_PATH=/path/to/opencv_contrib/modules/

当 运行 cmake。这将确保构建额外的模块,并且 opencv_bgsegm 库将在您的系统上可用。