在 Windows 上使用带有 Clion IDE 的 OpenCV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35984678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:29:55  来源:igfitidea点击:

use OpenCV with Clion IDE on Windows

windowsopencvclion

提问by miton18

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

我实际上正在寻找一种使用 JetBrains 的 Clion 使用 OpenCV 创建应用程序的方法。

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

我已经用 Choco 安装了 OpenCV,所以我在 C:\opencv 中有所有的东西

this is my projet with Clion

这是我与 Clion 的项目

CMakeLists.txt:

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:

和 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

和所有 OpenCV 函数的未定义错误

Do you know why it doesn't works?

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

回答by daB0bby

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

首先,你需要CMake
然后你需要一个你选择的编译器(MinGWVisual Studio,...)。

  1. Download the OpenCV source files. Link
  2. Unpack to C:\opencv(or a folder of your choice)
  3. Open CMakeand select source (directory of 2.) and build for example C:\opencv\mingw-buildor C:\opencv\vs-x64-build. Choose one accoring your configuration.
  4. Click Configureand select the generator according to you compiler. MinGW Makefilesin 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).
    enter image description hereClick Configureagain. When everything is white click Generateelse edit the red fields.
  6. Open cmdand 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_ALLproject from the generated solution in Visual Studio if your choosen compiler is MSVC.
    This takes a while.enter image description here
  8. Once it is done, run mingw32-make install(or mingw64-make install). If you've choosen Visual Studio you need to build the INSTALLproject.
    This creates an install folder, where everything you need for building your own OpenCV apps is included.
  9. To system PATHadd C:\opencv\mingw-build\install\x86\mingw\bin
    Restart your PC.
  10. Set up CLion:
  1. 下载 OpenCV 源文件。关联
  2. 解压到C:\opencv(或您选择的文件夹)
  3. 打开CMake并选择源(目录2.)并构建例如C:\opencv\mingw-buildC:\opencv\vs-x64-build。根据您的配置选择一个。
  4. 单击Configure并根据您的编译器选择生成器。MinGW Makefiles如果是 MingGW 或者Visual Studio ...如果您使用的是 Visual Studio 等等......
    (如果您遇到 MinGW 问题,请确保将 minGW/bin 目录添加到标有“PATH”的环境路径中)
  5. 等待配置完成,编辑您需要的属性(在我的情况下,我不需要测试、文档和 python)。再次
    在此处输入图片说明点击Configure。当一切都是白色时,点击Generate其他编辑红色字段。
  6. Open cmdand dir to build directory of 3.
    如果选择Visual Studio,打开生成的解决方案。
  7. 编译库。如果您选择的编译器是 MSVC,则从 Visual Studio 中生成的解决方案运行mingw32-make(或mingw64-make)或构建BUILD_ALL项目。
    这需要一段时间。在此处输入图片说明
  8. 完成后,运行mingw32-make install(或mingw64-make install)。如果您选择了 Visual Studio,则需要构建INSTALL项目。
    这将创建一个安装文件夹,其中包含构建自己的 OpenCV 应用程序所需的一切。
  9. 系统PATH添加C:\opencv\mingw-build\install\x86\mingw\bin
    重新启动您的PC。
  10. 设置 CLion:

CMakeLists.txt:

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:

主.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.

构建并运行main.cpp

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

所有路径都取决于您在2.3 中所做的设置您可以根据需要更改它们。

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

更新 1:我更新了使用您选择的编译器的帖子。

SUGGESTION:Using a package manager like Conanmakes life much easier.

建议:使用像Conan这样的包管理器会让生活变得更轻松。