The OpenCV library provides several methods for drawing geometric shapes on images, including rectangles, lines, and circles. In this blog post, we will explore how to use the cv2.rectangle()
, cv2.line()
, cv2.arrowedLine(),
and cv2.circle()
methods to draw these shapes on images.
First, let’s talk about rectangles. A rectangle is a four-sided shape with four right angles. In OpenCV, we can draw rectangles on an image using the cv2.rectangle
function. This function takes the following parameters:
- the image on which to draw the rectangle,
- the top-left corner coordinates of the rectangle are represented as a tuple of (x, y) values,
- the bottom-right corner coordinates of the rectangle are also represented as a tuple of (x, y) values,
- the color of the rectangle is represented as a tuple of (B, G, R) values,
- the thickness of the rectangle’s border.
# draw a red rectangle with a thickness of 2
image = cv2.imread('./Hrithik-Roshan-1-1900x.jpg')
image = cv2.resize(image,(512,512))
cv2.rectangle(image , (50 , 50) , (200,200) , (0,255,255) , 2)
plt.imshow(image[:,:,::-1])
Next, let’s talk about lines. A line is a straight path that extends infinitely in both directions. In OpenCV, we can draw lines on an image using the cv2.line()
function. This function takes the following parameters:
- the image on which to draw the line,
- the coordinates of the start point of the line are represented as a tuple of (x, y) values,
- the coordinates of the endpoint of the line are also represented as a tuple of (x, y) values,
- the color of the line represented as a tuple of (B, G, R) values,
- the thickness of the line.
# draw a blue line with a thickness of 1
image = cv2.imread('./Hrithik-Roshan-1-1900x.jpg')
image = cv2.resize(image,(512,512))
cv2.line(image , (30,100) , (300,300) , (255,255,255) , 2)
plt.imshow(image[:,:,::-1])
In some cases, we may want to draw an arrowed line in the image instead of a simple line, OpenCV provides cv2.arrowedLine()
for this purpose. this function takes the same parameters as cv2.line()
but with one additional parameter, tip length
: the length of the arrow tip.
# draw a green arrowed line with a thickness of 2 and tip length = 0.2
image = cv2.imread('./Hrithik-Roshan-1-1900x.jpg')
image = cv2.resize(image,(512,512))
cv2.arrowedLine(image , (60,60) , (180,180) , (255,255,0) , 2) # last 2 refers for thickness
plt.imshow(image[:,:,::-1])
with parameter tip length
# draw a green arrowed line with a thickness of 2 and tip length = 0.2
image = cv2.imread('./Hrithik-Roshan-1-1900x.jpg')
image = cv2.resize(image,(512,512))
cv2.arrowedLine(image , (60,60) , (180,180) , (255,255,0) , 2 , 2) # last 2 refers for tiplength
plt.imshow(image[:,:,::-1])
Finally, let’s talk about circles. A circle is a closed shape with all points at an equal distance from the center. In OpenCV, we can draw circles on an image using the cv2.circle()
function. This function takes the following parameters:
- the image on which to draw the circle,
- the center coordinates of the circle are represented as a tuple of (x, y) values,
- the radius of the circle,
- the color of the circle represented as a tuple of (B, G, R) values,
- the thickness of the circle’s border.
image = cv2.imread('./Hrithik-Roshan-1-1900x.jpg')
image = cv2.resize(image,(512,512))
cv2.circle(image,(250,250) , 55 , (255,255,0) , 2)
plt.imshow(image[:,:,::-1])
All the coordinates in one image
image = cv2.imread('./Hrithik-Roshan-1-1900x.jpg')
image = cv2.resize(image,(512,512))
cv2.rectangle(image , (50 , 50) , (200,200) , (0,255,255) , 2)
cv2.line(image , (30,100) , (300,300) , (255,255,255) , 2)
cv2.arrowedLine(image , (60,60) , (180,180) , (255,255,0) , 2,tipLength = 2)
cv2.circle(image,(250,250) , 55 , (255,255,0) , 2)
plt.imshow(image[:,:,::-1])
In conclusion, the OpenCV library provides several methods for drawing geometric shapes on images, including rectangles, lines, and circles. Using the cv2.rectangle()
, cv2.line()
, cv2.arrowedLine()
and cv2.circle()
methods, we can draw these shapes on gray scale images with ease. These functions allow us to specify the shape's position, color, and thickness, giving us complete control over how the shape is drawn on the image. By understanding how to use these methods and their parameters, we can quickly and easily add geometric shapes to our images to create interesting and visually compelling results.