OpenCV Python Tutorial for Beginners Part 3
Himanshu K.
Frontend React JS Developer @ Dynamics Monk | React JS | Next JS | Typescript | JavaScript | React Testing Library | Jest | Webpack | Parcel | Low-Level & High Level System Design
Welcome to OpenCV Python Beginners Tutorial Part 3.
In case if you missed reading the Second tutorial of OpenCV python, then it is recommended to read the second part at OpenCV Python Tutorial Part 2.
In our last tutorial, we covered some basics tasks like:
- Get Image / Video Frame Size & Shape
- Resize the Image
- Create Blank Black/White Image using numpy
- How to Add Two Images together using the Add function
- How to add two together using the cv2.addWeighted method
Now, In this tutorial, we will begin the article from where we left off.
Note: In this tutorial, We will use the matplotlib library and some of their image display function to see results. I intend to show you the image along with the x and y-axis scale, and only with the plt.imshow(), we can do the same to show the image along with the x and y scale.
To use the matplotlib library, you need to install matplotlib library by giving this command in cmd:
pip install matplotlib
Once you install the matplotlib library, don't forget to import it along with the OpenCV and Numpy libraries.
Example:
import cv2 import numpy as np import matplotlib.pyplot as plt
This part of the OpenCV Python tutorial will be a bit lengthy and in-depth but, don't worry. I will explain every small detail in simple & plain English.
Without more interruption, Let's get started:
TASK 10: How to find X & Y coordinates to draw a shape or to write on an image:
Before drawing the shape or write the text on an image, we need to find the value of x and y coordinates first so that our operation can perform at the correct places of an image.
To find the value of X and Y coordinates, we can take the help of some matplotlib methods.
img = np.ones((512,512,3), dtype=np.uint8) * 255 plt.imshow(img) plt.show()
Let's understand the above code:
img = np.ones((512,512,3), dtype=np.uint8) * 255
In the first line, we have used the numpy one's method to create a white blank image so that we can draw the shape on the blank white image. To know more about this method, visit the second tutorial of the OpenCV python, and read TASK 7.
plt.imshow(img) plt.show()
Now, In the second and third lines, we have used the plt.imshow and plt.show function to display an image along with the x and y-axis scale.
If you are using it on a Jupyter Notebook, then the image array will appear as embedded in the notebook or part of an image.
Image:
TASK 11: How to Draw a Line on Image
Code:
img = np.ones((512,512,3), dtype=np.uint8) * 255 #creating white image cv2.line(img, (100,400),(400,100), (255,0,0), 2, cv2.LINE_4) plt.imshow(img) plt.show()
Let's understand the code:
img = np.ones((512,512,3), dtype=np.uint8) * 255 #creating white image
In the First Line, we have used the np.ones method to create a white image so that we can draw the shape on an image.
In case you don't know what does it do? or to understand this function more in-depth. Read Task 7 in the Second tutorial of our OpenCV Python.
cv2.line(img, (100,400),(400,100), (255,0,0), 2, cv2.LINE_4)
In the Second Line, we have used the cv2.line method to draw a line on the white image array. In the cv2.line, we passed the image array as the first argument which we want to use for drawing a line.
In the second argument of the cv2.line method, we passed a tuple that contains the starting value of x and the starting value of y. The value should be in the integer format. If we look at the syntax of this tuple format, then it is (starting_value_of_x,starting_value_of_y).
In the third argument of the cv2.line method, we passed another tuple that contains the ending value of x and the ending value of y. The value should be the integer format.
An important key to remember: the y-axis is always starting from the top, which means the top y value is 0.
In the fourth argument of the cv2.line method, we passed a tuple that contains the (B, G, R) color channel values. As we discussed in our first tutorial of OpenCV, we know that OpenCV reads RGB as BGR, and these color channel values range from 0 to 255. To make the line blue, we passed the B as 255, and the rest of the channel values stay as 0.
Example:
Blue: (255,0,0), Red: (0,0,255)
In the fifth argument of the cv2.line method, we passed the thickness of the line, which we want to add in the line, and the value should be in an integer format.
An important key to remember: Suppose, if you need to fill the shape of a rectangle, circle, or polygon, then you need to pass the -1 value. I will also explain these figures in the next tasks. -1 only works with closed shape figures. If you give it in the cv2.line, it will throw an exception.
In the sixth argument of the cv2.line method, we passed the line type. It is an optional argument which you can give. In case if you want to specify the line type, then you will have several type's available:
- cv2.LINE_AA
- cv2.LINE_4
- cv2.LINE_8
As I am practicing OpenCV, I noted that most types, instances are in the capital format. If you have read my previous tutorial on OpenCV, you also saw that most of these instances like cv2.LINE_AA, cv2.IMREAD_COLOR, or cv2.COLOR_BGR2GRAY are all in the capital format. It will also help you to determine what is the function/module/method or instance of the OpenCV.
plt.imshow(img) plt.show()
In the third & fourth lines, we have used two functions of matplotlib to display an image on the screen. If you are using it on a Jupyter Notebook, then the image array will appear as embedded in the notebook or part of an image.
plt.imshow function and plt.show() functions are both used together, and we passed the image array as the first argument of the plt.imshow to display an image.
An important key to remember: As we aware that OpenCV reads an image in BGR format, and on another side, the maltplotlib library reads images in the RGB format. To convert color spaces to different colorspace, we need to use the cv2.cvtColor.
In case, if you don't know about the cv2.cvtColor method, read TASK 4 in the First tutorial of OpenCV to learn how to use it.
TASK 12: How to draw a rectangle shape on an image
Code:
img = np.ones((512,512,3), dtype=np.uint8) * 255 #creating white image cv2.rectangle(img, (100,100),(300, 200), (0,255,0), 2) plt.imshow(img) plt.show()
Let's understand the code:
img = np.ones((512,512,3), dtype=np.uint8) * 255 #creating white image
In the first line, we have used the np.ones method to create a white image so that we can draw the rectangle shape on the blank white image.
In case if you don't know what does it do? or to understand this function more in-depth. Read Task 7 in the Second tutorial of our OpenCV Python.
cv2.rectangle(img, (100,100),(300, 200), (0,255,0), 2)
In the Second Line, we have used the cv2.rectangle method to create a rectangle shape on the white image. In the first argument of cv2.rectangle, we have passed the image array, which we want to use for drawing the rectangle shape.
In the second argument of the cv2.rectangle method, we have passed the tuple that contains two integer values, one is for the starting value of X, and the second integer value is the starting value of Y. Both values represent the top left corner of the rectangle.
In the third argument of the cv2.rectangle method, we have given another tuple that contains two integer values, one is for the ending value of X, and the second integer value is the ending value of Y. Both values represents the bottom right corner of the rectangle.
In the fourth argument of the cv2.rectangle method, we have given three color channel values in the form of a tuple. In the tuple, the first value represents the Blue, the second represents the Green, and the third represents the Blue.
Note: In case you are not sure about color channels or don't know about it, Visit the first tutorial of OpenCV python.
In the fifth argument of the cv2.rectangle method, we have passed the thickness value of the rectangle borders. A more positive value = more thickness of the rectangle will be.
If you want to fill the shape of the rectangle, then you need to pass the -1 value as the thickness.
plt.imshow(img) plt.show()
In the third and fourth lines, we have used plt.imshow and plt.show methods of the matplotlib, and these methods are for showing an image, and these methods are the same as the cv2.imshow method.
In the first argument of plt.imshow, we need to pass only image array. We can also give more Arguments like the cmap = "gray" to show the image in grayscale format.
TASK 13: How to draw a circle on an Image:
Code:
img = np.ones((512,512,3), dtype=np.uint8) * 255 #creating white image cv2.circle(img, (200,200), 100, (0,255,0), 2) plt.imshow(img) plt.show()
Let's understand the code:
img = np.ones((512,512,3), dtype=np.uint8) * 255 #creating white image
In the first line, we have used the numpy one's methods to create a white image so that we can use it to draw a circle.
cv2.circle(img, (200,200), 100, (0,255,0), 2)
In the second line, we have used the cv2.circle method to create a circle on the white image. To draw a circle on the image, we passed the image array as the first argument of the cv.circle method.
In the second argument, we have passed the tuple that contains the center position of the circle. In the tuple, first value represents the X, and the second value represents the Y.
In the third argument, we have passed the integer value that represents the radius of the given circle, and the integer value should be positive.
In the fourth argument, we have passed the tuple that contains three color channel values, the first value is for Blue color, the second value is for Green, and the third value is for Red.
Note: In case you are not sure about color channels or don't know about it, Visit the First tutorial of OpenCV python.
In the fifth argument, we have passed the thickness value of the rectangle borders. A more positive value = more thickness of the rectangle will be.
plt.imshow(img) plt.show()
In the third and fourth lines, we have used plt.imshow and plt.show methods of the matplotlib, and these methods are for showing an image, and these methods are the same as the cv2.imshow method.
In the first argument of plt.imshow, we need to pass only image array. We can also give more Arguments like the cmap = "gray" to show the image in grayscale format.
TASK 14: How to write text on an image
Code:
img = np.ones((512,512,3), dtype=np.uint8) * 255 #creating white image cv2.putText(img, "HIMANSHU", (50, 410), cv2.FONT_HERSHEY_COMPLEX, 0.9, (255,0,0), 2) plt.imshow(img) plt.show()
Let's understand the code:
img = np.ones((512,512,3), dtype=np.uint8) * 255 #creating white image
In the first line, we have numpy one's methods to create a white image so that we can write text on the image.
cv2.putText(img, "HIMANSHU", (50, 410), cv2.FONT_HERSHEY_COMPLEX, 0.9, (255,0,0), 2)
In the second line, we have used the cv2.put method to write the text on the image. In the cv2.putText, we have passed the white image array as the first argument.
In the second argument of the cv2.putText method, we have passed the string which we want to write on the image, and text should come between the double quotation mark.
In the third argument of the cv2.putText method, we have passed a tuple that contains two values, the first value is for X, and the second value is for Y. Value should be in the integer type.
In the fourth argument of the cv2.putText method, we have passed the font face, which we want to use to write the text.
In simple language, if you are aware of the FONT function in the Microsoft Word or PowerPoint Presentation, then the font face is the same as the Microsoft font function. But, there is only one difference is that we need to specify the font in the cv2.putText method.
There are many font faces available in OpenCV, and some of them are:
- cv2.FONT_HERSHEY_COMPLEX
- cv2.FONT_HERSHEY_COMPLEX_SMALL
- cv2.FONT_HERSHEY_DUPLEX
To find all available font faces, you can take the help of IDE IntelliSense or write the below code to get all available font faces:
fonts = [i for i in vars(cv2) if "FONT_" in i] print(fonts)
Output:
In the fifth argument of the cv2.putText method, we have passed the 0.9 font scale value, and it should be float data type. The font scale is for setting the font size of your given text. In the font scale, generally, we start the value as low as 0.4 because 2.1 or 3.1 can make the text font size much larger than the image.
Note: To get the desired size of your font, try to start debugging from the lowest float value if possible, and slowly increase your font scale value by adding 0.1 to the existing value of font scale until you get the expected font size.
In the sixth argument, we have passed the tuple that contains three color channel values, the first value is for Blue color, the second value is for Green, and the third value is for Red.
Note: In case you are not sure about color channels or don't know about it, Visit the First tutorial of OpenCV python.
In the seventh argument, we have passed the thickness value of the given text. A more positive value = more thickness of the font will be.
plt.imshow(img) plt.show()
In the eighth and ninth lines, we have used plt.imshow and plt.show methods of the matplotlib, and these methods are for showing an image, and these methods are the same as the cv2.imshow method.
In the first argument of plt.imshow, we need to pass only image array. We can also give more Arguments like the cmap = "gray" to show the image in grayscale format.
Note: You can also use cv2.imshow method to display your image, but before using that please make sure that you have found the correct X, and Y values for your shapes.
To read the fourth Part of OpenCV Python Tutorial Series, Click Here: OpenCV Python Tutorial Part 4
Note: I hope you like the third part of my OpenCV tutorial Series. In case of any mistakes in this tutorial. Please feel free to comment so that I can improve the quality of my articles.