我正在尝试减少ios的opencv2.framework大小。我的项目只使用core,imgproc和highgui模块。如何只使用那些模块进行编译?或者是否有减少尺寸的替代方案?
感谢。
答案 0 :(得分:5)
我现在用版本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)
答案 1 :(得分:2)
从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脚本> profite。
之后,手动将lib添加到项目中,并删除标题中留下的讨厌的导入标题> opencv.hpp,你应该好好去!
答案 2 :(得分:1)
只需从modules文件夹中删除模块,然后从头文件中删除包含行。并编译。在此之前,请阅读CMakeLists.txt以获取依赖项。
例如,对于highgui模块;
ocv_define_module(imgproc opencv_core)
答案 3 :(得分:1)
从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