Windows builds OpenCV development environment (C++)


1. Environmental preparation

Windows 10
OpenCV
MinGW-w64
CMake
CLion

1. Download OpenCV, MinGW, CMake

Package download address: https://download.csdn.net/download/u012899618/87902325
You can also download the compiled file directly (this way you can skip the compilation step): https://download.csdn.net/download/u012899618 /87907660

2. Configure environment variables

Both MinGW and CMake need to be configured (configure according to the actual directory)
insert image description here

3. Verify that the environment variables are configured successfully

Open a command prompt, enter

gcc -v

If the prompt is as follows, the configuration is successful
insert image description here

2. Compile OpenCV

1. Configure through cmake-gui

Double-click cmake-gui.exe in the cmake directory
The code is as follows (example):
to enter the interface and follow steps 1, 2, and 3 to configure.
insert image description here
The drop-down box pops up to select MinGW Makefiles
insert image description here
. It should be noted that you must pull down the menu to find the "CPU_DISPATCH" column, and put the following The parameter selection is empty, otherwise an error may be reported when compiling later
insert image description here
Click Generate to
insert image description here
run the command prompt and enter the C:/opencv/mingw-build directory to execute the compilation command

mingw32-make

After the above command is executed, execute the following command to generate the install folder

mingw32-make install

2. Configure OpenCV library environment variables

After the above command is executed, such a folder will be generated.
insert image description here
Add the contents of the bin file to the system environment variable and
insert image description here
run it in the command prompt.
insert image description here
If the version number is displayed, the configuration is successful.

run in CLion

Create a new project untitled1 in CLion and modify the CMakeList.txt file

cmake_minimum_required(VERSION 3.25)
project(untitled1)

set(CMAKE_CXX_STANDARD 11)

# 可以将下面的路径单独添加到环境变量OpenCV_DIR
set(OpenCV_DIR "D:\\opencv\\mingw-build\\install")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
# "untitled1"与你工程的命名相关,注意和原来CMakeList.txt中的声明一致
add_executable(untitled1 main.cpp)
target_link_libraries(untitled1 ${OpenCV_LIBS})

Enter the following in main.cpp

#include <iostream>
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int main() {
    
    
    Mat srcImage = imread("Lenna.png");
    if (!srcImage.data) {
    
    
        std::cout << "Image not loaded";
        return -1;
    }
    imshow("image", srcImage);
    waitKey(0);
    return 0;
}

Put Lenna.png in the cmake-build-debug path, because the compiled executable file is here, or you can also use the absolute path.
The running effect is shown in the figure below
insert image description here


Tip: For more content, please visit Clang's Blog: https://www.clang.asia

Guess you like

Origin blog.csdn.net/u012899618/article/details/131187335