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?
daB0bby
I can tell you, how I did this on Windows.
First of all, you need MinGW and CMake.
- Download the OpenCV source files. Link
- Unpack to
C:\opencv(or a folder of your choice) - Open
CMakeand select source (directory of 2.) and build for exampleC:\opencv\mingw-build - Click
Configureand selectMinGW Makefiles. (If you experience problems, 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 Configureagain. When everything is white clickGenerateelse edit the red fields. - open
cmdand dir to build directory of 3. - Run
mingw32-make(ormingw64-make). This takes a while.
- Once it is done, run
mingw32-make install(ormingw64-make install).
This creates an install folder, where everything you need for building your own OpenCV apps is included. - To system
PATHaddC:\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 depends on the setup you make in 2. and 3. You can change them if you like.
来源:https://stackoverflow.com/questions/35984678/use-opencv-with-clion-ide-on-windows

