在Windows上将OpenCV与Clion IDE一起使用 [英] use OpenCV with Clion IDE on Windows
问题描述
我实际上是在寻找一种通过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-build
或C:\opencv\vs-x64-build
.选择一个根据您的配置.Configure
并根据您的编译器选择生成器.如果是MingGW,则为MinGW Makefiles
,如果使用的是Visual Studio等,则为Visual Studio ...
... (如果您遇到MinGW的问题,请确保将minGW/bin目录添加到标有"PATH"的环境路径中)
再次单击
Configure
.当所有内容均为白色时,单击Generate
否则编辑红色字段.cmd
和目录以建立 3的目录. 如果选择Visual Studio,请打开生成的解决方案.
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
重新启动计算机.
- 您需要下载 FindOpenCV.cmake 并将其添加到
.
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, ...).
- 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).
ClickConfigure
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.cmake and add it to
project-root/cmake/
.
- You need to download FindOpenCV.cmake and add it to
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屋!