Table of Contents
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
1 2 3 |
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:
image:
Input image of n-dimensional array.starting_coordinate:
Starting top most Left coordinate of the rectangle which is given in the form of tuple.ending_coordinate:
Ending bottom most right coordinate of the rectangle which is given in the form of tuple.color:
Boundary color of rectangular shape which is given in the form of tuple with respective BGR values.thickness:
Thickness of rectangle’s boundary (given in pixels). Its default value is 1.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.shift:
Represents the number of fractional bits in the coordinates.
Optional parameters are :
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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 :
Example 2: Draw a square shape on the image with bold border.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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:
You may similarly change the values of other properties and observe the outputs.