CodeBlocks under Windows configures the Opencv environment

Table of contents

1. Opencv download

2.CodeBlocks download

3. Cmake compilation tool download

4. Compile Opencv source code

5. Compile the OpenCV library file

 (1) Method 1

 (2) Method 2

6. Dynamic link library file environment configuration

7.CodeBlocks configuration

8. Write an example using OpenCV

9. Abnormal program termination solution

expand


Tip: It is recommended that readers install or configure the path of environment variables without any special symbols, such as '-' or spaces.

1. Opencv download

https://opencv.org/releases/

 Tip: After the download is complete, double-click to decompress.

        The Opencv directory contains two folders, build and source. The build folder is the compiled VC++ version of the Opencv library file, and the source folder is the Opencv source code file. Since the cross-platform compiler GCC/G++ is used, it needs to be in Code: : In the Blocks environment, combine CMake with GCC/G++ to compile and link to generate the required static library files (.a files) and dynamic link library files (.dll files);

2.CodeBlocks download

https://www.codeblocks.org/downloads/binaries/

 hint:

(1) Unzip and install after the download is complete;

(2) Configure the MinGW\bin system environment volume (Path) under the CodeBlocks directory: Advanced System Settings -> Environment Variables -> System Variables;

Such as: E:\CodeBlocks\CodeBlocks\MinGW\bin

3. Cmake compilation tool download

https://cmake.org/download/

4. Compile Opencv source code

Tip: Therefore, it is best not to have Chinese characters or spaces in the file installation path, otherwise the compilation will fail when facing it.

After decompression, enter the bin directory under the Cmake directory and use cmake-gui.exe to compile: E:\VisualStudio2019\cMake\cmake-3.25.3-windows-x86_64\bin

The path in the first step is the source directory in the installed Opencv.

The path selection in the second step is chosen by myself. Here I choose build/x64/mingw32 under Opencv (where mingw32 is a newly created file).

The selection path of gcc.exe and g++.exe is: under the installed CodeBlocks: E:\CodeBlocks\CodeBlocks\MinGW\bin 

After selecting GCC/G++, click finish.

Tip: After configuring done above, click Generate again. Afterwards, the OpenCV library project file OpenCV.cbp in the Code::Blocks environment will be generated in the directory: E:\VisualStudio2019\Opencv-4-5-4\opencv\build\x64\mingw32 (the path selected above).

 

5. Compile the OpenCV library file

 (1) Method 1

        Double-click the OpenCV.cbp generated above, as follows:

Click Build => Build and run to compile. 

 (2) Method 2

        Enter E:\VisualStudio2019\Opencv-4-5-4\opencv\build\x64\mingw32, use the command line under windows to execute:

mingw32-make can

 Tip: Some warnings may be generated during the compilation process, don't worry too much, just continue to let it execute.

After executing mingw32-make, execute: mingw32-make install (some files related to OpenCV will be downloaded):

 

The downloaded files are stored in:

E:\VisualStudio2019\Opencv-4-5-4\opencv\build\x64\mingw32\install

After entering the install folder:

 

6. Dynamic link library file environment configuration

       The library file mentioned here is a file starting with lib. To facilitate setting the path name of the library file, replace lib with l in Code::Blocks, such as lopencv_core454.dll corresponds to the library file libopencv_core454.dll. The end 454 represents the version number of the library file.

After the above steps are executed, the directory

E:\VisualStudio2019\Opencv-4-5-4\opencv\build\x64\mingw32\lib和

E:\VisualStudio2019\Opencv-4-5-4\opencv\build\x64\mingw32\bin

Generate some dynamic link library files as follows:

Change the path E:\VisualStudio2019\Opencv-4-5-4\opencv\build\x64\mingw32\bin and

E:\VisualStudio2019\Opencv454\opencv\build\x64\mingw32\lib

Add to the system environment variable (Path):

 

 

7.CodeBlocks configuration

Tip: Open installed CodeBlocks

Tip: The path selected by the configuration below is:

E:\VisualStudio2019\Opencv-4-5-4\opencv\build\x64\mingw32\lib 

 

 

 

8. Write an example using OpenCV

 

 

 

#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>

using namespace cv;

int main()
{
    //读取一张图片
    Mat image = imread("images/GuiZhou.jpg");
    //创建一个名称为My Image的窗口
    namedWindow("My Image");
    //显示图片
    imshow("My Image",image);
    //设置显示等待时间
    waitKey(5000);
    return 0;
}

9. Abnormal program termination solution

Run the program (you may be surprised to get the following results) 

Tip: What's going on, why there is no error when compiling, but the program crashes directly when running. 

The solution is as follows:

 

The results show that:

 

expand

Since our configuration process in CodeBlocks above is global, that is to say, when we write OpenCV code next time, we can directly create the project in the same way as above. But if there is no global configuration in the above way, you can configure the OpenCV environment separately for each project file in the following way.

 

Reference link:

Digital Image Processing - Fourth Edition

https://zhuanlan.zhihu.com/p/26468685

Guess you like

Origin blog.csdn.net/Keep_Trying_Go/article/details/130367003