文章目录
- 1.前置条件-cmake和c++
- 2. opencv
- 2.1 opencv安装
- 2.2 opencv测试
- 2.2.1 基本测试
- 2.2.2 opencv里的自带测试图像
- 2.3 报错
- 2.3.1 MacOSX10.15.sdk/usr/include/sys/cdefs.h:807:2: error: Unsupported architecture
- 2.2.2 电脑上没有安装java(Unable to locate a Java Runtime.)
- 2.3.1 zlib报错(No rule to make target zlib, needed by `lib/libopencv_imgcodecs.4.8.0.dylib'. Stop.)
- 2.4 记录安装信息
- 2.5 non-free模块
- 3. Qt配置
- 3.1 环境和支持
- 3.1.1 确定本机架构、平台环境等
- 3.1.2 Qt对macOS的支持
1.前置条件-cmake和c++
- 先看看vscode里怎么弄c++,详见:mac下vscode配置c++环境
- 同时记得把cmake下载好,路径配置好
2. opencv
直接从官网教程开始(事实证明不是很全面,还是有很多问题需要额外找解决方案):
https://docs.opencv.org/4.x/d0/db2/tutorial_macos_install.html
一般是有两种安装方式:
- 直接安装对应平台的预编译版本,可以直接去这里下载,默认不含有
opencv_contrib
模块
上面三个就是安卓,ios平台和Windows下的预编译好的版本,后面两个source code就是从源码编译了,也就是第二种安装方式,一般linux都是用后者来安装。 - 从源码安装,也就是下面介绍的内容
2.1 opencv安装
# 1. 确认安装好了cmake,同时版本大于3.9
>cmake --version
cmake version 3.22.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
# 2. 下载稳定版的opencv源码,截止2023.10.30 最新的稳定版是4.8
https://github.com/opencv/opencv/archive/4.8.0.zip
# 下载后解压,无论是macos双击解压,还是命令行解压都行
# 3. 然后就可以用cmake来从源码build opencv了
# 这里建议新建立一个 build_opencv存放构建好的opencv,和解压后的opencv不是一个东西
mkdir build_opencv
cd build_opencv
# 然后在build_opencv目录下运行下面的cmake命令,也可以使用cmake的可视化界面
# 1. cmake用来生成makefile
# 确保当前的执行路径位于build_opencv
# 确认当前的python环境就是下面写的那个python执行环境,同时保证这个环境里有numpy
# 注意,换行命令分割的符号\要和前面的命令之间隔空格,但是VAR:type=value,参数和参数值(等号左右没有空格)
cmake \
-DWITH_OPENJPEG=OFF \
-DWITH_IPP=OFF \
-D CMAKE_BUILD_TYPE=Release \
-D BUILD_EXAMPLES=ON \
-D WITH_IPP=OFF \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_JAVA=OFF \
-D BUILD_OPENEXR=ON \
-D PYTHON3_EXECUTABLE=/Users/huangshan/Documents/software/miniconda3/miniconda3/bin/python \
../opencv-4.8.0
# 2. make命令把Makefile 文件中定义的一系列任务将软件源代码编译成可执行文件
make -j8
# make之后 还有人会进行make install
# make install 命令就是将可执行文件、第三方依赖包和文档复制到正确的路径。
# 其实make之后,软件其实就已经编译好了,可用了
上面的cmake里的命令里,有一些参数是可以自己配的
CMAKE_BUILD_TYPE=Release
或者是Debug
,一般开发过程中建议用debug,可以帮助调试,前者的话是无法打开调试模式的,更详细的说明可以看:Debug build- 如果开启了
CV_DISABLE_OPTIMIZATION
,那么很多第三方库就不会优化了,比如:Eigen等 - 如果使用的是gcc的默认GNU(libstdc++),则可以考虑打开
ENABLE_GNU_STL_DEBUG
,则生成的内容也可以用debug模式
- 如果开启了
- 如果要捎带构建opencv_contrib里的内容,那么需要设置
OPENCV_EXTRA_MODULES_PATH=<path to opencv_contrib>/modules
,详见:Build with extra modules,主要就是opencv_contrib
中的一些内容 - 另外,编译的时候也可以禁用一些插件,解耦插件,详见:(optional) Step 4: Build plugins
- macOS在构建的时候可能需要额外自己注意下自己的系统架构,是
xx86_64
还是arm64
,可以用命令uname -a
试一下,比如我的就是x86_64
> uname -a Darwin XXXMacBook-Pro.local 22.3.0 Darwin Kernel Version 22.3.0: Mon Jan 30 20:42:11 PST 2023; root:xnu-8792.81.3~2/RELEASE_X86_64 x86_64
参考:
- configure、 make、 make install 背后的原理(翻译)
- Determine the architecture of a Mac from the command line or script?
2.2 opencv测试
2.2.1 基本测试
为了验证opencv安装是否成功,可以随便找个程序跑一下:
1. CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
2. DisplayImage.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], IMREAD_COLOR );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
这两个文件创建好之后,切换到这两个文件所在目录,运行:
如果直接运行下面的命令而不加任何别的参数,
>cmake .
则会报错:
CMake Error at CMakeLists.txt:3 (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.
因此在运行时,需要添加CMAKE_PREFIX_PATH
参数,来指明编译好的opencv的位置。即正确的运行命令是:
>cmake -D CMAKE_PREFIX_PATH=XXXXX/build_opencv .
所以构建好的库就不要随便挪位置了,不然记不住了。。。
正确后会提示:
-- Found OpenCV: XXXX/build_opencv (found version "4.8.0")
-- Configuring done
-- Generating done
-- Build files have been written to: XXXX
然后就:
> make
[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
[100%] Linking CXX executable DisplayImage
[100%] Built target DisplayImage
此时就已经生成了 CMakeLists中project( DisplayImage )这个项目名称,为名称的一个可执行文件
接下来运行,需要传递一个图像路径作为参数,自己随便找个图,如果懒得找的话,可以去opencv的安装包里找,路径类似下面。
>./DisplayImage ./lib/opencv-4.8.0/samples/data/lena.jpg
ctrl+C
由于上面的可执行程序是被强制终止的,因此使用`echo $?`查看上个程序的执行状态,返回的是130
如果是正确执行,返回的则是1
> echo $?
130
关于exit code是130的解释,详见:Why is “Doing an exit 130 is not the same as dying of SIGINT”?
然后就可以看到这样的显示啦,但是左上角没有❎,要去程序里ctrl+C
强制终止。
参考:
- OpenCV Tutorials-Using OpenCV with gcc and CMake
2.2.2 opencv里的自带测试图像
在vscode里,使用command+p打开搜索框,输入.jpg,就可以看到,其实opencv自带了很多图片,主要的两个路径是:
- opencv-4.8.0/samples/data
- lib/opencv-4.8.0/doc/tutorials中每个教程里面的images文件夹
2.3 报错
2.3.1 MacOSX10.15.sdk/usr/include/sys/cdefs.h:807:2: error: Unsupported architecture
完整的报错信息是:
CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/stdlib.h:62:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/cdefs.h:807:2:
error: Unsupported architecture
报错信息提示是架构问题,
# 以下面的命令为基准
cmake \
-DWITH_OPENJPEG=OFF \
-DWITH_IPP=OFF \
-D CMAKE_BUILD_TYPE=Release \
-D BUILD_EXAMPLES=ON \
-D WITH_IPP=OFF \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D PYTHON3_EXECUTABLE=/Users/huangshan/Documents/software/miniconda3/miniconda3/bin/python \
../opencv-4.8.0
# 分别添加以下的配置时指定架构的命令,同时为了防止cmake用cache,每次cmake前会删除当前文件夹,重新全部生成
1. 不指定架构参数 可以make,不会再报架构错误,因此以下两种就没有再尝试了。
注意:如果之前cmake的时候指定过一些参数,那么下次cmake即便没有用这些参数,默认cmake会找上次的cache也会出错,
因此,建议cmake出错之后,clear一下,再去进行cmake,
之前错是因为指定-DCMAKE_OSX_ARCHITECTURES=arm64,之后没有删除cache,就继续cmake了
2. 指定x86_64和arm64
# CFLAGS="-mmacosx-version-min=11.0 -arch x86_64 -arch arm64" \
# -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" \
3. 只指定x86_64
# CFLAGS="-mmacosx-version-min=11.0 -arch x86_64" \
# -DCMAKE_OSX_ARCHITECTURES="x86_64" \
参考:
- How do I build cmake from source as universal(arm64 and x86_64) binary on Mac?
- Creating universal binary(arm64 and x86_64) for Mac system #1206
- Building and Installing OpenCV 4.5.0 on an M1 Mac
- How to fix “#error architecture not supported” when installing pycurl with Python 3.8.2 on macOS 10.15?
- Osx_image xcode12.2 does not come with macos 11 SDK (no way to compile for ARM)
- Determine the architecture of a Mac from the command line or script?
2.2.2 电脑上没有安装java(Unable to locate a Java Runtime.)
报错:
[ 15%] Building Java objects for opencv_java_jar.jar
[ 15%] Linking CXX static library ../lib/liblibtiff.a
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
make[2]: *** [modules/java/jar/CMakeFiles/opencv_java_jar.dir/java_compiled_opencv_java_jar] Error 1
make[1]: *** [modules/java/jar/CMakeFiles/opencv_java_jar.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
解决:
在cmake命令里加入:
-D BUILD_JAVA=OFF \
参考:OpenCV configuration options reference-Miscellaneous features
2.3.1 zlib报错(No rule to make target zlib, needed by `lib/libopencv_imgcodecs.4.8.0.dylib’. Stop.)
报错的完整信息:
[ 30%] Building CXX object modules/features2d/CMakeFiles/opencv_features2d.dir/src/kaze/fed.cpp.o
make[2]: *** No rule to make target `zlib', needed by `lib/libopencv_imgcodecs.4.8.0.dylib'. Stop.
make[2]: *** Waiting for unfinished jobs....
[ 30%] Building CXX object modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/src/exif.cpp.o
错误原因:
其实不是网上有些方案说的homebrew里的zlib库冲突导致的,用brew info zlib
命令查看本机的zlib情况,可以看到,本机是没有安装zlib的
>brew info zlib
==> zlib: stable 1.2.13 (bottled), HEAD [keg-only]
General-purpose lossless data-compression library
https://zlib.net/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/zlib.rb
License: Zlib
我这里更深层的原因是:
本机之前安装了OPENEXR这个库,导致opencv直接用了这个brew安装的openexr,但是这个openexr并不包括zlib的依赖,因此显示No rule to make target zlib
可以看一下自己的电脑是不是也安装过:
>brew info OpenEXR
==> openexr: stable 3.1.9 (bottled)
High dynamic-range image file format
https://www.openexr.com/
/usr/local/Cellar/openexr/3.1.9 (194 files, 7.4MB) *
Poured from bottle using the formulae.brew.sh API on 2023-07-24 at 19:00:32
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openexr.rb
由上面信息可知,是2023.7.24安装的,
如果想知道是安装什么的时候给装上去的,可以按照时间去排列brew安装的库,即
>brew ls -l # 按照时间顺序查看安装的库
drwxr-xr-x 3 huangshan admin 96 Jul 24 19:03 node
drwxr-xr-x 3 huangshan staff 96 Jul 24 19:03 opencore-amr
drwxr-xr-x 3 huangshan admin 96 Jul 24 19:03 openexr
drwxr-xr-x 3 huangshan admin 96 Jul 24 19:03 openfortivpn
drwxr-xr-x 3 huangshan staff 96 Jul 24 19:03 openjpeg
再去百度一下,就知道openexr是跟着node一起安的
如果之前安装过node
,会默认配套一起安装openexr这个库,这个库的介绍可以看看openexr-百度百科。
解决:
在cmake命令里加入:
-D BUILD_OPENEXR=ON \
一些相关资料:
# 在 opencv-4.8.0/modules/imgcodecs/CMakeLists.txt 文件中,有
if(HAVE_PNG OR HAVE_TIFF OR HAVE_OPENEXR OR HAVE_SPNG)
ocv_include_directories(${ZLIB_INCLUDE_DIRS})
list(APPEND GRFMT_LIBS ${ZLIB_LIBRARIES})
endif()
# 在build_opencv/modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/DependInfo.cmake中,有
"/Users/XXX/Documents/DailyStudy/cpp/lib/build_opencv/3rdparty/zlib/CMakeFiles/zlib.dir/DependInfo.cmake"
# 即zlib其实位于opencv-4.8.0/3rdparty/zlib文件夹里
参考:
- ✅Issues on OpenCV 4.5.5 compiling from scratch on newer macOS (Intel chip, macOS Monterey) #21389
- mac os 安装完整版opencv (with qt )
- openCV4 : Mac 编译和开发环境搭建
- MacOS Catalina Unable to find Zlib#4109
brew info zlib
- CMake No rule to make target
2.4 记录安装信息
其实安装信息里打印的内容(opencv的可配置内容)大多数来自:OpenCV configuration options reference
cmake过程中会出现很多提示信息,比如关于库和安装的信息,也许以后会用得到,这里记录一下:(类似的日志可以在build_opecv/version_string.tmp 、build_opecv/CMakeVars.txt 中看到)
-- Performing Test HAVE_CXX_MAVX512F (check file: cmake/checks/cpu_avx512.cpp)
类似这样的检查信息,其实大部分是检查有没有包含一些库,或者一些视频音频编码器的支持,
可以打开这些文件看看,内容其实都挺简单的,
比如:opencv-4.8.0/cmake/checks/opencl.cpp就是看本机有没有安装opencl,这个文件只有21行
# SIMD项没有启用
-- libjpeg-turbo(SIMD): SIMD extensions disabled: could not find NASM compiler. Performance will suffer.
# 有一些包没有放在opencv源码里,想用的话需要额外下载
-- Checking for modules 'libavcodec;libavformat;libavutil;libswscale'
-- Found libavcodec, version 60.3.100
-- Found libavformat, version 60.3.100
-- Found libavutil, version 58.2.100
-- Found libswscale, version 7.1.100
-- Checking for module 'libavresample'
-- No package 'libavresample' found
-- Checking for module 'gstreamer-base-1.0'
-- No package 'gstreamer-base-1.0' found
-- Checking for module 'gstreamer-app-1.0'
-- No package 'gstreamer-app-1.0' found
-- Checking for module 'gstreamer-riff-1.0'
-- No package 'gstreamer-riff-1.0' found
-- Checking for module 'gstreamer-pbutils-1.0'
-- No package 'gstreamer-pbutils-1.0' found
-- Checking for module 'gstreamer-video-1.0'
-- No package 'gstreamer-video-1.0' found
-- Checking for module 'gstreamer-audio-1.0'
-- No package 'gstreamer-audio-1.0' found
-- Checking for module 'libdc1394-2'
-- No package 'libdc1394-2' found
# 最重要的安装信息
-- General configuration for OpenCV 4.8.0 =====================================
-- Version control: unknown
--
-- Platform:
-- Timestamp: 2023-10-30T03:00:47Z
-- Host: Darwin 22.3.0 x86_64
-- CMake: 3.22.3
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: Release
-- CPU/HW features:
-- Baseline: SSE SSE2 SSE3 SSSE3 SSE4_1
-- requested: DETECT
-- Dispatched code generation: SSE4_2 FP16 AVX AVX2 AVX512_SKX
-- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
-- SSE4_2 (2 files): + POPCNT SSE4_2
-- FP16 (1 files): + POPCNT SSE4_2 FP16 AVX
-- AVX (8 files): + POPCNT SSE4_2 AVX
-- AVX2 (37 files): + POPCNT SSE4_2 FP16 FMA3 AVX AVX2
-- AVX512_SKX (8 files): + POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
--
# 这下面是本机的c/c++编译器和调试器的情况,支持的命令
# 另外,可以看到,支持的C++标准是C++ 11
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ standard: 11
-- C++ Compiler: /Library/Developer/CommandLineTools/usr/bin/c++ (ver 11.0.0.11000033)
-- C++ flags (Release): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- C flags (Release): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,-dead_strip
-- Linker flags (Debug): -Wl,-dead_strip
再下面,就是已经安装好的opencv的module
-- OpenCV modules:
-- To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java ml objdetect photo stitching ts video videoio
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: python2 python3
-- Applications: tests perf_tests examples apps
...
-- Python (for build): /Users/XXX/Documents/software/miniconda3/miniconda3/bin/python3
2.5 non-free模块
之前说的有些受专利保护的内容,被放在这个模块。说明详见:学习Opencv(蝴蝶书/C++)——1. 前言 和 第1章.概述中1.3.1 基本信息部分
在构建好的OpenCV中,在build_opencv/opencv2/opencv_modules.hpp
中,有以下内容:
// This definition means that OpenCV is built with enabled non-free code.
// For example, patented algorithms for non-profit/non-commercial use only.
/* #undef OPENCV_ENABLE_NONFREE */
#define HAVE_OPENCV_CALIB3D
#define HAVE_OPENCV_CORE
#define HAVE_OPENCV_DNN
#define HAVE_OPENCV_FEATURES2D
...
同时,在build_opencv/modules/features2d/CMakeFiles/opencv_features2d.dir/src
文件夹中,确实看不到那几个受到专利保护的算法,其他的都可以看到:
- https://docs.opencv.org/4.8.0/d5/d51/group__features2d__main.html
3. Qt配置
也是从官网教程开始看,主要是:
- Qt for macOS
- Getting Started with Qt
另外,还有:
- https://web.stanford.edu/dept/cs_edu/resources/qt/install-mac
- https://learnopencv.com/configuring-qt-for-opencv-on-osx/
其实Qt安装起来比较简单,直接下载安装包安装软件即可。
要和OpenCV连用,主要是要在Qt里配置一下OpenCV的路径等。
暂时用不上,等需要的时候再安装吧,大概知道重点在哪了
3.1 环境和支持
3.1.1 确定本机架构、平台环境等
macOS是苹果对Mac系列的电脑的操作系统,是基于Darwin内核的UNIX平台,类似于其他的类UNIX系统。主要的区别就是X11之后不再使用窗口系统,而是使用苹果自己原生的窗口系统,可以通过Cocoa API访问。
1. 查看自己的内核和架构,比如,我的就是:Darwin x86_64
>uname -a
Darwin huangshandeMacBook-Pro.local 22.3.0 Darwin Kernel Version 22.3.0: Mon Jan 30 20:42:11 PST 2023; root:xnu-8792.81.3~2/RELEASE_X86_64 x86_64
2. 查看自己的SDK/XCode版本,比如:我的就是10.15的sdk
> xcrun -sdk macosx --show-sdk-path
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
> cd /Library/Developer/CommandLineTools/SDKs
MacOSX.sdk MacOSX10.14.sdk MacOSX10.15.sdk