Installing OpenCV in Linux and configuration with Visual Studio Code

Ajay Girish
4 min readMar 30, 2021

There are many tutorials available for installing OpenCV in both Windows and Linux environment. A few describe the next step for configuration with Visual Studio Code on Windows platform, but configuration with Visual Studio Code on Linux platform is rarely found on the internet.

I decided to write this blog as I could not find a proper tutorial which explains everything in one place for Linux environment.

The tutorial assumes installation of Visual Studio is complete.

Install OpenCV

Step 1: Open terminal and navigate to home folder, alternatively from home folder open terminal as shown in the figure

Install Cmake : $ sudo apt install -y cmake

Install Make : $ sudo apt install -y make

Step 2: Download the current stable OpenCV version zip file from the GitHub page at https://github.com/opencv/opencv/tree/4.5.1 (The version at the time of writing this blog is 4.5.1)

Step 3: Unzip the file and copy contents to home folder.

Step 4 : Create a folder called ‘build’ inside the extracted OpenCV folder (in my case the folder was called ‘opencv 4.5.1')

Setp 5 : Open Cmake gui enter OpenCV folder address in ‘Where is my source code’ tab (See below picture for details)

Step 6 : Enter the address of ‘build’ folder created in step 4 in ‘Where to build libraries’ tab

Step 7 : Click Configure and after completion click generate

Step 8 : Go back to terminal and navigate to ‘build’ folder, enter the below commands one after the other,

make -j4

make install

sudo apt-get install libopencv-dev

Step 9 : Check if installation is successful,

pkg-config --modversion opencv4 (if installation is successful, openCV version will be printed)

Configuration with Visual Studio Code

Step1 : Create a new project in Visual Studio by entering the following commands in terminal (important to note : Create the project outside OpenCV folder, I am creating in home folder)

mkdir projects

cd projects

mkdir test

cd test

code . (this commands opens Visual Studio editor)

Step 2 : Create a new file in Visual studio code (I’m naming it main.cpp)

Step 3 : Copy the below code into main.cpp, and add a picture to the project folder and name it ‘Sample.png’

#include <opencv2/highgui.hpp>

#include <iostream>

int main( int argc, char** argv ) {

cv::Mat image;

image = cv::imread(“Sample.png” , cv::IMREAD_COLOR);

if(! image.data ) {

std::cout << “Could not open or find the image” << std::endl ;

return -1;

}

cv::namedWindow( “Display window”, cv::WINDOW_AUTOSIZE );

cv::imshow( “Display window”, image );

cv::waitKey(0);

return 0;

}

Step 4 : With the main.cpp file open, press ctrl+shift+p to open command pallet.

Enter C/C++ Edit Configurations(JSON) to open c_cpp_properties.json file

In the under includePath add “/usr/local/include/opencv4/**” as shown below.

Step 5 : Create another file in visual studio and name it ‘Makefile’ (without extensions) and add below contents to it

CC = “g++”

PROJECT = output

SRC = main.cpp

LIBS = `pkg-config — cflags — libs opencv4`

$(PROJECT) : $(SRC)

$(CC) $(SRC) -o $(PROJECT) $(LIBS)

Step 6 : Open a terminal by entering ctrl+shift+`, and type make to run the code and create an executable called ‘output’

Step 7 : Type ./output in the terminal to get the picture as the output.

--

--