vscode builds the OpenCV environment (vscode already exists by default)

 
 

@[TOC] (write the title of the directory here)

# 1. File download:

## 1. MinGW download

## 2. Cmake download

## 3. Opencv download

# Two. Configure environment variables:

# Three.vscode configuration

## 1、launch.json

## 2、c_ cpp_ properties json

## 3、tasks json

# Four. Test

1. File download

1. MinGW download ( MinGW-w64 - for 32 and 64 bit Windows - Browse Files at SourceForge.net )

Create a new folder in a separate disk. The name is optional. My (MinGw) download will be completed. The first file in this folder is the file after decompression.

2. Cmake download ( https://cmake.org/files/v3.20/ )

        Select this version to download and create a new folder as above to store cmake. My (cmake) first is the decompressed file.

 3. Opencv download ( https://github.com/huihut/OpenCV-MinGW-Build )

Since the source code of opencv is compiled with mingw, there are quite a lot of pitfalls. Here, download the compiled one directly to save those troubles. After downloading and decompressing, you will get these two folders.

2. Configure environment variables

 1. Configure environment variables

        open control panel

1. Add the environment variable of MinGW: (OK after clicking)

There will be a blank space after adding the following path! ! !

2. Add the environment variable of cmake: (select the file and click OK)

 This path appears to indicate success

3. Click on the blank space before adding the OpenCV environment variable

The appearance of this path indicates success and finally confirms that the environment variable configuration is complete

5. Check whether the installation configuration is complete. Ctrl+R, enter cmd, and bring up the cmd command window.  

   Type: gcc -v

It shows that the MinGw installation and configuration is complete.

Enter: cmake -version

Explain that cmake can only be installed and configured

3. vscode configuration

To open vscode, you need to install the corresponding extension

 You can create a new folder dedicated to the OpenCV project

        To create a new main.cpp file in vscode, you must first create a .vscode folder with a dot in front. This folder stores 3 json configuration files, and the 3 json files must be put into .vscode! ! !

 Add this code in main.cpp

/********************** Display the picture of the specified address ********************* *********/

#include<iostream>

#include<opencv2/core/core.hpp>

#include<opencv2/highgui/highgui.hpp>

using namespace std;

using namespace cv;

int main(int argc, char** argv)    

{                                  

    Mat image;

    image = imread("D:\\OpenCV\\z23.jpg");

    if (image.data == nullptr) //nullptr is a new null pointer constant in c++11

    {

        cout << "The picture file does not exist" << endl;

    }

    else

    {

        //display image

        imshow("meinv", image);

        waitKey(0);

    }

    // Output the basic information of the image

    cout << "The image width is:" << image.cols << "\tHeight is:" << image.rows << "\tThe number of channels is:" << image.channels() << endl;


 

    // loop through each pixel

    //The reason why the name y is used to represent the row is because the row number in the coordinate system of the picture is y

    for (size_t y = 0; y < image.rows; y++)

    {

        unsigned char* row_ptr = image.ptr<unsigned char>(y);

        for (size_t x = 0; x < image.cols; ++x) {

            //This is the head pointer to obtain the pixel data array, note that the pixel data may have multiple channels, so it needs to be stored in an array

            unsigned char* data_ptr = &row_ptr[x * image.channels()];

            // Output the color value of the current pixel channel by channel

            for (int i = 0; i < image.channels(); ++i) {

                cout << int(data_ptr[i])<<endl;

            }

        }

    }

    system("pause");

    return 0;

}

Press F5 to run to generate a folder .vscode and then configure three files in it 1. launch.json 2, c_ cpp_ properties json 3, tasks json   

1. launch.json

// {

// // Use IntelliSense for related properties.

// // Hover to see descriptions of existing properties.

// // For more information visit: https://go.microsoft.com/fwlink/?linkid=830387

//     "version": "0.2.0",

//     "configurations": []

// }

{

    // Use IntelliSense to learn about possible attributes.

    // Hover to view descriptions of existing attributes.

    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387

    "version": "0.2.0",

    "configurations": [

       

        {

            "name": "(gdb) Launch",

            "type": "cppdbg",

            "request": "launch",

            "program": "${workspaceFolder}/${fileBasenameNoExtension}.o",

            "args": [],

            "stopAtEntry": false,

            "cwd": "${workspaceFolder}",

            "environment": [],

            "externalConsole": true,

            "MIMode": "gdb",

            "miDebuggerPath": "D:\\MinGw\\MinGw\\bin\\gdb.exe",//Modify here

            "preLaunchTask": "g++",

            "setupCommands": [

                {

                    "description": "Enable pretty-printing for gdb",

                    "text": "-enable-pretty-printing",

                    "ignoreFailures": true

                }

            ]

        },

    ]

}

2、c_ cpp_ properties json

{

         "configurations": [

             {

            "name": "Win",

               "includePath": [

                     "${workspaceFolder}/**",

                     "D:\\OpenCV\\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\\include",//modify here

                     "D:\\OpenCV\\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\\include\\opencv2"//Modify here

                    // "F:\\Tools\\opencv\\build\\include\\opencv"                

                ],

                "defines": [],

                "compilerPath": "D:\\MinGw\\MinGw\\bin\\gcc.exe",//Modify here

                "cStandard": "c11",

                //"cStandard": "c17",

                "cppStandard": "c++17",

                "intelliSenseMode": "clang-x64"

                //"intelliSenseMode": "windows-gcc-x64"

                //"intelliSenseMode": "${default}"

            }

        ],

        "version": 4

    }

3、tasks json
 

{

         // See https://go.microsoft.com/fwlink/?LinkId=733558

         // for the documentation about the tasks.json format

         "version": "2.0.0",

         "label": "g++",

         "command": "g++",

        //"command": "D:\\MinGw\\MinGw\\bin\\g++.exe",//Modify here

         "args": [

            "-g",

             "-std=c++11",

             "${file}",

            "-o",

            "${fileBasenameNoExtension}.o",  

            "-I","D:/OpenCV/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/include",//Modify here

            "-I", "D:\\OpenCV\\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\\include\\opencv2",//Modify here

            "-L","D:/OpenCV/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/x64/mingw/lib",//Modify here

            "-L","D:/OpenCV/OpenCV-MinGW-Build-OpenCV-4.5.2-x64/x64/mingw/bin",//Modify here

           

            "-l","libopencv_calib3d452", //Modify the last three digits of each version here

           

            "-l","libopencv_core452",

           

            "-l","libopencv_dnn452",

           

           "-l","libopencv_features2d452",

           

            "-l","libopencv_flann452",

           

            "-l","libopencv_gapi452",

           

            "-l","libopencv_highgui452",

           

            "-l","libopencv_imgcodecs452",

           

            "-l","libopencv_imgproc452",

           

            "-l","libopencv_ml452",

           

            "-l","libopencv_objdetect452",

           

            "-l","libopencv_photo452",

           

            "-l","libopencv_stitching452",

           

            "-l","libopencv_video452",

           

            "-l","libopencv_videoio452",

           

               

        ], // compile command parameters

        "problemMatcher":{

            "owner": "cpp",

            "fileLocation":[

                "relative",

                "${workspaceFolder}"

            ],

            "pattern":[

                {

                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",

                    "file": 1,

                    "location": 2,

                    "message": 3

                }

            ]

        },

        "group": {

            "kind": "build",

            "isDefault": true

        },

       

    }

Press Ctrl+F5 to run  创建的text.cpp文件and display a picture of your choice:

It shows that vscode has successfully configured the OpenCV environment

Thanks to the bloggers for their help, you can also refer to other bloggers:

VScode builds Opencv (C++ development environment)

VsCode installation and configuration c/c++ environment (super complete, Xiaobai special)

Windows platform uses vscode to configure opencv

Windows system [VSCode builds OpenCV, C++ development environment] - A Small Tree x's Blog - CSDN Blog

[Image processing] c++ uses opencv to read pictures

Vscode configures opencv_Wddfh_frw's blog-CSDN blog_opencv in vscode

Guess you like

Origin blog.csdn.net/qq_51243202/article/details/127341405