Working with images is fun. When you understand the basics of processing images using programming, you come up with creative ideas on image processing and image augmentation. While there are many Python libraries available for such tasks, we will get our hands on the most famous Python library used for such tasks: OpenCV. The implementation of OpenCV in Python makes working with images more fun as the library undertakes those mathematical computations, no one of us likes to do and lets us focus on our main task.
In this blog, we will understand the basics of OpenCV in Python, how to install it, and explore the common functionalities of the library. This is what we will see:
Table of Contents
Let’s get started.
Reading an image using OpenCV
We will use an image of a cat and import it using imread()
function of OpenCV.
import cv2
image = cv2.imread('cat.jpeg')
cv2.imshow(image)
Output:
[Note: If you are using Google Colab as your IDE, cv2.imshow()
will not work. Instead you need to use cv2_imshow()
which can be impored as follows:]
from google.colab.patches import cv2_imshow
Every colored image imported has 3 channels: Red, Green and Blue. One can see this by calling shape()
attribute of the imported image.
print(image.shape)
Output:
(523, 500, 3)
As seen in output, the image has 3 channels. The first number 523 indicates the number of rows in the image or height of the image. The width or number of columns are 500. Lastly, we have the number of channels, that is, 3. Assuming that we keep our image colored, the number of channels remain constant. It is only when you convert the image to grayscale that the number of channels reduces to one. How to convert a colored image to grayscale is shown here. During resizing, we only change the width and height of the image as we will see next.
Resizing the Image
As mentioned above, the image we imported has a shape of (523, 500, 3). [inlinetweet]In many cases, resizing an image helps in optimizing a task. For example, resizing training images during preprocessing helps the model to learn faster and also reduces the required computational power.[/inlinetweet]
We will decreases the size of the imported image from (523, 500, 3) to (100, 200, 3). This number is completely optional and you can vary it the way you require.
resized_image = cv2.resize(image, (100, 200))
Output:
Rotating the Image
OpenCV also allows us the rotate an image. Here, we will go back to the original image that we imported to test this functionality. However, this is not just a one-line code. Let us first rotate the image and then understand the lines.
center = (image.shape[1]/2, image.shape[0]/2)
rotationMatrix = cv2.getRotationMatrix2D(center, 180, 1)
rotated_image = cv2.warpAffine(image, rotationMatrix, (image.shape[1], image.shape[0]))
Output:
Let us take a moment to understand what did we do in those 3 lines of code.
- First, we initialized the center of rotation, which here is the center of the image. We took the midpoint of the height and the same of the width of the image.
- Next, we created a rotation matrix using
getRotationMatrix2D()
of OpenCV. The function takes the center points of rotation as the first argument, the degree of rotation as the second and the scaling factor as third. I want to rotate the image by 180 degrees which I entered as the second argument. No scaling of image is needed as of now, so I left it to 1. Had I entered it as 0.5, the image would have decreased by 50 % of its size. You can go ahead an try it. - Lastly, we enter the source image, rotation matrix and the rotation centers as arguments to a function called
warpAffine()
.
Drawing a Rectangle
In case you want to highlight an area in an image, OpenCV provides a function called rectangle()
for that.
image_copy = image.copy()
#initializing the arguments
top_left_corner = (150, 30)
bottom_right_corner = (390, 300)
color = (0, 0, 255)
border_thickness = 2
rectangle = cv2.rectangle(image_copy, top_left_corner, bottom_right_corner, color, border_thickness)
cv2_imshow(rectangle)
rectangle()
takes in 5 arguments:
- the source image
- starting coordinates of the rectangle, i.e., the coordinates of the top left corner
- end coordinates of the rectangle, i.e., the coordinates of the bottom right corner
- color of the rectangle
- border thickness.
The output of the code written above can be seen in the following picture:
Output:
Writing a text in Image
Let’s say that along with the rectangle, you also want to display the text that says that the picture is of a cat. For this, OpenCV provides a function called putText()
.
text_coordinates = (250, 25)
text = 'Cat'
size_of_text = 1
thickness_of_text = 2
text = cv2.putText(image_copy, text, text_coordinates , cv2.FONT_HERSHEY_SIMPLEX, size_of_text, color, thickness_of_text)
cv2_imshow(image_copy)
putText()
takes in the following arguments:
- the source image
- text to be displayed
- coordinates where text is to be displayed in the image
- type of font
- size of the text
- thickness of the text
Output:
Conclusion
In this article, we learned the basics of OpenCV in Python. We saw how to import and resize an image. Moreover, we also highlighted a region with some text in the image. Apart from these, OpenCV is also used in many other Python tasks like image processing for machine learning. I encourage you to go ahead and try these OpenCV functions yourself. Take any image and highlight and label some objects in it. Upload your results in your Instagram story and tag me @machinelearningsite. Happy coding!
If you enjoyed this tutorial on OpenCV in Python, get in touch with me on my social media where I upload short content and useful tips on programming and machine learning.
You don’t want to miss out on such interesting blogs, do you? Then join the *FREE* newsletter where you’ll stay updated on the monthly posts you might have missed: