Opencv Visual Studio Code
Goals
In this tutorial
- We will learn to setup OpenCV-Python in your Windows system.
Below steps are tested in a Windows 7-64 bit machine with Visual Studio 2010 and Visual Studio 2012. The screenshots shows VS2012.
Open the file explorer and navigate to the following path: '%OCV2015ROOT%vs2015WS10.0x86', then open OpenCV.sln in Visual Studio. On the top of the screen, next to the green Run button, select Release instead of Debug and Win32 instead of ARM or x64. Installation of OpenCV in Visual Studio Environment.Originally was created for Image Processing class, Robotics Department, Nazarbayev University, Kazakhstan.
Installing OpenCV from prebuilt binaries
- Below Python packages are to be downloaded and installed to their default locations.
- Python 3.x (3.4+) or Python 2.7.x from here.
- Numpy package (for example, using
pip install numpy
command). - Matplotlib (
pip install matplotlib
) (Matplotlib is optional, but recommended since we use it a lot in our tutorials).
- Install all packages into their default locations. Python will be installed to
C:/Python27/
in case of Python 2.7. - After installation, open Python IDLE. Enter import numpy and make sure Numpy is working fine.
- Download latest OpenCV release from GitHub or SourceForge site and double-click to extract it.
- Goto opencv/build/python/2.7 folder.
- Copy cv2.pyd to C:/Python27/lib/site-packages.
- Open Python IDLE and type following codes in Python terminal. >>> print( cv.__version__ )
If the results are printed out without any errors, congratulations !!! You have installed OpenCV-Python successfully.
Building OpenCV from source
- Download and install Visual Studio and CMake.
Download and install necessary Python packages to their default locations
- Python
- Numpy
- Note
- In this case, we are using 32-bit binaries of Python packages. But if you want to use OpenCV for x64, 64-bit binaries of Python packages are to be installed. Problem is that, there is no official 64-bit binaries of Numpy. You have to build it on your own. For that, you have to use the same compiler used to build Python. When you start Python IDLE, it shows the compiler details. You can get more information here. So your system must have the same Visual Studio version and build Numpy from source.
- Another method to have 64-bit Python packages is to use ready-made Python distributions from third-parties like Anaconda, Enthought etc. It will be bigger in size, but will have everything you need. Everything in a single shell. You can also download 32-bit versions also.
- Make sure Python and Numpy are working fine.
- Download OpenCV source. It can be from Sourceforge (for official release version) or from Github (for latest source).
- Extract it to a folder, opencv and create a new folder build in it.
- Open CMake-gui (Start > All Programs > CMake-gui)
- Fill the fields as follows (see the image below):
- Click on Browse Source... and locate the opencv folder.
- Click on Browse Build... and locate the build folder we created.
Click on Configure.
It will open a new window to select the compiler. Choose appropriate compiler (here, Visual Studio 11) and click Finish.
- Wait until analysis is finished.
You will see all the fields are marked in red. Click on the WITH field to expand it. It decides what extra features you need. So mark appropriate fields. See the below image:
Now click on BUILD field to expand it. First few fields configure the build method. See the below image:
Remaining fields specify what modules are to be built. Since GPU modules are not yet supported by OpenCV-Python, you can completely avoid it to save time (But if you work with them, keep it there). See the image below:
Now click on ENABLE field to expand it. Make sure ENABLE_SOLUTION_FOLDERS is unchecked (Solution folders are not supported by Visual Studio Express edition). See the image below:
Also make sure that in the PYTHON field, everything is filled. (Ignore PYTHON_DEBUG_LIBRARY). See image below:
- Finally click the Generate button.
- Now go to our opencv/build folder. There you will find OpenCV.sln file. Open it with Visual Studio.
- Check build mode as Release instead of Debug.
- In the solution explorer, right-click on the Solution (or ALL_BUILD) and build it. It will take some time to finish.
Again, right-click on INSTALL and build it. Now OpenCV-Python will be installed.
- Open Python IDLE and enter 'import cv2 as cv'. If no error, it is installed correctly.
- Note
- We have installed with no other support like TBB, Eigen, Qt, Documentation etc. It would be difficult to explain it here. A more detailed video will be added soon or you can just hack around.
Additional Resources
Exercises
If you have a windows machine, compile the OpenCV from source. Do all kinds of hacks. If you meet any problem, visit OpenCV forum and explain your problem.
This Vs Code extension provides the user with Opencv snippets in python. It helps in effectively providing, refactoring and editing the opencv projects in python3. All the snippets starts with 'o', so typing a letter o gives recommendation for all the available opencv snippets.
Example -
This Example detects corner in the image.
This Example detects shapes in the image.
From the examples above, it can be easily seen that how easy and fast it become to code in Opencv by using this extension. It also saves you from any syntactical mistakes and increase your coding speed by many folds.
Installation:
This Extension can be installed from the Visual Studio Code Marketplace or by searching within VS Code.
In VS code press ctrl+p and type ext install extensionName and press enter.
Snippets:
Opencv Visual Studio Code Python

Trigger | Full Form | Description |
---|---|---|
orimg | Reading Image | Reads image from the computer |
owimg | Write Image | Write image in the computer |
odis | Display Image | Display Image |
orvid | Read Video | Read Video from camera or a Video File |
owvid | Write Video | Write Video in the computer |
oline | Draw Line | Draw Line on the image |
oaline | Draw Arrowed Line | Draw arrowed line on the image |
orect | Draw Rectangle | Draw Rectangle on the image |
ocircle | Draw Circle | Draw Circle on the image |
oellipse | Draw Ellipse | Draw Ellipse on the image |
opoly | Draw Closed Polygon | Draw any Polygon on the image |
otext | Write Text | Put any text on the image |
olistevents | Listing Events | List all the events available in opencv |
ohmevent | Handling Mouse Events | Provide basic Layout to handle all mouse events |
octrack | Create Trackbar | Creates trackbar on any window |
otrackpos | Get Trackbar Position | Gives the position of trackbar at any time |
ocvtcolor | Convert Color | Converts color from one format to other |
ohsvcoloriden | Identify Color using HSV | Identifies any color in the image |
othresh | Thresholding | Returns a threshold Image |
oathresh | Adaptive Thresholding | Returns Adaptive thresholded Image |
odilate | Dilate Image | Returns a Dilated image |
oerode | Erode Image | Returns a Eroded image |
omorphex | Morphological Transformations | Removes noises from the image by performing morphological transformations on it. |
oblur | Blur Image | Used for blurring or smoothening of image |
ogblur | Gaussian Blur | Returns Gaussian blur of a image |
omedblur | Median Blur | Used for smoothening of image using Median blur |
obblur | Bilateral Blur | Used for smoothening of image using Bilateral blur |
olapedge | Laplacian Edge Detector | Edge Identification using Laplacian Edge Detector |
osobedge | Sobel Edge Detector | Edge Identification using Sobel Edge Detector |
ocanny | Canny Edge Detector | Edge Identification using Canny Edge Detector |
ocontours | Contours | Identifies and draw contours on the image |
oshapedetect | Shape Detection | Detects shapes present in the image and writes the name of the shape in the image. |
omatchtem | Template Matching | Matches template in any main image and draws rectangle around the identified template in the main image |
ohough | Hough Line Transformation | Detects straight lines of infinite length in the image. |
ophough | Probabilistic Hough Line Transform | Detects straight lines of finite length in the image. |
ohaarc | Haar Cascades Classifier | Provide Layout for using Haar Cascades Classifier |
oharrisc | Harris Corner Detector | Detects corner in the Given image (Harris Corner Detector) |
ogoodf | Shi-Tomasi Corner Detector | Detects desired number of corners in the Given image (Shi-Tomasi Corner Detector) |
opers | Perspective Transformation | Returns the perspective transformation of the image |
oaffn | Affine Transformation | Returns the Affine Transformation of the image |
orotate | Rotate Image | Rotates the image by desired angle |
Requirements
- Python3 should be installed in the system.
For installing python latest version you can head over to python offcial website.
- Opencv library of python. This can be installed by executing the following command in the terminal or cmd.
- Numpy library of python. This can be installed by executing the following command in the terminal or cmd.
This need not be compulsorily installed but it is advised to install it because there are various snippets which require this library for better and smooth performance.
Issues
Report issues and bugs at Opencv Snippets Issues
Release Notes
The detailed release notes of Opencv Snippets can be found at -
v1.2.2
- Bug fixes.
- Keywords added.
- Added more snippets.
v1.2.1
Opencv With Visual Studio
- Improvements and bug fixes.
- Code cleaned to make it more readable.
- Comments added.
v1.2.0
- More Snippets added.
- Better handling of triggers.
- Code quality improvements.
- Code should run faster now.

v1.1.2
- Reformating of code.
- Minor bug fixes.
v1.1.1
- Added gifs and icons.
- readme prepared.
v1.1.0
- Added Multiple Snippets.
- Snippets made more generic.
- Code modularized.
v1.0.0
- Initial Release of Opencv-Snippets
- Basic Layout of the project setup
- Added Image and Video read/write snippets.

License

Opencv Snippets is an open-sourced software licensed under the GNU GENERAL PUBLIC LICENSE.
About Me
- You can connect with me at linkedin:
- You can follow me on github:
Opencv In Visual Studio Code
For more information
This is a open Source project, so for more information and contribution you can checkout:
Enjoy!
