在 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
use OpenCV with Clion IDE on Windows
提问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。
然后你需要一个你选择的编译器(MinGW,Visual Studio,...)。
- Download the OpenCV source files. Link
- Unpack to
C:\opencv
(or a folder of your choice) - Open
CMake
and select source (directory of 2.) and build for exampleC:\opencv\mingw-build
orC:\opencv\vs-x64-build
. Choose one accoring your configuration. - Click
Configure
and select the generator according to you compiler.MinGW Makefiles
in case of MingGW orVisual 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') - 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 clickGenerate
else edit the red fields. - Open
cmd
and dir to build directory of 3.
If you chose Visual Studio open the generated solution. - Compile the library. Run
mingw32-make
(ormingw64-make
) or build theBUILD_ALL
project from the generated solution in Visual Studio if your choosen compiler is MSVC.
This takes a while. - Once it is done, run
mingw32-make install
(ormingw64-make install
). If you've choosen Visual Studio you need to build theINSTALL
project.
This creates an install folder, where everything you need for building your own OpenCV apps is included. - To system
PATH
addC:\opencv\mingw-build\install\x86\mingw\bin
Restart your PC. - Set up CLion:
- You need to download FindOpenCV.cmakeand add it to
project-root/cmake/
.
- You need to download FindOpenCV.cmakeand add it to
- 下载 OpenCV 源文件。关联
- 解压到
C:\opencv
(或您选择的文件夹) - 打开
CMake
并选择源(目录2.)并构建例如C:\opencv\mingw-build
或C:\opencv\vs-x64-build
。根据您的配置选择一个。 - 单击
Configure
并根据您的编译器选择生成器。MinGW Makefiles
如果是 MingGW 或者Visual Studio ...
如果您使用的是 Visual Studio 等等......
(如果您遇到 MinGW 问题,请确保将 minGW/bin 目录添加到标有“PATH”的环境路径中) - 等待配置完成,编辑您需要的属性(在我的情况下,我不需要测试、文档和 python)。再次
点击
Configure
。当一切都是白色时,点击Generate
其他编辑红色字段。 - Open
cmd
and dir to build directory of 3.
如果选择Visual Studio,打开生成的解决方案。 - 编译库。如果您选择的编译器是 MSVC,则从 Visual Studio 中生成的解决方案运行
mingw32-make
(或mingw64-make
)或构建BUILD_ALL
项目。
这需要一段时间。 - 完成后,运行
mingw32-make install
(或mingw64-make install
)。如果您选择了 Visual Studio,则需要构建INSTALL
项目。
这将创建一个安装文件夹,其中包含构建自己的 OpenCV 应用程序所需的一切。 - 系统
PATH
添加C:\opencv\mingw-build\install\x86\mingw\bin
重新启动您的PC。 - 设置 CLion:
- 您需要下载FindOpenCV.cmake并将其添加到
project-root/cmake/
.
- 您需要下载FindOpenCV.cmake并将其添加到
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这样的包管理器会让生活变得更轻松。