Computer Vision and Image Processing with OpenCV
OpenCV ‘Open Source Computer Vision Library’ is an open-source library that includes several hundreds of computer vision algorithms. Using OpenCV, you could pretty much do every Computer Vision task that could ever be imagined!
Real-life problems require you to utilize multiple building blocks together in order to achieve the desired outcome. So, you just have to understand what modules and functions are needed to get what you want!
In the next few minutes, we shall quickly dive into what OpenCV can do out of the box!
Content Covered
The following topics have been precisely covered in the due course of the article —
- Installation and Importing
- Getting Started
- Edge Detection
- Feature Extraction
- Feature Matching
- Transformations
- Face Detection
- Working with Videos
- Additional Resources and References
Installation and Importing
Installation
Can be done on your local machine via Python command prompt
pip install opencv-python
Refer opencv-python· PyPI for troubleshooting
Importing OpenCV
import cv2 # OpenCV-Python
It’s as simple as that!
Getting Started
Open/display an image
Modify pixels & ROI
- You can access/modify a single-pixel or ROI (Region of Interest) using Numpy indexing.
- Just like matrix indexing,
img[a, b]
refer toa
-th row andb
-th column.
Code Implementation: https://github.com/tanvipenumudy/Winter-Internship-Internity/blob/main/Day%2005/Day-5%20Notebook-1%20(OpenCV%20Basics).ipynb
Edge Detection
We shall now detect edges using Sobel operator cv2.Sobel()
and Canny edge detectorcv2.Canny()
Edge Detection with Sobel Operator
dst = cv2.Sobel(src, ddepth, dx, dy, ksize=3, scale=1.0)
src
: input imageddepth
: output image depthdx
: order of the derivative xdy
: order of the derivative yksize
: size of the extended Sobel kernel; it must be 1, 3, 5, or 7scale
: optional scale factor for the computed derivative values
Canny Edge Detection
edges = cv2.Canny(image, threshold1, threshold2, apatureSize=3, L2gradient=False)
image
: 8-bit grayscale input imagethreshold1
/threshold2
: thresholds for the hysteresis procedureapertureSize
: aperture size for theSobel()
operatorL2gradient
: A flag.True
to use L2, else L1.
Code Implementation: https://github.com/tanvipenumudy/Winter-Internship-Internity/blob/main/Day%2005/Day-5%20Notebook-2%20(Edge%20Detection).ipynb
Feature Extraction
We shall now see how to extract SIFT (Scale-Invariant Feature Transform) and match SIFT features of two images with OpenCV-Python.
Extract SIFT features from an image
You need to initially convert the image into a greyscale image
Feature Matching
SIFT Feature Matching
Displaying Matches
Code Implementation: https://github.com/tanvipenumudy/Winter-Internship-Internity/blob/main/Day%2005/Day-5%20Notebook-3%20(Feature%20Extraction%20%26%20Matching).ipynb
Transformations
Let’s now see how to do basic operations on images such as — Scaling, Translation, Rotation, Affine Transformation and Homography (Perspective Transformation)
Scaling
Translation
Rotation
Affine Transformation
Homography
Code Implementation: https://github.com/tanvipenumudy/Winter-Internship-Internity/blob/main/Day%2005/Day-5%20Notebook-4%20(Transformations).ipynb
Face Detection
Face detection using ‘Haarcascades’ is a machine learning-based approach where a cascade function is trained with a set of input data. OpenCV already comprises of a good number of in-built classifiers for face, eyes, smiles, etc.
Code Implementation: https://github.com/tanvipenumudy/Winter-Internship-Internity/blob/main/Day%2005/Day-5%20Notebook-5%20(Face%20Detection).ipynb
Working with Videos
OpenCV library can be used to perform multiple operations on videos, apart from images which we’ve seen in the aforementioned sections.
Capture Video from Camera
To capture a video, we need to create a VideoCapture object
. VideoCapture has the device index or the name of a video file. Device index is just the number to specify which camera. If we pass 0 then it is for the first camera, 1 for the second camera so on. We capture the video frame by frame.
cv2.VideoCapture(0) # Means first camera or webcam
cv2.VideoCapture(1) # Means second camera or webcam
cv2.VideoCapture("file name.mp4") # Means video file
Playing Video from file
It is same as capturing from Camera, just change camera index with the video file name. Also while displaying the frame, use appropriate time for cv2.waitKey()
.
Saving a Video
So we capture a video, process it frame-by-frame and we want to save that video. For images, it is very simple, just use cv2.imwrite()
.
Here a little more work is required!
You can also access some of the features of this video using cap.get(propId) method where propId is a number from 0 to 18. Each number denotes a property of the video and full details can be seen here Property Identifier.
That’s all for today! I know that’s a lot to take in at once! But you made it until the end! Kudos on that!
Also, do not forget to go through — OpenCV Documentation
Additional Resources and References
OpenCV has been around for a while and there are a lot of other good resources if you’re still interested in getting the most out of this library.
- https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html
- https://github.com/dalgu90/opencv-tutorial
- https://towardsdatascience.com/computer-vision-for-beginners-part-1-7cca775f58ef
For the complete implementation, do check out my GitHub Repository —
GitHub Gists featured in this article —