How to install OpenCV for C++ in Visual Studio code for Ubuntu OS

Vignesh Kathirkamar
Analytics Vidhya
Published in
5 min readAug 16, 2021

--

While searching out for “how to install OpenCV for Visual studio code using Ubuntu 18.04 OS ” there were only very few resources. So, after spending around few hours I managed to install the OpenCV for Visual studio in a linux OS, by going through this article. Though the article was covering the core concept of how to install, it wasn’t clear for beginners like me. So, I am reproducing a version of mine, such that it would be useful for beginners like me, who are interested in having the taste of OpenCV using C++ using their favorite Visual Studio Code IDE.

Part 1 : Installing OpenCV

Step1: Open your terminal and type in the following commands as shown below in the pic. (i.e move to your home directory using “cd” command and install “cmake” and make”)

cd
sudo apt install -y cmake
sudo apt install -y make
Installation of cmake and make

(since I had installed them already, I got the message as shown above in the console)

Step2: Download the latest stable version of the OpenCV file from the github page https://github.com/opencv/opencv/tree/4.5.1

While writing this article OpenCV 4.5.1 was the latest stable version.

Go to the link mentioned and better do the git clone method as shown below in the home folder

git clone https://github.com/opencv/opencv.git
git clone opencv

After successful git clone, you should find a folder called opencv in your home folder

Step3: Now go into the opencv folder and create a folder called build

cd opencv && mkdir build

Step4: Now install cmake gui using the following command

sudo apt-get install cmake-qt-gui

once it is done, open the cmake gui using the command “cmake-gui”

cmake-gui

Now you should get the window as shown below

cmake-gui

As shown in the above figure, for “Where is the source code: ” dialog box, paste the path of your opencv file and for “Where to build the binaries: ”, copy paste the path of the build directory within the opencv directory (which we created at the starting of step3)

Step5: Now click configure and then click generate. Don’t be intimidated by the values in red that appears in the gui, just click configure and once it is completed click generate

Step6: Go back to terminal now and move to the build folder within the opencv folder and enter the commands one by one as shown in the below snippet. Sit back and relax as “make install” is going to take some significant amount of time

cd ~/opencv/build
make -j4
make install
sudo apt-get install libopencv-dev

Step7: Once the above step is over, now it’s time to check if the installation is successful. This can be done using the command “pkg-config — modversion opencv4”

pkg-config --modversion opencv4
checking version of opencv package

if you are facing any errors while executing the above command, then go back to the build folder cd ~/opencv/build and try the following command and then try “pkg-config — modeversion opencv4”

cd ~/opencv/buildcmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_INSTALL_PREFIX=/usr/local ..sudo make 
sudo make install
pkg-config --modversion opencv4

Part : 2 Configuration with Visual Studio code

Step1: Open the visual studio code -> create a new folder (say “project”) -> create a new cpp file (say “new.cpp”)

Step2: Copy paste a picture (say “Lena.jpg”) into the new folder created

Now our folder structure should look similar to the one below

project
|--new.cpp
|--Lena.png

Step3: Copy paste the below code and don’t forget to change the image name if required and also make sure to check if there are any syntax errors are present before moving further

#include <opencv2/highgui.hpp>
#include <iostream>
int main( int argc, char** argv )
{
cv::Mat image;
image = cv::imread("Lena.png",cv::IMREAD_COLOR);
if(! image.data)
{
std::cout<<"Could not open file" << std::endl;
return -1;
}
cv::namedWindow("namba image", cv::WINDOW_AUTOSIZE);
cv::imshow("namba image", image);
cv::waitKey(0);
return 0;
}

Step4: With the main.cpp file open press “ctrl+shift+p”, this will open the command pallet, In that select “C/C++: Edit Configurations(JSON)” to open “c_cpp_properties.json” as shown below

Step5: In “c_cpp_properties.json” edit the IncludePath to add

“/usr/local/include/opencv4/**” as shown below

Step6: Now create another file and name it as “Makefile”, our folder structure should be something similar to what shown below in the snippet

project
|--new.cpp
|--Lena.png
|--Makefile

Inside the Makefile type the below code. Good to type the code below instead of copy pasting since that may result in error.

(Note: Make sure that the last line has a tab character at the beginning instead of four spaces while you type the command)

CC = g++
PROJECT = new_output
SRC = new.cpp
LIBS = `pkg-config --cflags --libs opencv4`
$(PROJECT) : $(SRC)
$(CC) $(SRC) -o $(PROJECT) $(LIBS)

Step7: Now go to the terminal in the VS code and type “make”. This should give you an executable called “new_output”

Step8: Now execute ./new_ouput. This should pop our image as shown below

If this article had helped you, don’t forget to leave two claps. Feel free to post if any errors faced in the comment section. Thank You.

Regards,

Vignesh Kathirkamar

AI Pylinux

--

--