我正在尝试减少 ios 的 opencv2.framework 大小。我的项目只使用 core、imgproc 和 highgui 模块。如何仅使用这些模块进行编译?还是有减小尺寸的替代方法?
谢谢。
我现在在 3.1 版本中遇到了这个问题,nebuto 的答案很接近,但目前还不完整。通过更新 build_framework.py 文件,以下内容能够产生所需的结果。
def getCMakeArgs(self, arch, target):
args = [
"cmake",
"-GXcode",
"-DBUILD_SHARED_LIBS=OFF",
"-DBUILD_opencv_core=ON",
"-DBUILD_opencv_imgcodecs=OFF",
"-DBUILD_opencv_imgproc=ON",
"-DBUILD_opencv_world=OFF",
"-DBUILD_opencv_gpu=OFF",
"-DBUILD_opencv_calib3d=OFF",
"-DBUILD_opencv_contrib=OFF",
"-DBUILD_opencv_features2D=OFF",
"-DBUILD_opencv_flann=OFF",
"-DBUILD_opencv_highgui=ON",
"-DBUILD_opencv_legacy=OFF",
"-DBUILD_opencv_ml=OFF",
"-DBUILD_opencv_nonfree=OFF",
"-DBUILD_opencv_objdetect=OFF",
"-DBUILD_opencv_photo=OFF",
"-DBUILD_opencv_stitching=OFF",
"-DBUILD_opencv_video=OFF",
"-DBUILD_opencv_videoio=OFF",
"-DBUILD_opencv_videostab=OFF",
"-DAPPLE_FRAMEWORK=ON",
"-DCMAKE_INSTALL_PREFIX=install",
"-DCMAKE_BUILD_TYPE=Release",
]
return args
此更新还包括两个在 2.4.6 中似乎不存在的新模块。
对于一些额外的节省,您可能会删除位于文件底部的模拟器构建的 i386 设置。如果您的应用是 iOS 9 及更高版本,并且您希望省略 32 位设备,您也可以去掉 armv7 选项。
b = Builder(args.opencv, args.contrib,
[
("armv7", "iPhoneOS"),
("armv7s", "iPhoneOS"),
("arm64", "iPhoneOS"),
#("i386", "iPhoneSimulator"),
("x86_64", "iPhoneSimulator"),
])
b.build(args.out)
从 v2.4.6 开始,OpenCV 有一个自定义 python 脚本来简化工作(plateforms > ios > build_framework.py)
您只需要在 cmake 行上添加标志即可进行自定义编译。
例如:我只需要 lib imgproc 并将所有标题包含到存档中,我的 cmake 现在类似于:
cmakeargs = ("-GXcode " +
"-D BUILD_SHARED_LIBS=OFF "+
"-D BUILD_opencv_core=ON "+
"-D BUILD_opencv_imgproc=ON "+
"-D BUILD_opencv_world=ON "+
"-D BUILD_opencv_gpu=OFF "+
"-D BUILD_opencv_calib3d=OFF "+
"-D BUILD_opencv_contrib=OFF "+
"-D BUILD_opencv_features2D=OFF "+
"-D BUILD_opencv_flann=OFF "+
"-D BUILD_opencv_highgui=OFF "+
"-D BUILD_opencv_legacy=OFF "+
"-D BUILD_opencv_ml=OFF "+
"-D BUILD_opencv_nonfree=OFF "+
"-D BUILD_opencv_objdetect=OFF "+
"-D BUILD_opencv_photo=OFF "+
"-D BUILD_opencv_stitching=OFF "+
"-D BUILD_opencv_video=OFF "+
"-D BUILD_opencv_videostab=OFF "+
"-DCMAKE_BUILD_TYPE=Release " +
"-DCMAKE_TOOLCHAIN_FILE=%s/platforms/ios/cmake/Toolchains/Toolchain-%s_Xcode.cmake " +
"-DCMAKE_INSTALL_PREFIX=install") % (srcroot, target)
不要忘记添加核心和世界库,因为它们是强制性的。执行python脚本>利润。
之后手动将库添加到您的项目中,并删除头文件 > opencv.hpp 中留下的讨厌的导入头文件,您应该一切顺利!
只需从模块文件夹中删除模块并从头文件中删除包含行。然后编译。在这样做之前,请阅读 CMakeLists.txt 以了解依赖项。
比如对于highgui模块;
ocv_define_module(imgproc opencv_core)
从 opencv 3.4.1 开始,您现在可以使用BUILD_LIST
arg 指定要构建的模块。这是一种白名单方法与使用-BUILD_opencv_xxx=OFF
上述方法的黑名单方法。
如果您想要的只是imgproc
模块,那么您可以将构建脚本修改为以下内容:
def getCMakeArgs(self, arch, target):
args = [
"cmake",
"-GXcode",
"-DBUILD_LIST=imgproc"
"-DAPPLE_FRAMEWORK=ON",
"-DCMAKE_INSTALL_PREFIX=install",
"-DCMAKE_BUILD_TYPE=Release",
]
return args
尽管在实践中,这对我来说并不奏效。我还必须手动排除highgui
、imgcodecs
和videoio
:
def getCMakeArgs(self, arch, target):
args = [
"cmake",
"-GXcode",
"-DBUILD_LIST=imgproc"
"-DBUILD_opencv_highgui=OFF",
"-DBUILD_opencv_imgcodecs=OFF",
"-DBUILD_opencv_videoio=OFF",
"-DAPPLE_FRAMEWORK=ON",
"-DCMAKE_INSTALL_PREFIX=install",
"-DCMAKE_BUILD_TYPE=Release",
]
return args