OpenCV rectangle | Examples of OpenCV rectangle

0
2
OpenCV rectangle | Examples of OpenCV rectangle

OpenCV rectangle | Examples of OpenCV rectangle

Introduction to OpenCV rectangle

The following article provides an outline for OpenCV rectangle. OpenCV rectangle() is a function which is focused on designing algorithm capable of solving problems related to computer vision. The OpenCV rectangle function is utilized in order to draw a rectangle a rectangular shaped hollow box on any image which is provided by the user. The function has the capability of defining the thickness of the line being drawn for the pixel ize being defined by the user. Also, the color of the rectangular box can also be defined which are represented by numeral representations.

Syntax of OpenCV rectangle

The following is the syntax used for application of the rectangle function in python coding language:

cv2 .  rectangle  (image,   start  _  point,   end  _  point,   color,   thickness )

Parameters:

The following are the parameters which are present in the OpenCV rectangle function that have specific usage to enable the function to create a rectangular outline or include a rectangle within the image that has been provided:

  • image: The parameter represents the original image that has to be processed and a rectangular shape has to be included.
  • start_point: The parameter start_point represents the starting coordinates that are present in the rectangle. Tuples of values are presented with respect to the rectangular coordinates (i.e., the X and the Y coordinate values).
  • end_point : The parameter end_point represents the ending coordinates that are present in the rectangle. Tuples of values are presented with respect to the rectangular coordinates (i.e., the X and the Y coordinate values).
  • color : The parameter represents the colour that would be on the border line of the rectangle that is being drawn. For examples, if the user is trying to making a blue square the following tuple has to be passed: 255, 0, 0 which makes the colour – BLUE.
  • thickness : The parameter represents the thickness of the border which is being drawn by the user (the rectangular shape). The thickness of the line is defined by the pixel size or px. The thickness of the line to be made is -1 px which will fill the border or the rectangle shape by the colour which has been specified by the user.

Return Value:

Output image which has been given an outline or rectangular shape included after the function is executed upon the original image.

Examples of OpenCV rectangle

Given below examples demonstrates the utilization of the OpenCV rectangle function:

Example #1

A program written in python coding language aimed at explaining the cv2.flip() in built method.

Code:

# importing the class library cv2 in order perform the usage of flip ()
import cv2
# defining the variable which read the image path for the image to be processed
path_1 = r'C:UsersdataDesktopedu cba logo2.png'
# Reading the provided path defined image file in the default mode
image_1 = cv2.imread(path_1)
# the name of the window in which image is to be displayed
window_name1 = 'Output Image'
# starting coordinates, here the given coordinates are (50, 50)
# the coordinates are representing the top left corner of the given rectangle
start_point1 = (50, 50)
# Ending coordinates, here the given coordinates are (2200, 2200)
#The coordinates are representing the top right corner of the given rectangle
end_point1 = (2200, 2200)
# The rectangular box that is being made on the input image – being defined in Blue color
color1 = (2550, 0, 0)
# The rectangular box that is being made on the input image – being defined for line thickness of 2 px
thickness1 = 2
# Using the Open CV rectangle() method in order to draw a rectangle on the image file
# Drawing a rectangle which has blue border and a has thickness of approximately 2 px
image_1 = cv2.rectangle(image_1, start_point1, end_point1, color1, thickness1)
# Displaying the output image which has been outlined with a rectangle
cv2.imshow(window_name1, image_1)
cv2.waitKey(0)

Output:

The final output of the above image where the image has been outlined using the rectangle function is:

OpenCV rectangle 1

Example #2

Code:

# importing the class library cv2 in order perform the usage of flip ()
import cv2
# defining the variable which read the image path for the image to be processed
path_1 = r'C:UsersdataDesktopedu cba logo2.png'
# Reading the provided image in the grayscale mode
image_1 = cv2.imread(path_1, 0)
# the name of the window in which image is to be displayed
window_name1 = 'Output Image'
# Starting coordinate : (100, 50)
# The coordinates are representing the top right corner of the given rectangle
start_point1 = (100, 50)
# End coordinate : (125, 80)
# the coordinates are representing the top left corner of the given rectangle
end_point1 = (125, 80)
# The rectangular box that is being made on the input image – being defined in Black color
color1 = (0, 0, 0)
thickness1 = -1
# Making use of the Open cv2.rectangle() function
# Drawing a rectangle which has blue border and a has thickness of approximately -1 px
image_1 = cv2.rectangle(image_1, start_point1, end_point1, color1, thickness1)
# Displaying the output image which has been outlined with a rectangle
cv2.imshow(window_name1, image_1)
cv2.waitKey(0)

Output:

OpenCV rectangle 2

Conclusion

OpenCV rectangle() function is an important inbuilt function that enables to instantaneously drawing a rectangle or box around the images that are being processed by the system. It is typically useful in software for image detection, filtering and beautification such as border and frame maker and editor software.

Recommended Articles

This is a guide to OpenCV rectangle. Here we discuss the introduction and examples of OpenCV rectangle for better understanding. You may also have a look at the following articles to learn more –

  1. OpenCV kmeans
  2. Python deepcopy
  3. Python AssertionError
  4. Python Command-line Arguments

The post OpenCV rectangle appeared first on EDUCBA.

Source link