Python | cv2 rectangle() Method

In this tutorial, we will see how to draw a Rectangle shape, Square shapes on the given input image in python by the use of open-cv which exists as cv2 (computer vision) library.

You can use rectangle() method of cv2 library to draw rectange, squares on the image. To use cv2 library, you need to import cv2 library using import statement.

Now let’s see the syntax and return value of rectangle() method, then we will move on the examples.

Syntax

cv2.rectangle(image, starting_coordinate , ending_coordinate, color, thickness, lineType, shift)

Parameters

You can pass 7 parameters to resize() method. Among the 7 parameters, the first four(image,starting_coordinate,ending_coordinate and color) are mandatory; rest(thickness, lineType and shift) are optional.

Necessary parameters are:

  1. image: Input image of n-dimensional array.
  2. starting_coordinate: Starting top most Left coordinate of the rectangle which is given in the form of tuple.
  3. ending_coordinate: Ending bottom most right coordinate of the rectangle which is given in the form of tuple.
  4. color: Boundary color of rectangular shape which is given in the form of tuple with respective BGR values.
  5. Optional parameters are :

  6. thickness: Thickness of rectangle’s boundary (given in pixels). Its default value is 1.
  7. lineType: Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv.LINE_AA gives anti-aliased line which looks great for curves.
  8. shift: Represents the number of fractional bits in the coordinates.

Return Value

It returns Output image of n-dimensional array with rectangular shapes drawn on it .

cv2.rectangle() Method example

Now Let’s see the Python code :

Example 1: Draw a rectangular shape on the image with a thin border.

# import computer vision library(cv2) in this code
import cv2

# main code
if __name__ == "__main__" :

    # mentioning absolute path of the image
    img_path = "C:\\Users\\user\\Desktop\\flower.jpg"

    # reading an image in default mode
    img = cv2.imread(img_path)

    # show the input image on the newly created image window
    cv2.imshow('image1 window',img)

    # top left corner coordinate
    starting_coordinate = (10,10)

    # bottom right corner coordinate
    ending_coordinate = (100,150)

    # BGR value of black color
    color = (0,0,0)

    # drawing a rectangle with black colour on given image 
    new_image = cv2.rectangle(img, starting_coordinate , ending_coordinate, color)

    # show the output image on the newly created image window
    cv2.imshow('image2 window',new_image)

Output :

output1

Example 2: Draw a square shape on the image with bold border.

# import computer vision library(cv2) in this code
import cv2

# main code
if __name__ == "__main__" :

    # mentioning absolute path of the image
    img_path = "C:\\Users\\user\\Desktop\\flower.jpg"

    # reading an image in default mode
    img = cv2.imread(img_path)

    # show the input image on the newly created image window
    cv2.imshow('image1 window',img)

    # top left corner coordinate
    starting_coordinate = (10,10)

    # bottom right corner coordinate
    ending_coordinate = (100,100)

    # BGR value of black color
    color = (0,0,0)

    # drawing a rectangle with black colour on given image 
    new_image = cv2.rectangle(img, starting_coordinate , ending_coordinate, color, 5)

    # show the output image on the newly created image window
    cv2.imshow('image2 window',new_image)

Output:

output2

You may similarly change the values of other properties and observe the outputs.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.