Beginner’s guide to OpenCV in Python
Introduction
OpenCV is a library of computer vision algorithms for Python. It is a collection of programming functions and classes that implement various computer vision applications. In this blog, we will cover the basics of OpenCV in Python and provide an example code along with the output for each task.
What is OpenCV?
OpenCV is a library of functions for real-time computer vision. It has bindings for Python, Java, MATLAB, R and other languages. The core components of OpenCV are:
- The video processing class provides methods for capturing video frames from various camera devices such as USB webcams or remote control cameras like a Kinect sensor on the Xbox 360 video game console. The video processing class also provides functions to modify images before they are written out to disk or displayed onscreen.
- The Mat object type represents 2D data that can be processed using one or more filters (also known as operations). An image mat object can contain any number of channels, each containing pixels with values between 0 and 255 inclusive
- Methods that perform image processing operations such as edge detection, blob detection (called Haar cascade in object-oriented parlance), colourization/desaturation etc.; all these methods return new images which contain processed results at specific locations within the original image frame (called bounding boxes)
Define: OpenCV
OpenCV is an open-source computer vision and machine learning software library. It’s written in C++, with a Python interface. OpenCV was initially developed as part of the Intel Parallel Studio XE, but it has since expanded to include support for other programming languages aside from C++.
OpenCV can be used for real-time applications like surveillance, face recognition, object tracking and much more.
Python OpenCV basics and relevant keys
OpenCV is an open-source computer vision library, written in C and Python. It’s a popular choice for machine-learning algorithms and image-processing tasks.
Here are some basic concepts you should know before diving into OpenCV programming:
- OpenCV is a library that can be easily integrated with Python.
- Import the library by typing “import cv2” in your code.
- The key functions in OpenCV include reading and writing images, image manipulation, and feature detection.
- Image preprocessing with the -filter flag (like operator overloading) will be explained later when we get our first example code block. For now, just keep in mind that this command allows us to modify images before their input gets passed through any other bits of processing such as feature extraction or classification algorithms like Convolution Neural Network (CNN). For example, we can use the -filter flag to blur an image or replace its colors with black and white.
Loading libraries
To use OpenCV, you will need to import the library using the following code:
import cv2
from google.colab.patches import cv2_imshow
import numpy as np
Code language: Python (python)
Reading an image
The first step is to import the OpenCV library.
Now, the first step in image processing is to read an image. Use the imread()
function to read an image.
This image is used in the illustration of every step.
img = cv2.imread("Bridge.jpg")
Code language: Python (python)
Additionally, to display the image, you can use the imshow()
function and waitKey()
function from OpenCV.
cv2_imshow("Image", img)
cv2.waitKey(0)
Code language: Python (python)
Output
The imshow()
function is used to display the image on the screen, and the waitKey()
function is used to hold the image on the screen until any key is pressed.
Writing on image
You can use the built-in OpenCV library to load an image and draw on it. The first step is to import cv2, which provides functions for reading image data from files and cameras, as well as for transforming images between different formats.
After importing cv2, you’ll need to import NumPy as Np. This package contains functions for working with arrays of numbers (like adding numbers together). Now, to write text on an image, you can use the putText()
function.
cv2.putText(img, "Hello World!", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
Code language: Python (python)
Further to display this image use imshow()
and waitkey()
as mentioned above.
Blurring image
Blurring an image is a simple way to reduce noise and make your image clearer. The method for blurring an image is as follows:
img = cv2.blur(img, (5, 5))
Code language: Python (python)
This code will create blurred versions of our original image.
Output
Drawing a circle on the image
You can use the circle() function to draw circles on images. The rest of this tutorial assumes that you’ve already installed OpenCV 3.0 onto your computer by following our installation guide here:
cv2.circle(img, (100, 100), 50, (0, 0, 255), 2)
Code language: Python (python)
Output
Checking RGB pictures of the original picture
To check the RGB values of an image, you can use the split() function.
r, g, b = cv2.split(img)
print(f"Red = {r}, Green = {g}, Blue = {b}")
Code language: Python (python)
Output
Convert Images into Cartoon images
To convert an image into a cartoon image, you can use the medianBlur()
function and the bilateralFilter()
function.
img = cv2.medianBlur(img, 5)
img = cv2.bilateralFilter(img, 9, 75, 75)
Code language: Python (python)
Output
Canny edge detection using OpenCV
To detect edges in an image, you can use the Canny() function.
edges = cv2.Canny(img, 100, 200)
Code language: Python (python)
Output
Extracting the Region of Interest
To extract a specific region of an image, you can use the region of interest (ROI) function.
roi = img[50:150, 50:150]
Code language: Python (python)
Output
Displaying text
The first step to displaying text is to import the cv2 module. The next step is to import the NumPy module and use it as a data type in your code.
Now that you have all these libraries installed on your computer or device (if they aren’t already), let’s look at how we can display text in OpenCV.
To display text on an image, you can use the putText()
function.
cv2.putText(img, "Hi", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
Code language: Python (python)
Output
Resizing the Image
In order to resize the image, we need to import the OpenCV image processing library. You can use the resize() function for this purpose.
img = cv2.resize(img, (500, 500))
Code language: Python (python)
Output
Rotating the Image
Now it’s time to rotate the image. We’ll do this using the getRotationMatrix2D() function and the warpAffine() function. So make sure you import the cv2 module.
Once you have imported OpenCV and its supporting modules, we’re ready to get started with rotating our image.
height, width = img.shape[:2]
matrix = cv2.getRotationMatrix2D((width/2, height/2), 90, 1)
img_rotation = cv2.warpAffine(img, matrix, (width, height))
Code language: Python (python)
Output
Conclusion
In summary, OpenCV is a computer vision library for Python. It’s very easy to use and has some powerful features that make it an excellent choice when you want to get started with image processing and machine learning. However, there are plenty of online tutorials available. But I hope this one will be useful for newbies as well as experienced programmers who want to learn more about these techniques without having to spend hours reading documentation and tutorials themselves.
FAQs
What is OpenCV for Python?
OpenCV for Python is a library that provides a set of tools and functions for image processing and computer vision in Python. It is built on top of the C++ OpenCV library and provides a convenient interface for Python developers.
What is OpenCV used for?
OpenCV is used for a wide range of image processing and computer vision tasks, including image filtering, feature detection, object recognition, and more. Some common applications of OpenCV include image recognition in self-driving cars, object tracking in video surveillance systems, and image processing in medical imaging.
Is Python suitable for OpenCV?
Python is a suitable language for OpenCV as it provides a simple, easy-to-use syntax, and many libraries for scientific computing and data analysis. Python also has a large and active community. This makes it easy to find help and resources when working with OpenCV.
Should I use Python for OpenCV?
Whether you should use Python for OpenCV depends on your specific use case and the tools and libraries that you are already familiar with. However, Python is a popular choice for OpenCV due to its simplicity and ease of use.
Do Python and OpenCV go together?
Python and OpenCV go well together, as they are both open-source and have large and active communities. Python provides a simple, easy-to-use syntax that is well-suited for image processing and computer vision tasks, while OpenCV provides a wide range of tools and functions for these tasks. The combination of Python and OpenCV provides a powerful and flexible platform for image processing and computer vision.
Sharing is caring
Did you like what Srishti Kumari wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses:
303 students learning
Haris
Python Crash Course for Beginners
Surendra varma Pericherla
Learn Data Structures Using Python