标签归档:world

opencv world版编译说明

1、命令模式生成工程

先用命令模式生成工程:

cmake . -Bbuild -G"Visual Studio 16 2019" -Ax64

指令要求生成VS2019的msvc16版本工程。
基本毫无悬念,都能正确生成(最多会有几个需要从网站上现下的库下载失败,不需要,无所谓)
工程文件生成到了build目录下,名为OpenCV.sln。

  • 关于CMake动态下载失败问题

在执行cmake生成指令时,CMake会根据配置从网站下载一些第三方库,opencv用到的几个为IPPICV、FFMPEG等,但均提示下载失败,原因是”无法解析主机名”,这里用的是主机’raw.githubusercontent.com’。CMake要求用IP,所以失败,这也许是CMake的bug。
解决办法:在hosts文件中增加github的IP映射:

  • hosts文件: C:\Windows\System32\drivers\etc\hosts (记得要用管理员打开,否则无法保存)
  • 映射到IP: 199.232.68.133 raw.githubusercontent.com
    内容大概是如下样子:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1             localhost
199.232.68.133      raw.githubusercontent.com

因为其他第三方库都是这个IP,所以增加此行后,其他几个库也能正常下载。

2、编译opencv_world版本

opencv被分成了大量的动态库,使用起来非常不方便,所以有人用CMake配置了一个opencv_world版,干净利落,所以更倾向于使用world版,但不知道怎么在命令模式增加此选项,只能打开cmake GUI程序,在里面找到BUILD_opencv_world选项,勾上,然后点configure按钮,然后点Generate按钮,这时候项目只会生成opencv_ts和opencv_world两个模块。

3、编译、安装

理想情况下opencv的sdk就是一个包含了头文件、lib文件dll文件、相关文档等的目录,但是它的include目录并没有整理得很好。所幸它的install做的极其完美(这也是他没有按照预想的方式整理include、lib、dll目录的原因吧),利用它的install就能把编译完的include目录、lib文件等完美放到build目录下,方法:
到build目录双击OpenCV.sln,在vs2019中打开工程。在解决方案中,找到CMakeTarges,展开后,右键编译INSTALL。它会编译各个模块并在build目录下生成一个install目录,然后把所有所需文件“安装”到这个目录,成为一个完整的sdk包,直接拿去用。
记得debug和release两个版本都install build一下,因为如果用的是静态库,debug和release模式得用相应版本。

4、关于opencv contrib库

很多东西从3.0就被拆分到contrib中了,而且单独维护,就是说,默认下载的很多东西没了,如果要用contrib里的东西,比如tracking等,你需要下载contrib库:

https://github.com/opencv/opencv_contrib

下完后解压,放到某目录,在cmake-gui中搜索extra,搜到后指定你的contrib中的modules目录,再次配置、生成,就有了tracking等功能;

  • 组件下载失败导致contrib编译不通过

contrib中有一个xfeatures2d库,这个库用到的一些文件需要动态从github下载,而如果下载配置有问题,大概率就会编译失败:

C1083    Cannot open include file: 'vgg_generated_120.i': No such file or directory
C1083    Cannot open include file: 'boostdesc_bgm.i': No such file or directory

配置成功后,执行cmake生成工程时会看到如下信息:

-- xfeatures2d/boostdesc: Download: boostdesc_bgm.i
-- xfeatures2d/boostdesc: Download: boostdesc_bgm_bi.i
-- xfeatures2d/boostdesc: Download: boostdesc_bgm_hd.i
-- xfeatures2d/boostdesc: Download: boostdesc_binboost_064.i
-- xfeatures2d/boostdesc: Download: boostdesc_binboost_128.i
-- xfeatures2d/boostdesc: Download: boostdesc_binboost_256.i
-- xfeatures2d/boostdesc: Download: boostdesc_lbgm.i
-- xfeatures2d/vgg: Download: vgg_generated_48.i
-- xfeatures2d/vgg: Download: vgg_generated_64.i
-- xfeatures2d/vgg: Download: vgg_generated_80.i
-- xfeatures2d/vgg: Download: vgg_generated_120.i
  • opencv_contrib_4.5无法编译的问题

上面提到的contrib库,我是从contrib的master上下载的,与4.5配合发布的contrib 4.5无法编译,master里已经解决了这个问题。

5、第三方引用的cmake find文件

假设名为FindOpencvWorld.cmake,其内容为:

########################################################################
# CMake module for finding OpencvWorld
#
# The following variables will be defined:
#
#  OPENCVWORLD_FOUND
#  OPENCVWORLD_INCLUDE_DIR
#  OPENCVWORLD_LIBRARY
#

unset(OpenCV_INCLUDE_DIR CACHE)
unset(OpenCV_FOUND CACHE)
unset(OpenCV_LIBRARY CACHE)

if (${arch} STREQUAL "i386")
    set(build_arch "x86")
endif (${arch} STREQUAL "i386")
if (${arch} STREQUAL "x86_64")
    set(build_arch "x64")
endif (${arch} STREQUAL "x86_64")

find_path(OPENCVWORLD_INCLUDE_DIR
  NAMES opencv2/opencv.hpp
  PATHS ${PROJECT_SOURCE_DIR}/thirdparty/opencv-4.5.0/include
  NO_DEFAULT_PATH)
find_library(OPENCVWORLD_LIBRARY_debug
  NAMES opencv_world450d
  PATHS ${PROJECT_SOURCE_DIR}/thirdparty/opencv-4.5.0/${build_arch}/vc16/lib/
  NO_DEFAULT_PATH)

find_library(OPENCVWORLD_LIBRARY_release
   NAMES opencv_world450
   PATHS ${PROJECT_SOURCE_DIR}/thirdparty/opencv-4.5.0/${build_arch}/vc16/lib/
   NO_DEFAULT_PATH)

set(OPENCVWORLD_LIBRARY optimized ${OPENCVWORLD_LIBRARY_release} debug ${OPENCVWORLD_LIBRARY_debug})
#set(OPENCVWORLD_LIBRARY ${OPENCVWORLD_LIBRARY_release})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(OpencvWorld DEFAULT_MSG OPENCVWORLD_LIBRARY OPENCVWORLD_INCLUDE_DIR)