OpenCV Geometric Shapes Tutorial – Line, Rectangle, Circle, Ellipse, Polygon and Text

OpenCV Geometric Shapes Tutorial - Line, Rectangle, Circle, Ellipse, Polygon and Text
OpenCV Geometric Shapes Tutorial - Line, Rectangle, Circle, Ellipse, Polygon and Text

Introduction

OpenCV offers several functions to draw different geometric shapes and write text on an image.  In this tutorial, we are going to see opencv functions to draw shapes like Line, Rectangle, Circle, Ellipse, and Polygon. Below is the list of functions that we are going to cover –

  • cv2.line() : This function is used to draw line on an image.
  • cv2.rectangle() : This function is used to draw rectangle on an image.
  • cv2.circle() : This function is used to draw circle on an image.
  • cv2.ellipes() : This function is used to draw ellipse on an image.
  • cv2.polylines() : This function is used to draw polygon on an image.
  • cv2.putText() : This function is used to write text on image.

Importing opencv library

In [1]:
import cv2
import numpy as np

Drawing a line on image with OpenCV: cv2.line()

Opencv provides cv2.line() function for drawing a line on an image.

Syntax

cv2.line (img,pt1,pt2,color,thickness = 1,lineType = LINE_8)

It draws a line segment connecting two points.

The function line draws the line segment between pt1 and pt2 points in the image.

Ad
Deep Learning Specialization on Coursera
  • img: Image where the line will be drawn.
  • pt1: First point or starting coordinate of the line segment.
  • pt2: Second point or ending coordinate of the line segment.
  • color: Line color.Color of the shape. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.
  • thickness: Thickness of the lines.thickness=10 means thickness of 10 pixels
  • lineType: Type of the line. Different type of line is 4 connected, 8-connected, anti-aliased line, etc. By default, it is 8-connected. cv.LINE_AA gives anti-aliased line which looks great for curves.

Example1: Drawing line using cv2.line()

In [2]:
#Load a sample image
img_line=cv2.imread("image.jpg")
In [3]:
#Display image
window_name='imagefirst'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_line)
cv2.waitKey(0)
cv2.destroyAllWindows()
Opencv Geometric Shapes - Sample Image
Opencv Geometric Shapes – Sample Image
In [4]:
#Draw line
#line is red with thickness of 10 pixels
cv2.line(img_line,(0,0),(500,500),(0,255,0),10)
Out[4]:
array([[[  0, 255,   0],
        [  0, 255,   0],
        [  0, 255,   0],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[  0, 255,   0],
        [  0, 255,   0],
        [  0, 255,   0],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[  0, 255,   0],
        [  0, 255,   0],
        [  0, 255,   0],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [5]:
#Display image
window_name='imagefirst_line'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_line)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-1 of cv2.line()
Example-1 of cv2.line()

Example 2: Drawing a red line with more thickness using cv2.line()

In [6]:
#Load image
img_line_second=cv2.imread("image.jpg")
In [7]:
#Draw line
#Red Lines with thickness of 20 pixels
cv2.line(img_line_second,(0,0),(1000,1000),(0,0,255),20)
Out[7]:
array([[[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [8]:
#Display image
window_name='imagesecond_line'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_line_second)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-2 of cv2.line()
Example-2 of cv2.line()

Drawing a rectangle on image with OpenCV: cv2.rectangle()

Opencv provides cv2.rectangle() function for drawing a rectangle on an image. The function cv2.rectangle draws a rectangle outline or a filled rectangle whose two opposite corners are pt1 and pt2.

Syntax

cv2.rectangle(img, pt1, pt2, color,thickness,lineType)

  • img: Image on which rectangle to be drawn.
  • pt1: Vertex of the top-left corner rectangle.
  • pt2 Vertex of the Bottom-right corner of rectangle i.e opposite to pt1.
  • color: Rectangle color (BGR) or brightness (grayscale image).
  • thickness: Thickness of lines that make up the rectangle. Negative values, like FILLED, mean that the function has to draw a filled rectangle.
  • lineType: Type of the line. Same as above

Example 1: Drawing rectangle using cv2.rectangle()

In [9]:
#Load image
img_rectangle=cv2.imread("image.jpg")
In [10]:
cv2.rectangle(img_rectangle,(384,0),(1024,1024),(255,0,0),3)
Out[10]:
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [11]:
#Display image
window_name='imagewithRectangle'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_rectangle)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-1 of cv2.rectangle()
Example-1 of cv2.rectangle()

Example 2: Drawing a yellow rectangle with more thickness using cv2.rectangle()

In [12]:
#Load image
img_rectangle_second=cv2.imread("image.jpg")
In [13]:
#Draw rectangle
cv2.rectangle(img_rectangle_second,(500,0),(1024,1024),(0,255,255),20)
Out[13]:
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [14]:
#Display image
window_name='imageSecond_WithRectangle'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_rectangle_second)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-2 of cv2.rectangle()
Example-2 of cv2.rectangle()

Example 3: Drawing a filled yellow rectangle using cv2.rectangle()

In [41]:
#Load image
img_rectangle_third=cv2.imread("image.jpg")

To draw filled rectangle you have to pass thickness=-1

In [15]:
cv2.rectangle(img_rectangle_third,(500,0),(1024,1024),(0,255,255),-1)
Out[15]:
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [16]:
#Display image
window_name='imageThirdWithRectangle'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_rectangle_third)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-3 of cv2.rectangle()
Example-3 of cv2.rectangle()

Drawing a circle on image with OpenCV: cv2.circle()

Opencv provides cv2.circle() function for drawing a circle on an image.

We know in general, that we need to provide center coordinates and radius to draw a circle.

Syntax

cv2.circle( img, center, radius, colorthickness,lineType)

  • img: Image where the circle is drawn.
  • center: Center of the circle.
  • radius: Radius of the circle.
  • color: Colour of cirlce.
  • thickness: Thickness of the circle outline.
  • lineType: Type of the circle boundary.

Example 1: Drawing circle using cv2.circle()

In [17]:
#Load image
img_circle=cv2.imread("image.jpg")
In [18]:
#Draw circle
cv2.circle(img_circle,(1024,1024), 150, (0,0,255), 20)
Out[18]:
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [19]:
#Display image
window_name='ImageFirstWithCircle'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_circle)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-1 of cv2.circle()
Example-1 of cv2.circle()

Example 2: Drawing a filled yellow circle using cv2.circle()

In [20]:
#Load image
img_circle_second=cv2.imread("image.jpg")
In [21]:
#Draw filled circles
cv2.circle(img_circle_second,(1024,1024), 150, (0,0,255), -1)
Out[21]:
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [22]:
#Display image
window_name='imageSecondWithCircles'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_circle_second)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-2 of cv2.circle()
Example-2 of cv2.circle()

Drawing an ellipse on image with OpenCV: cv2.ellipse()

Opencv provides cv2.ellipse() function for drawing a ellipse on an image.

Syntax

cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness, lineType)

  • img: Image where the ellipse is drawn.
  • center: center location (x,y).
  • axes: argument is axes lengths (major axis length, minor axis length)
  • angle : angle is the angle of rotation of ellipse in anti-clockwise direction.
  • startAngle and endAngle : startAngle and endAngle denote the starting and ending of ellipse arc measured in a clockwise direction from major axis. i.e. giving values 0 and 360 gives the full ellipse.
  • color: colour of the ellipse .
  • thickness: Thickness of the ellipse arc outline
  • lineType: Type of the ellipse boundary.This argument is similar to the previous one

To draw the ellipse, we need to pass several arguments in comparison of other shapes.

Example 1: Drawing ellipse using cv2.ellipse()

In [23]:
#Load image
img_ellipse=cv2.imread("image.jpg")
In [24]:
#Draw ellipse
cv2.ellipse(img_ellipse,(256,256),(100,50),0,0,180,255,20)
Out[24]:
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [25]:
#Display image
window_name='imageFirstWithEllipse'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_ellipse)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-1 of cv2.ellipse()
Example-1 of cv2.ellipse()

Example 2: Drawing a filled yellow ellipse using cv2.ellipse()

In [26]:
#Load image
img_ellipse_second=cv2.imread("image.jpg")
In [27]:
#Draw filled ellipse
cv2.ellipse(img_ellipse_second,(256,256),(100,50),0,0,180,(0,255,255),-1)
Out[27]:
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [28]:
#Display image
window_name='ImageSecondWithEllipse'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_ellipse_second)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-2 of cv2.ellipse()
Example-2 of cv2.ellipse()

Drawing Polygon on image with OpenCV: cv2.polylines()

Opencv provides cv2.polylines() function for drawing a polygon on an image.

Syntax

cv2.polylines(img, pts, isClosed, color, thickness,lineType)

  • img: Image on which polygon needs to be drawn
  • pts: Array of polygonal curves. To draw a polygon, first you need coordinates of vertices. Make those points into an array of shape ROWSx1x2 where ROWS are the number of vertices and it should be of type int32.
  • isClosed :Flag indicating whether the drawn polylines are closed or not. If they are closed, the function draws a line from the last vertex of each curve to its first vertex.
  • color: Polyline color.
  • thickness: Thickness of the polyline edges.
  • lineType :Type of the line segments

Example: Drawing polygons using cv2.polylines()

In [29]:
#Load image
img_polygon=cv2.imread("image.jpg")
In [30]:
#Draw polygon on the coordinates pts
pts = np.array([[100,50],[200,300],[700,200],[500,100]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img_polygon,[pts],True,(0,0,255),20)
Out[30]:
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [31]:
#Display image
window_name='ImageFirstWithPolygon'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_polygon)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-1 of cv2.polylines()
Example-1 of cv2.polylines()

Example2: Drawing polygons using cv2.polylines()

If the third argument(isClosed) is False, you will get a polylines joining all the points, not a closed shape.

In [32]:
#Load image
img_polygon_second=cv2.imread("image.jpg")
In [33]:
#if isclosed=False
pts = np.array([[100,50],[200,300],[700,200],[500,100]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img_polygon_second,[pts],False,(0,0,255),20)
Out[33]:
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [34]:
#Display image
window_name='imagefirst'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_polygon_second)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-2 of cv2.polylines()
Example-2 of cv2.polylines()

Putting Text on image with OpenCV: cv2.putText()

Opencv provides cv2.putText() function to write a text on an image.

Syntax

cv2.putText(img, text, org, fontFace, fontScale, color, thickness,  lineType, bottomLeftOrigin)

  • img: The image on which to put text
  • text: Text string to be written.
  • org: Bottom-left corner of the text string in the image i.e Position coordinates of where you want to put the string
  • fontFace: Font type.
  • fontScale: Font scale factor that is multiplied by the font-specific base size.
  • color: Text color.
  • thickness: Thickness of the lines used to draw a text.
  • lineType: Line type.
  • bottomLeftOrigin: When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.

Example 1: Putting Text using cv2.putText

In [35]:
#Load image
img_first=cv2.imread("image.jpg")
In [36]:
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img_first,'PUT TEXT',(10,500), font, 4,(255,0,0),10,cv2.LINE_AA)
Out[36]:
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
In [37]:
#Display image
window_name='imagefirstWithText'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_first)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example-1 of cv2.putText()
Example-1 of cv2.putText()

 

Conclusion

So in this tutorial, we took a detailed look into various Opencv functions for producing geometric shapes of Line Rectangle, Circle, Ellipse, Polygon along with putting Text. We saw their syntaxes and examples for a better understanding.

Click here to download the code

Reference – https://docs.opencv.org/master/d6/d00/tutorial_py_root.html

Like and Comment section (Community Members)

Create Your ML Profile!

Don't miss out to join exclusive Machine Learning community

Comments

No comments yet