use OpenCV with Clion IDE on Windows

。_饼干妹妹 提交于 2019-11-30 09:25:32
daB0bby

I can tell you, how I did this on Windows.

First of all, you need MinGW and CMake.

  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
  4. Click Configure and select MinGW Makefiles. (If you experience problems, 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.
  7. Run mingw32-make (or mingw64-make). This takes a while.

  8. Once it is done, run mingw32-make install (or mingw64-make install).
    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 depends on the setup you make in 2. and 3. You can change them if you like.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!