在M1 Mac 上使用 OpenCV

今天尝试在 M1 Mac 上编译 OpenCV 的程序,源代码使用的是 《Master OpenCV 4》中第一章的代码

先看 Readme,里面关于 Mac 上的编译说明:

export OpenCV_DIR="~/OpenCV/build"
mkdir build
cd build
cmake -G Xcode -D OpenCV_DIR=$OpenCV_DIR ..
open Cartoonifier.xcodeproj

猜测 OpenCV_DIR 应该对应的是头文件所在的文件夹,尝试:

export OpenCV_DIR="/Users/xxxxx/miniforge3/envs/cv/include/opencv4/opencv2"

然后继续下面的步骤,可惜在执行 cmake 的时候,报错了:

CMake Error at CMakeLists.txt:8 (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" (requested
  version 4.0.0) 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.

根据提示,在 iniforge3 的文件夹下找到了含有 cmake 后缀的文件,重新 export 一下:

export OpenCV_DIR="/Users/xxxxx/miniforge3/pkgs/libopencv-4.5.5-py39h4853228_6/lib/cmake/opencv4"

成功的生成了 xcode 工程。 使用 Xcode 打开后, build and run 一下~ 运行时出错了:

dyld[7516]: Library not loaded: @rpath/libprotobuf.30.dylib
  Referenced from: /Users/xxxxx/miniforge3/pkgs/libopencv-4.5.5-py39h4853228_6/lib/libopencv_dnn.4.5.5.dylib
  Reason: tried: '/Users/xxxxx/Downloads/MasterOpenCV4/OpenCV4/Code/Chapter_01/build/Debug/libprotobuf.30.dylib' (no such file), '/usr/lib/system/introspection/libprotobuf.30.dylib' (no such file),
  .......

显然运行时找不到相关的 lib,继续在 miniforge3 文件夹下搜索,比如搜索 libprotobuf,在以下文件夹下找到:

/Users/xxxxx/miniforge3/envs/cv/lib

于是在 Xcode 内修改 build 选项,添加上述地址到 link path中,然后再运行,成功!
下面是运行效果:
master opencv in M1 Mac

Comments