在 Cygwin 上使用 CMake 编译 OpenCV 项目,为 Windows 安装 OpenCV

Compiling an OpenCV project using CMake on Cygwin, with OpenCV installed for Windows

情况:

我有一台 Windows 7 机器,上面安装了 OpenCV for Windows。 我的 OpenCV C++ 项目在 Visual Studio 2010 上运行良好。

我想 运行 我现有的 OpenCV C++ 项目到 Raspberry Pi 和其他 Linux 机器上的 运行。

麻烦

作为第一步,我尝试在 Cygwin 上使用 GCC 编译我的 .cpp 和 .h 文件61=] 机器。为此,我尝试为 Cygwin 使用 CMake 包,但 CMake 给出了找不到 OpenCV 包的错误。 (错误如下所列)。

问题:

  1. 我需要为 Cygwin 单独安装 OpenCV 包以便 CMake 工作吗?
  2. 有没有办法使用我现有的 OpenCV for Win 与 CMake 一起工作?
  3. 需要设置什么环境变量?
    (目前只有一个变量OPENCV_DIR设置为C:/opencv/build)

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.4) 
PROJECT (testProj)
find_package(OpenCV REQUIRED )
set( NAME_SRC
    src/main.cpp    
    src/imgProcess.cpp    
)

set( NAME_HEADERS       
     include/imgProcess.h
)

INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( testProj ${NAME_SRC} ${NAME_HEADERS} )

target_link_libraries( test ${OpenCV_LIBS} )

cmake错误

$ cmake .
-- The C compiler identification is GNU 4.9.3
-- The CXX compiler identification is GNU 4.9.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++.exe
-- Check for working CXX compiler: /usr/bin/c++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:3 (find_package):
  By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "OpenCV", but
  CMake did not find one.

  Could not find a package configuration file provided by "OpenCV" with any
  of the following names:

    OpenCVConfig.cmake
    opencv-config.cmake

  Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
  "OpenCV_DIR" to a directory containing one of the above files.  If "OpenCV"
  provides a separate development package or SDK, be sure it has been
  installed.
-- Configuring incomplete, errors occurred!
See also "/cygdrive/c/Work/Project/cmaketest/CMakeFiles/CMakeOutput.log".

变量应命名为 OpenCV_DIR,其大小写必须与 find_package(OpenCV REQUIRED) 中的相同。

正如@berak 在评论中所述,我使用 Cygwin 端口为 Cygwin 安装了 OpenCV 包。 (没有任何 src 或需要任何构建,仅简单安装)。
不得不稍微调整一下我的 CMakeLists.txt 但它编译得很好。

##### CMakeLists.txt ########
cmake_minimum_required(VERSION 2.8.4) 
PROJECT (test)
find_package(OpenCV REQUIRED )

set( NAME_SRC
    src/main.cpp    
    src/mytest.cpp    
)

set( NAME_HEADERS       
    include/mytest.hpp
)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( test ${NAME_SRC} ${NAME_HEADERS} )
include_directories(/usr/include /usr/include/opencv2 /usr/include/opencv ${CMAKE_CURRENT_SOURCE_DIR}/include)
link_directories(/usr/lib ${CMAKE_BINARY_DIR}/bin)
target_link_libraries(test opencv_core opencv_highgui opencv_imgproc)

(现在,我遇到了完全不同的运行时错误问题:Gtk-WARNING **: cannot open display,但这是另一个在 Cygwin 上没有 XServer 的故事)

首先按照以下步骤使用 visual studio 和 cmake gui 构建 opencv。

  1. 克隆到 opencv 源代码 repo

$ git clone https://github.com/Itseez/opencv.git

  1. 在文件夹内导航并检出指定版本

$ git checkout 3.0.0

  1. 创建一个构建目录并在其中使用 CMake 和 visual studio 构建 opencv。请注意,默认情况下 opencv 将构建动态库,除非您启用构建 opencv 静态库的选项。

  2. 完成后您应该处理环境变量。创建一个名为 OpenCV_DIR 的变量,并将其指向 opencv 的构建目录。然后更新您的 PATH 变量以添加新的 opencv dll 文件。通常它们可以在目录 opencv\build\bin\Release 中找到,假设您已经在 Release 模式

    中构建了 opencv

然后按如下方式设置您的项目CMakeList.txt

find_package(OpenCV  REQUIRED)  

include_directories(    
    ${OpenCV_INCLUDE_DIRS}     
)

target_link_libraries(test
    ${OpenCV_LIBS}
)

命令 find_package(OpenCV REQUIRED) 在目录“opencv/build”中找到 opencv 配置文件,也就是您构建 opencv 的目录(这就是为什么您应该设置变量值 ${OpenCV_DIR} 到此目录,以便命令 find_package 可以在其中找到所需的文件)。

两个变量{OpenCV_INCLUDE_DIRS}${OpenCV_LIBS}已经在那些配置文件中设置了。所以你应该准备好了。