Python OpenCV Tutorial
OpenCV stands for Open Source Computer Vision Library. It is a free, open source library which is used for computer vision. It provides good support in Machine Learning, Face Recognition, Deep Learning, etc. This library is written in optimized C/C++ and is supported in Windows, Linux, Android and MacOS.
The OpenCV Module is being used for a very wide range of applications-
- Object Identification
- Facial-recognition system
- Motion understanding
- Mobile Robotics
- Gesture Recognition
- Stereopsis stereo vision
Install Python Virtual Environment
OpenCV needs Python. We hope you have already installed it, and if you do not, please follow our this tutorial.
Before installing OpenCV, it is recommended to install virtual environment (virtualenv). It provides a new virtual environment for your project and creates your own directory.
Activate Virtual Environment
You have seen a new folder 'env', is created in the 'projects' folder. The subfolder 'Scripts' contains activation file. The OpenCV project needs to activate the virtual environment first. So, we need to activate this using the following commands.
c:\Python38\Scripts\projects>cd env/Scripts c:\Python38\Scripts\projects\env\Scripts>activate (env) c:\Python38\Scripts\projects\env\Scripts>
The above command modifies the shell prompt and indicates which virtual environment is currently active.
Install OpenCV in Python
Here is the command to install the OpenCV module with pip tool.
pip install opencv-python
On successful installation, it returns the following -
Collecting opencv-python Downloading opencv_python-22.214.171.124-cp37-cp37m-win_amd64.whl (33.0 MB) |████████████████████████████████| 33.0 MB 48 kB/s Requirement already satisfied: numpy>=1.14.5 in c:\python37\scripts\projects\env\lib\site-packages (from opencv-python) (1.18.1) Installing collected packages: opencv-python Successfully installed opencv-python-126.96.36.199
The above command installs OpenCV without any other modules. As OpenCV module requires some other modules, so instead of the above command we can also use the following -
pip install opencv-contrib-python
We can perform multiple operations using OpenCV. But first, we need to know how to open a file for performing the operation.
The imread() function loads an image from the specified location and can handle a wide range of image formats.Syntax -
Here, path represents the path of the image that is to be read and the flag is an optional parameter and can have one of the following possible values-
- cv2.IMREAD_COLOR - It specifies to load a color image.
- cv2.IMREAD_GRAYSCALE - It specifies to load an image in gray color mode.
- cv2.IMREAD_UNCHANGED - It specifies to load an image in RGB (Red, Green, Blue) or ARGB (RGB along with alpha or transparency) channel mode.
Example of imread()
import cv2 # image path path = r'nature.jpg' # using imread() img = cv2.imread(path) # displaying the image cv2.imshow('image', img) cv2.waitKey(0); cv2.destroyAllWindows(); cv2.waitKey(1)
The above code returns the following output -
The imwrite function saves an image to the specified location.Syntax
Here, path is the complete path of the output file and the image is the image that to be saved. The above function returns a boolean value.
Example of imwrite()
import cv2 # image path path = r'nature.jpg' # using imread() img = cv2.imread(path) h,w = img.shape[:2] print("Height & Width of Image: ",h,w) # save image imwrt = cv2.imwrite('result.png',img) if imwrt: print('Image is successfully saved.')
The above code returns output something like this -
(env) c:\python37\Scripts\projects>opencv1.py Height & Width of Image: 295 432 Image is successfully saved.
Related ArticlesOpenCV color detection
OpenCV bitwise and
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
OpenCV body tracking
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples