OpenCV4 environment configuration

0.Install mingw64

Official website link: mingw

  1. Install the red box mark to download the installation-free version, which can be decompressed.
    image-20230809214627758
  2. Add the decompressed mingw64\bin to the path environment variable
  3. Enter gcc -v in cmd, and the following figure appears, indicating that the configuration is successful.

image-20230809214807503

1. Download the OpenCV source code

Source code download

Official website: Releases - OpenCV

image-20230808164909641

Run the downloaded exe file and specify the storage path of the source code.

The downloaded source code is shown below

image-20230808165216872

Extension file download

Download the official website: Tags · opencv/opencv_contrib (github.com)

After downloading, unzip and place it in the OpenCV path

image-20230809230948205

2.Install CMAKE

Compiled OpenCV source code: https://pan.baidu.com/s/1NF5PElfjJT49pnpqgQy3zg?pwd=ksz0

If you use what I compiled, jump directly to 4.5, configure the environment variables, and then enter clion for testing.

The mingw-build folder is used, you can delete the VS-build folder.

The VS-build folder is built using VS. If you don’t use it, you can delete it (VS-build). The vsbuild file has more than ten G.

Official website: Download | CMake

image-20230808170430046

Run the installation file, and you need to check Add environment variables in the middle.

image-20230808170627223

Change installation path

image-20230808170701742

3.ckame compile OpenCV

  1. Create a new mingw-build folder in the OpenCV source code path
  2. Copy the opencv_videoio_ffmpeg470_64.dll file in the build/bin directory to the sources/3rdparty/ffmpeg directory
  3. Check in cmake, as shown in the figure below, select the file path

image-20230808171506788

  1. Click configure, select Mingw Makefile in the pop-up window, the compiler default will be fine

image-20230808223910146

  1. When the first compilation is completed, the content with a red background appears. Check the content shown in the picture below, and then continue to click configure.

image-20230809211613677

image-20230808231857217

image-20230809231055252

  1. Click configure multiple times until no more red appears.

An error may be reported during downloading.

FFMPEG: Download failed: 28;“Timeout was reached”

Solution:

  1. Copy the download link and enter it into the browser to download

  2. Open the .cache folder in the source folder of opencv

  3. Copy the name of the file that was not downloaded successfully, and rename the file that was successfully downloaded on the webpage to the name you just copied.

  4. Place the renamed files into each folder under the .cache folder, and replace the original files [How to confirm the location of the files: There is a corresponding folder name in the download link]

  5. Click configure again

image-20230808235948300

  1. Click generator

image-20230809000016173

4. Install compiled files

  1. Enter the mingw-build folder
  2. Enter cmd on the path and press Enter (it is recommended to run directly as an administrator, running in normal mode may cause some errors)

image-20230809000210918

image-20230809000229036

  1. entermingw32-make -j 16

Error resolution:

  1. If it appears error: 'recursive_mutex' in namespace 'std' does not name a type, re-download the posix version of mingw64, and then recompile the OpenCV source code in cmake
  2. If undefined reference to _Unwind_Resume'something like this occurs, run cmd as administrator and recompile.

image-20230809213424554

  1. If an error like this occurs Anaconda3-/hdf5, do not use the Python environment of Anaconda3 and replace it with other Python environments or do not use the Python environment directly (directly delete the Python configuration of Anaconda in the path environment change, replace it with other Python or do not configure Python), and use cmake again. Compile and then run in cmd

image-20230810110217317

image-20230809213446910

  1. entermingw32-make install

image-20230809213644437

  1. Add environment variables and add the mingw-build\bin directory to the system environment variables

image-20230809221112296

5.Clion configuration and use

  1. Create a new project
  2. Copy the following content CMakeLists.txtinto
set(OpenCV_DIR E:/kaifa/C/opencv/mingw-build/install) #这里刚才编译的OpenCV的路径
find_package(OpenCV REQUIRED)
target_link_libraries(OpenCVtest ${OpenCV_LIBS}) #OpenCVtest是你的项目名称

image-20230809215512469

  1. Run the main.cpp file
#include "iostream"
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main() {
    
    

    Mat img = imread("E:/C-Project/OpenCVtest/1.jpg");
    cout << img.type() << endl;
    if (img.empty()) {
    
    
        cout << "Error" << endl;
        return -1;
    }
    imshow("Lena", img);
    waitKey();
    return 0;

}
  1. A pop-up picture indicates that the environment configuration is successful.

Guess you like

Origin blog.csdn.net/qq_61228493/article/details/132396659