macOS环境使用clang/makefile编译opencv-4.2.0和opencv_contrib-4.2.0源码

简介: macOS环境使用clang/makefile编译opencv-4.2.0和opencv_contrib-4.2.0源码

之前写了一篇文章:


Windows环境使用VS2017编译opencv-4.2.0和opencv_contrib-4.2.0源码,支持TBB


现在macOS也需要使用opencv,使用clang编译源码,与Windows编译的不同点有几个:


1、准备好编译器


先安装好Xcode和Command Tools


再Agree to Xcode license in Terminal: sudo xcodebuild -license


2、不需要使能TBB,opencv-4.2.0自带并默认使用了Apple的GCD并行库。


Grand Central Dispatch(GCD)是Apple开发的一个多核编程的较新的解决方法。

在Mac OSX 10.6首次推出,也可在iOS4及以上版本使用。

它主要用于优化应用程序以支持多核处理器以及其他对称多处理系统。

它是一个在线程池模式的基础上执行的并发任务。

使用GCD的好处:

GCD可用于多核的并行运算;

GCD会自动利用更多的 CPU 内核(比如双核、四核);

GCD会自动管理线程的生命周期(创建线程、调度任务、销毁线程),程序员只需要告诉 GCD 想要执行什么任务。

https://opensource.apple.com/source/libdispatch/

https://opensource.apple.com/tarballs/libdispatch/

3、cmake的Configure选择Unix Makefiles


cmake的install参数默认值是CMAKE_INSTALL_PREFIX=/usr/local,请用户根据需要,自行决定是否要更改路径。


image.png


4、cmake Generate之后,会生成Makefile文件,使用终端命令进行编译


sudo make clean


sudo make -j8


sudo make install


5、应用实践


以qt为例,建立demo,pro文件如下:

#-------------------------------------------------
#
# Project created by QtCreator 2019-08-29T10:43:54
#
#-------------------------------------------------
QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = qt_opencv_demo
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
    Widget_op.cpp \
    mylabel.cpp
HEADERS  += \
    mylabel.h \
    Widget_op.h \
    stable.h
FORMS += Widget_op.ui
#指定生成的临时文件放置的目录
MOC_DIR = temp/moc
RCC_DIR = temp/rcc
UI_DIR = temp/ui
OBJECTS_DIR = temp/obj
#指定编译器选项和项目配置
#CONFIG += c++11
CONFIG += warn_on           #告诉qmake要把编译器设置为输出警告信息的
#CONFIG += warn_off          #不要警告输出
CONFIG += precompile_header #可以在项目中使用预编译头文件的支持
#预编译头文件路径
PRECOMPILED_HEADER = $$PWD/stable.h
#disable C4819 warning
win32:QMAKE_CXXFLAGS_WARN_ON += -wd4819
win32:QMAKE_CXXFLAGS += /FS
#避免VC编译器关于fopen等应使用fopen_s的安全警告
win32:DEFINES += _CRT_SECURE_NO_WARNINGS
#指定生成的应用程序放置的目录
win32:CONFIG(debug, debug|release){
    contains(DEFINES, WIN64) {
    DESTDIR = ../_debug64
    } else {
    DESTDIR = ../_debug32
    }
} else:win32:CONFIG(release, debug|release){
    contains(DEFINES, WIN64) {
    DESTDIR = ../_release64
    } else {
    DESTDIR = ../_release32
    }
}
macx:CONFIG(debug, debug|release){
    DESTDIR = ../bind
} else:macx:CONFIG(release, debug|release){
    DESTDIR = ../bin
}
message($$PWD)
message($$DESTDIR)
#Opencv Configuration
isEmpty(MY_OPENCV_PATH) {
    win32:MY_OPENCV_PATH = "D:/My Resources/ImageProcessingLibrary/opencv/" #注意斜杆的方向;引号;
    macx:MY_OPENCV_PATH = "/Users/firecat/opencv4.2.0"
    message($$MY_OPENCV_PATH)
}
#头文件包含路径
win32 {
    INCLUDEPATH += $${MY_OPENCV_PATH}/include/
    DEPENDPATH += $${MY_OPENCV_PATH}/include/
}
macx {
    INCLUDEPATH += $${MY_OPENCV_PATH}/include/opencv4
    DEPENDPATH += $${MY_OPENCV_PATH}/include/opencv4
}
#库文件包含路径
win32 {
contains(QT_ARCH, i386) {
        message("32-bit")
        #Windows x86 (32bit) specific build here
        MY_OPENCV_LIBS_PATH = $${MY_OPENCV_PATH}/Win32/vc15/lib
    } else {
        message("64-bit")
        #Windows x64 (64bit) specific build here
        MY_OPENCV_LIBS_PATH = $${MY_OPENCV_PATH}/x64/vc15/lib
    }
    message($$MY_OPENCV_LIBS_PATH)
}
macx {
    MY_OPENCV_LIBS_PATH = $${MY_OPENCV_PATH}/lib
    message($$MY_OPENCV_LIBS_PATH)
}
#依赖库的名称
win32:CONFIG(debug, debug|release): {#动态库
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_calib3d420d
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_core420d
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_features2d420d
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_flann420d
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_highgui420d
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgcodecs420d
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgproc420d
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_videoio420d
}
else:win32:CONFIG(release, debug|release):{
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_calib3d420
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_core420
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_features2d420
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_flann420
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_highgui420
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgcodecs420
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgproc420
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_videoio420
}
else:macx:{
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_calib3d.4.2.0
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_core.4.2.0
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_features2d.4.2.0
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_flann.4.2.0
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_highgui.4.2.0
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgcodecs.4.2.0
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgproc.4.2.0
    LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_videoio.4.2.0
}
#pro文件的参考文档
#https://doc.qt.io/qt-5/qmake-variable-reference.html
相关文章
|
2月前
|
算法 计算机视觉 Docker
Docker容器中的OpenCV:轻松构建可移植的计算机视觉环境
Docker容器中的OpenCV:轻松构建可移植的计算机视觉环境
Docker容器中的OpenCV:轻松构建可移植的计算机视觉环境
|
1月前
|
机器学习/深度学习 算法 数据库
【功能超全】基于OpenCV车牌识别停车场管理系统软件开发【含python源码+PyqtUI界面+功能详解】-车牌识别python 深度学习实战项目
【功能超全】基于OpenCV车牌识别停车场管理系统软件开发【含python源码+PyqtUI界面+功能详解】-车牌识别python 深度学习实战项目
|
1月前
|
监控 安全 计算机视觉
实战 | 18行代码轻松实现人脸实时检测【附完整代码与源码详解】Opencv、人脸检测
实战 | 18行代码轻松实现人脸实时检测【附完整代码与源码详解】Opencv、人脸检测
|
1月前
|
算法 数据处理 计算机视觉
基于OpenCV的人脸对齐步骤详解及源码实现
基于OpenCV的人脸对齐步骤详解及源码实现
|
1月前
|
机器学习/深度学习 算法 数据可视化
基于OpenCV的人脸检测软件(含Python源码+UI界面+图文详解)
基于OpenCV的人脸检测软件(含Python源码+UI界面+图文详解)
|
1月前
|
机器学习/深度学习 并行计算 计算机视觉
【入坑指南】| OpenCV4.8 + CUDA + 扩展模块支持编译
【入坑指南】| OpenCV4.8 + CUDA + 扩展模块支持编译
120 0
|
1月前
|
机器学习/深度学习 TensorFlow 算法框架/工具
Win7上编译OpenCV3.2与扩展模块
Win7上编译OpenCV3.2与扩展模块
25 0
|
1月前
|
计算机视觉 C++ Windows
OpenCV 3.1.0编译与添加扩展模块
OpenCV 3.1.0编译与添加扩展模块
40 0
|
2月前
|
Ubuntu 编译器 C++
Ubuntu系统下编译OpenCV4.8源码
本文档介绍了在Ubuntu系统下编译和安装OpenCV4.8的简单步骤:首先,通过wget命令下载源码包,然后解压;接着,安装必要的编译器和第三方库支持;最后,在源码目录创建build文件夹,执行cmake和make安装。整个过程包括下载、安装依赖和编译安装三个主要步骤。
66 6
|
1天前
|
机器学习/深度学习 XML 计算机视觉
OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉和机器学习库,它提供了大量的函数和工具,用于处理图像和视频数据。
OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉和机器学习库,它提供了大量的函数和工具,用于处理图像和视频数据。
5 0