Updated April 10, 2023
Introduction to Open CV rect ()
Open CV rect () Is an inbuilt function which is present in the open CV Public Library. this function is designed in order to provide for drawing of a rectangular shape given in an image to project that rectangle on the screen. this rectangular shape should essentially contain for sides, two sides parallel to each other and should be at an angle of 90 degrees with each of the edges. direct angular killer should be equal in length for the parallel sites that are represented by opposite ends of the rectangle. If the structure projected by the system on the screen is similar to a parallelogram (structure made which does not contain the angle between the sides of 90 degrees) it is not a correct representation of the Open CV rect () function.
Syntax and functions used for operation of Open CV rect()
Following is the syntax which is used for implementation the Open CV rect () function:
rect * (* x *, * y *, * w *, * h *, * tl *, * tr *, * br *, * bl *) *
or
rect * (* x *, * y *, * w *, * h *, * detail * X *, * detail * Y *) *
Parameters:
The following parameters are accepted by the Open CV rect function:
Parameter Description of the Parameter
x *: This parameter represents the x coordinates for the rectangle that has to be drawn on the screen canvas.
y *: This parameter represents the y coordinates for the rectangle that has to be drawn on the screen canvas.
w *: This parameter represents the width that has to be set for the rectangle that has to be drawn on the screen canvas based on the input or specification of the user.
h *: This parameter represents the height that has to be set for the rectangle that has to be drawn on the screen canvas based on the input or specification of the user.
tl *: This parameter represents the radius for the top left corner of the rectangle that has to be set for the rectangle that has to be drawn on the screen canvas based on the input or specification of the user. This parameter is an optional parameter.
tr *: This parameter represents the radius for the top right corner of the rectangle that has to be set for the rectangle that has to be drawn on the screen canvas based on the input or specification of the user. This parameter is an optional parameter.
br *: This parameter represents the radius for the bottom right corner of the rectangle that has to be set for the rectangle that has to be drawn on the screen canvas based on the input or specification of the user. This parameter is an optional parameter.
bl *: This parameter represents the radius for the bottom left corner of the rectangle that has to be set for the rectangle that has to be drawn on the screen canvas based on the input or specification of the user. This parameter is an optional parameter.
detailX *: This parameter represents the number of the segments that are present in the x-direction that has to be set for the that has to be drawn on the screen canvas based on the input or specification of the user.
detailY *: This parameter represents the number of the segments that are present in the y-direction that has to be set for the that has to be drawn on the screen canvas based on the input or specification of the user.
Example of Open CV rect Function
# command used to import the Open CV library to utilize the the histogram equalizer function
import cv2
# command used to import the Numpy library for utilization of the function in the below code
import numpy as np1
// function setup called to initiate the drawing of a rectangle
function setup() {
// Creating the canvas of given size
create *Canvas *( *4000 *, *3000 *) *; *
} * * * * * * * *
function draw() {
background(2200);
// Using the function color() to choose a specific colour
let c1 = color('gray');
// Using the function fill() in order to fill color in the rectangle drawn
fill(c1);
// Drawing the rectangle on the canvas
rect * (*50 *, * * *50 *, * * *300 * *, *200 *) *; *
}
# the resultant image are displayed which are showed together show the difference in the images
cv2.imshow(\'image\', res1)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Note: The function rect () with Open CV library typically is seen to be assuming that the top corner and left side boundary of the rectangle that is being drawn by the function are all-inclusive, while we see that the right boundary and bottom boundary are not included.
Conclusion
The Open CV rect function is responsible for reading the instructions that have been entered by the user and then analyzing to reflect it on the canvas of the screen. The background is set on the basis of which the rectangle is presented. Further, the color option is also presented to the user to select the specified color in which the rectangle needs to be shaped, and also a fill option is given in order to fill in the inward space that is made for the rectangle. this function is specifically used for image detection programs where face detection or object detection and framing confrontational operations happen. having the rect() function enables the verbosity of the programs where the function is implemented to be reduced and the efficiency and delivery time for the program to execute is made more efficient.
Recommended Articles
We hope that this EDUCBA information on “Open CV rect ()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.