Macos安装OpenCV遇到的坑

1、使用homebrew安装OpenCV

在终端中输入

brew install opencv

2、OpenCV安装目录

新版MacOS(12.4)下,brew的安装位置在 /opt/homebrew

Macos安装OpenCV遇到的坑_第1张图片

3、遇到的错误

  1. cmake报错
====================[ Build | all | Debug ]=====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/kyros/CLionProjects/games/cmake-build-debug --target all -- -j 6
CMake Error at Assignment1/CMakeLists.txt:5 (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.

Macos安装OpenCV遇到的坑_第2张图片

原因:新版MacOS(12.4)的brew安装位置改变,导致cmake的find_package(OpenCV REQUIRED)找不到OpenCV的库报错,需要手动设置包的位置。

解决办法:

1、在brew根目录下搜索 OpenCVConfig.cmake

find /opt/homebrew -name "OpenCVConfig.cmake"

Macos安装OpenCV遇到的坑_第3张图片

2、找到文件后在顶层CMakeList.txt中添加:

set(OpenCV_DIR /opt/homebrew/Cellar/opencv/4.5.5_2/lib/cmake/opencv4/)
find_package(OpenCV REQUIRED)

然后CMake会自动处理好头文件和运行库

你可能感兴趣的:(电脑知识,macos,opencv,计算机视觉,人工智能)