├── .vscode ├── c_cpp_properties.json └── tasks.json ├── README.md ├── images └── screenshot.png ├── samples └── starry_night.jpg └── src └── testing.cpp /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Linux", 5 | "includePath": [ 6 | "${workspaceFolder}/**", 7 | "/usr/include/opencv4" 8 | ], 9 | "defines": [], 10 | "compilerPath": "/usr/bin/gcc", 11 | "cStandard": "gnu17", 12 | "cppStandard": "gnu++14", 13 | "intelliSenseMode": "linux-gcc-x64" 14 | } 15 | ], 16 | "version": 4 17 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "type": "cppbuild", 6 | "label": "C/C++: g++ build active file", 7 | "command": "/usr/bin/g++", 8 | "args": [ 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}/${fileBasenameNoExtension}", 13 | "`pkg-config", "--cflags", "--libs" ,"opencv4`" 14 | ], 15 | "options": { 16 | "cwd": "${fileDirname}" 17 | }, 18 | "problemMatcher": [ 19 | "$gcc" 20 | ], 21 | "group": { 22 | "kind": "build", 23 | "isDefault": true 24 | }, 25 | "detail": "compiler: /usr/bin/g++" 26 | } 27 | ] 28 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple OpenCV running example on Ubuntu and Visual Studio Code 2 | 3 | This is the minimal check to validate opencv development C++ on `Ubuntu 20.04` using `Visual Studio Code` 4 | 5 | ## Installation 6 | 7 | Install the **g++ compiler** 8 | 9 | ```bash 10 | $ sudo apt install build-essential 11 | ``` 12 | Validate the installation 13 | 14 | ```bash 15 | $ g++ --version 16 | g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 17 | Copyright (C) 2019 Free Software Foundation, Inc. 18 | This is free software; see the source for copying conditions. There is NO 19 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 20 | ``` 21 | 22 | Then install **OpenCV**. I prefer prebuilt binaries 23 | 24 | ```bash 25 | $ sudo apt install libopencv-dev 26 | ``` 27 | 28 | The library files should be located in `/usr/include/opencv4/opencv2` folder 29 | 30 | The last step is to install **visual studio code* via snap 31 | 32 | ```bash 33 | sudo snap install --classic code 34 | ``` 35 | 36 | ## Usage 37 | 38 | 1. Launch `visual studio code' 39 | 2. press `CTRL+SHIFT+X` to launch extensions tab 40 | 3. Install `C/C++` ,and `C++ intellisense` 41 | 4. Use `CTRL+SHIFT+E` to go to the explorer. Open src/testing.cpp 42 | 5. Click on `CTRL+SHIFT+B` to build the project. The code would compile in the tereminal window 43 | 44 | ```bash 45 | Executing task: C/C++: g++ build active file < 46 | 47 | Starting build... 48 | /usr/bin/g++ -g /home/wbadry/Desktop/simplecv/src/testing.cpp -o /home/wbadry/Desktop/simplecv/src/testing `pkg-config --cflags --libs opencv4` 49 | Build finished successfully. 50 | 51 | Terminal will be reused by tasks, press any key to close it. 52 | ``` 53 | 6. In the terminal window, you may run the executable `testing` 54 | 55 | ```bash 56 | $ ./testing 57 | ``` 58 | 59 | you should get an windows with gray image representing its `green color plane` 60 | 61 | ![screenshot of the IDE](images/screenshot.png) 62 | 63 | ## License 64 | [MIT](https://choosealicense.com/licenses/mit/) -------------------------------------------------------------------------------- /images/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wbadry/Simple-OpenCV-CPP-VSCode/976afbf8de12f842106236470243868cee1be273/images/screenshot.png -------------------------------------------------------------------------------- /samples/starry_night.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wbadry/Simple-OpenCV-CPP-VSCode/976afbf8de12f842106236470243868cee1be273/samples/starry_night.jpg -------------------------------------------------------------------------------- /src/testing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main(int argc, char** argv) 10 | { 11 | // read the image from samples folder 12 | cv::Mat img = cv::imread("../samples/starry_night.jpg", cv::IMREAD_COLOR); 13 | 14 | if(img.empty()) 15 | { 16 | std::cout << "Could not read the image ! " << std::endl; 17 | return 1; 18 | } 19 | 20 | // cerate an image holder to get the green channel 21 | cv::Mat channel; 22 | 23 | // Extract green plane 24 | cv::extractChannel(img, channel, 2); 25 | 26 | // Resize image for display 27 | cv::resize(channel, channel, cv::Size(), 0.20, 0.20); 28 | 29 | // Get a named window 30 | cv::namedWindow("Green Color plane"); 31 | 32 | // show the green plane (gray image) 33 | imshow("Green Color plane", channel); 34 | 35 | int k = cv::waitKey(0); 36 | return 0; 37 | } --------------------------------------------------------------------------------