Setting Up an OpenCV C Project - zlacey1234/LearningOpenCV GitHub Wiki

OpenCV C++ Project (VS Code)

Installing Visual Studio Code (VS Code) on Linux

This can be installed using the Ubuntu Software Application

VS Code Extensions

  • C/C++ (by Microsoft)
  • CMake Tools (by Microsoft)
  • Python (by Microsoft)
  • ROS (by Microsoft)

Setting Up an OpenCV Project

  1. Set up the Project Directory with the following folders:
  • bin
  • include
  • src
  1. Create task.json. To do this, Select:

Terminal --> Configure Default Build Task

Note: Terminal is a tab on the Top-bar of VS Code

  1. Create additions to the default task.json.
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",
			"label": "C/C++: g++ build active file",
			"command": "/usr/bin/g++",
			"args": [
				"-std=c++17",
				"-g",
				"${workspaceFolder}/src/**.cpp",
				"-o",
				"${workspaceFolder}/bin/main",
				"-I/usr/local/include/opencv4", 
				"-L/usr/local/lib", 
				"-lopencv_photo",
				"-lopencv_stitching",
				"-lopencv_highgui", 
				"-lopencv_videoio", 
				"-lopencv_imgcodecs", 
				"-lopencv_objdetect", 
				"-lopencv_dnn", 
				"-lopencv_gapi", 
				"-lopencv_ml", 
				"-lopencv_video", 
				"-lopencv_calib3d", 
				"-lopencv_features2d", 
				"-lopencv_imgproc", 
				"-lopencv_flann", 
				"-lopencv_core",
				"-lrealsense2"
			],
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			}
		}
	]
}
  1. To Build using this task.json Ctrl+Shift+B
  2. Create launch.json Ctrl+Shift+D and then Press Run
  3. Create additions to default launch.json.
{
    // 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}/bin/main",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

Note: ${workspaceFolder}/bin/main is the path the executable file main. This executable does not have to be called main. We can specify a different executable name by altering it in the task.json.

  1. Create c_cpp_properties.json
Ctrl+Shift+P

Type: Config. Choose C/C++: Edit Configurations (JSON)

  1. Create additions to the default c_cpp_properties.json.
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/opencv4/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}