使用CMake on Cygwin编译OpenCV项目,并为Windows安装OpenCV

时间:2015-11-17 04:32:36

标签: c++ opencv gcc cmake raspberry-pi

情况:

我安装了一台安装了 OpenCV for Windows 的Windows 7计算机。 我的OpenCV C ++项目可以与Visual Studio 2010一起使用。

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

故障

作为第一步,我正在尝试编译我的.cpp&我的Windows机器上 Cygwin 上使用 GCC 的.h文件。为此,我尝试使用 CMake软件包进行Cygwin ,但 CMake 会出现无法找到OpenCV软件包的错误。 (错误列在下面)。

问题:

  1. 我是否需要分别为Cygwin安装 OpenCV软件包以使CMake正常工作?
  2. 有没有办法使用我现有的 OpenCV for Win 来使用CMake?
  3. 需要设置哪些环境变量?
    (目前只有变量OPENCV_DIR设置为C:/ opencv / build)
  4. 的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".
    

3 个答案:

答案 0 :(得分:1)

该变量应该被称为OpenCV_DIR,它必须具有与find_package(OpenCV REQUIRED)中相同的大写字母。

答案 1 :(得分:1)

正如@berak在评论中所述,我使用Cygwin Ports为Cygwin安装了OpenCV Package。 (没有任何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)

答案 2 :(得分:0)

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

  1. 克隆到 opencv 源代码仓库

$ 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}。所以你应该准备好了。