在Windows上将OpenCV与Clion IDE一起使用 [英] use OpenCV with Clion IDE on Windows

查看:735
本文介绍了在Windows上将OpenCV与Clion IDE一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是在寻找一种通过JetBrains的Clion使用OpenCV创建应用的方法.

我已经在Choco上安装了OpenCV,所以所有内容都放在C:\ opencv

这是我与Clion合作的项目

CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)
project(test)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories("C:\\opencv\\build\\include\\")

FIND_PACKAGE( OpenCV REQUIRED core highgui imgproc)
set(OpenCV_FOUND TRUE)

set(SOURCE_FILES main.cpp)
add_executable(prog ${SOURCE_FILES})

和main.cpp:

#include <opencv2/opencv.hpp>

int main() {

    cv::Mat img = cv::imread("./test.jpg", -1);
    cv::imshow("Mon image", img);
    cv::waitKey(0);
    return 0;
}

生成的响应为:

undefined reference to `cv::imread(cv::String const&, int)'

所有OpenCV函数的未定义错误

您知道为什么它不起作用吗?

解决方案

首先,您需要 CMake .
然后,您需要选择的编译器( MinGW 链接

  • 解压到C:\opencv(或您选择的文件夹)
  • 打开CMake并选择源( 2.目录)并构建例如C:\opencv\mingw-buildC:\opencv\vs-x64-build.选择一个根据您的配置.
  • 单击Configure并根据您的编译器选择生成器.如果是MingGW,则为MinGW Makefiles,如果使用的是Visual Studio等,则为Visual Studio ... ...
    (如果您遇到MinGW的问题,请确保将minGW/bin目录添加到标有"PATH"的环境路径中)
  • 等待配置完成,根据需要编辑您的属性(在我的情况下,我不需要测试,文档和python).
    再次单击Configure.当所有内容均为白色时,单击Generate否则编辑红色字段.
  • 打开cmd和目录以建立 3的目录.
    如果选择Visual Studio,请打开生成的解决方案.
  • 编译库.如果您选择的编译器是MSVC,请运行mingw32-make(或mingw64-make)或从Visual Studio中生成的解决方案中构建BUILD_ALL项目.
    这需要一段时间.
  • 完成后,运行mingw32-make install(或mingw64-make install).如果选择了Visual Studio,则需要构建INSTALL项目.
    这将创建一个安装文件夹,其中包含构建自己的OpenCV应用程序所需的所有内容.
  • 向系统PATH添加C:\opencv\mingw-build\install\x86\mingw\bin
    重新启动计算机.
  • 设置CLion:
  • CMakeLists.txt:

    project(test)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    
    # Where to find CMake modules and OpenCV
    set(OpenCV_DIR "C:\\opencv\\mingw-build\\install")
    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
    
    find_package(OpenCV REQUIRED)
    include_directories(${OpenCV_INCLUDE_DIRS})
    
    add_executable(test_cv main.cpp)
    
    # add libs you need
    set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs) 
    
    # linking
    target_link_libraries(test_cv ${OpenCV_LIBS})
    

    main.cpp:

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <iostream>
    
    int main(int argc, char** argv)
    {
        if(argc != 2)
        {
            std::cout << "Usage: display_image ImageToLoadAndDisplay" << std::endl;
            return -1;
        }
    
        cv::Mat frame;
        frame = cv::imread(argv[1], IMREAD_COLOR); // Read the file
    
        if(!frame) // Check for invalid input
        {
            std::cout << "Could not open or find the frame" << std::endl;
            return -1;
        }
    
        cv::namedWindow("Window", WINDOW_AUTOSIZE); // Create a window for display.
        cv::imshow("Window", frame); // Show our image inside it.
    
        cv::waitKey(0); // Wait for a keystroke in the window
        return 0;
    }
    

    构建并运行main.cpp.

    所有路径取决于您在 2. 3中进行的设置.您可以根据需要进行更改.

    更新1:我更新了该帖子,以使用您选择的编译器.

    建议:使用类似 Conan 的程序包管理器,可使生活变得更加轻松.

    I'm actually looking for a way to create apps with OpenCV with Clion from JetBrains.

    I've installed OpenCV with Choco, so I have all the stuff in C:\opencv

    this is my projet with Clion

    CMakeLists.txt:

    cmake_minimum_required(VERSION 3.3)
    project(test)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    include_directories("C:\\opencv\\build\\include\\")
    
    FIND_PACKAGE( OpenCV REQUIRED core highgui imgproc)
    set(OpenCV_FOUND TRUE)
    
    set(SOURCE_FILES main.cpp)
    add_executable(prog ${SOURCE_FILES})
    

    and the main.cpp:

    #include <opencv2/opencv.hpp>
    
    int main() {
    
        cv::Mat img = cv::imread("./test.jpg", -1);
        cv::imshow("Mon image", img);
        cv::waitKey(0);
        return 0;
    }
    

    and the response to build is :

    undefined reference to `cv::imread(cv::String const&, int)'
    

    and undefined errors for all OpenCV functions

    Do you know why it doesn't works?

    解决方案

    First of all, you need CMake.
    Then you need a compiler of your choise (MinGW, Visual Studio, ...).

    1. Download the OpenCV source files. Link
    2. Unpack to C:\opencv (or a folder of your choice)
    3. Open CMake and select source (directory of 2.) and build for example C:\opencv\mingw-build or C:\opencv\vs-x64-build. Choose one accoring your configuration.
    4. Click Configure and select the generator according to you compiler. MinGW Makefiles in case of MingGW or Visual Studio ... if you are using Visual Studio and so on ...
      (If you experience problems with MinGW, ensure that the minGW/bin directory is added to the evironment path labelled, 'PATH')
    5. Wait for the configuration to be done, edit your properties of your needs (in my case I don't need tests, docs and python).
      Click Configure again. When everything is white click Generate else edit the red fields.
    6. Open cmd and dir to build directory of 3.
      If you chose Visual Studio open the generated solution.
    7. Compile the library. Run mingw32-make (or mingw64-make) or build the BUILD_ALL project from the generated solution in Visual Studio if your choosen compiler is MSVC.
      This takes a while.
    8. Once it is done, run mingw32-make install (or mingw64-make install). If you've choosen Visual Studio you need to build the INSTALL project.
      This creates an install folder, where everything you need for building your own OpenCV apps is included.
    9. To system PATH add C:\opencv\mingw-build\install\x86\mingw\bin
      Restart your PC.
    10. Set up CLion:

    CMakeLists.txt:

    project(test)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    
    # Where to find CMake modules and OpenCV
    set(OpenCV_DIR "C:\\opencv\\mingw-build\\install")
    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
    
    find_package(OpenCV REQUIRED)
    include_directories(${OpenCV_INCLUDE_DIRS})
    
    add_executable(test_cv main.cpp)
    
    # add libs you need
    set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs) 
    
    # linking
    target_link_libraries(test_cv ${OpenCV_LIBS})
    

    main.cpp:

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <iostream>
    
    int main(int argc, char** argv)
    {
        if(argc != 2)
        {
            std::cout << "Usage: display_image ImageToLoadAndDisplay" << std::endl;
            return -1;
        }
    
        cv::Mat frame;
        frame = cv::imread(argv[1], IMREAD_COLOR); // Read the file
    
        if(!frame) // Check for invalid input
        {
            std::cout << "Could not open or find the frame" << std::endl;
            return -1;
        }
    
        cv::namedWindow("Window", WINDOW_AUTOSIZE); // Create a window for display.
        cv::imshow("Window", frame); // Show our image inside it.
    
        cv::waitKey(0); // Wait for a keystroke in the window
        return 0;
    }
    

    Build and run main.cpp.

    All Paths depend on the setup you make in 2. and 3. You can change them if you like.

    UPDATE 1: I updated the post for using a compiler of you choice.

    SUGGESTION: Using a package manager like Conan makes life much easier.

    这篇关于在Windows上将OpenCV与Clion IDE一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆