OpenCV和OpenCV_contrib编译为MacOS系统的framework

最近在MacOS上要用到SIFT算法来计算图片相似度,opencv的SIFT算法代码在OpenCV_contrib中,如果想打包一个供MacOS使用的framework需要将OpenCV和OpenCV_contrib打在一起。本文介绍从OpenCV和OpenCV_contrib源码打包cocoa开发使用的framework的方法,OpenCV和OpenCV_contrib选择当前最新的release4.1.0版本。

具体步骤:

1.准备工作

安装Homebrew

安装CMake

下载opencv源码和opencv_contrib源码放在同一个目录下

OpenCV和OpenCV_contrib编译为MacOS系统的framework_第1张图片

2.编译

$ cd 
$ python opencv-4.1.0/platforms/osx/build_framework.py osx

编译完成后,在当前用户目录下会自动创建osx目录,opencv2.framework文件在osx目录中。

3.可能遇到的问题

问题:ippicv_2019_mac_intel64_general_20180723.tgz无法下载

源码编译opencv卡在IPPICV: Download: ippicv_2019_mac_intel64_general_20180723.tgz,此时需要手动下载该文件并修改对应的配置。具体方法参见解法。

4.编译脚本说明

打包前对opencv-4.1.0/platforms/osx/build_framework.py的原始内容做了一些调整:

(1)默认脚本MACOSX_DEPLOYMENT_TARGET=10.9,版本过低导致打包失败,根据自己需要支持的最低版本进行修改,我调整为10.12

修改前

OpenCV和OpenCV_contrib编译为MacOS系统的framework_第2张图片

修改后

OpenCV和OpenCV_contrib编译为MacOS系统的framework_第3张图片

(2)默认脚本打出的framework只包含oponcv,需要将opencv_contirb包含进来。同时SIFT算法需要开启nonfree。下图中选中的区域为针对原脚本变动的部分。

OpenCV和OpenCV_contrib编译为MacOS系统的framework_第4张图片

5.完整脚本

修改后的完整脚本如下,也可在这里下载后修改的脚本。

#!/usr/bin/env python
"""
The script builds OpenCV.framework for OSX.
"""

from __future__ import print_function
import os, os.path, sys, argparse, traceback, multiprocessing

# import common code
sys.path.insert(0, os.path.abspath(os.path.abspath(os.path.dirname(__file__))+'/../ios'))
from build_framework import Builder

class OSXBuilder(Builder):

    def getToolchain(self, arch, target):
        return None

    def getBuildCommand(self, archs, target):
        buildcmd = [
            "xcodebuild",
            "MACOSX_DEPLOYMENT_TARGET=10.12",
            "ARCHS=%s" % archs[0],
            "-sdk", target.lower(),
            "-configuration", "Release",
            "-parallelizeTargets",
            "-jobs", str(multiprocessing.cpu_count())
        ]
        return buildcmd

    def getInfoPlist(self, builddirs):
        return os.path.join(builddirs[0], "osx", "Info.plist")


if __name__ == "__main__":
    folder = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "../.."))
    folder2 = os.path.join(os.path.dirname(folder),"opencv_contrib-4.1.0")
    parser = argparse.ArgumentParser(description='The script builds OpenCV.framework for OSX.')
    parser.add_argument('out', metavar='OUTDIR', help='folder to put built framework')
    parser.add_argument('--opencv', metavar='DIR', default=folder, help='folder with opencv repository (default is "../.." relative to script location)')
    parser.add_argument('--contrib', metavar='DIR', default=folder2, help='folder with opencv_contrib repository (default is "None" - build only main framework)')
    parser.add_argument('--without', metavar='MODULE', default=[], action='append', help='OpenCV modules to exclude from the framework')
    parser.add_argument('--enable_nonfree', default=True, dest='enablenonfree', action='store_true', help='enable non-free modules (disabled by default)')
    args = parser.parse_args()

    b = OSXBuilder(args.opencv, args.contrib, False, False, args.without, args.enablenonfree,
        [
            (["x86_64"], "MacOSX")
        ])
    b.build(args.out)

参考文章:

opencv和opencv_contrib编译为iOS系统的framework

源码编译opencv卡在IPPICV: Download: ippicv_2017u3_lnx_intel64_general_20170822.tgz解决办法

你可能感兴趣的:(MocOS)