Updated April 17, 2023
Introduction to OpenCV ellipse
OpenCV ellipse() is a command present in the OpenCV Public Library, which is used to draw an ellipse on top of an image that the user has provided. This library had been designed for utilizing various commands to be executed on Python programming language, which are aimed at solving problems related to computer vision.
Syntax and parameters of Using OpenCV ellipse()
The following syntax has to be used in order to utilize the OpenCV ellipse function for drawing an ellipse or circular shape on top of the image which the user has provided:
cv2.ellipse ( image , center Coordinates , axes Length , angle , start Angle , end Angle , color [, thickness [, line Type [, shift ] ] ] )
Parameters:
The following parameters are used for making use of the OpenCV ellipse command:
Parameter Used | Description of Parameter |
image | It is the original image which the user has provided in order to place an ellipse on top of it |
center coordinates | The parameter is used to represent the central coordinates corresponding to the ellipse drawn on the image provided. A tuple represents the coordinates with two values representing the X coordinate value and the Y coordinate value. |
axes length | The parameter is used to represent the length of the axis for the ellipse drawn on the image provided. Two couples are passed by the system used to represent two different variables containing the coordinates for the ellipse’s minor axis and the coordinates of the ellipse’s major axis representing their length. |
start angle | This parameter is used for representing the angle taken for starting of the elliptic arc. The value of this parameter is calculated in degrees. |
angle | This parameter is used for representing the rotation angle for the ellipse that is being drawn on the image provided by the user. This parameter is measured in terms of degrees. |
end angle | This parameter is used for representing the angle taken for the ending of the elliptic arc. The value of this parameter is calculated in degrees. |
color | This parameter is used for representing the color which is to be chosen for the border of the ellipse or lying of the shape which is to be drawn on the image which the user provides. For the representation of the color using the blue, green, red matrix or BGR, we need to pass a tuple. For instance, the user needs to draw the ellipse using the color blue the tuple to be passed will have the coordinates of (255, 0, 0 ). |
thickness | This parameter represents the exact thickness that needs to be taken to draw the shape border for the ellipse, which is being made on the image the user has provided. This parameter is measured in terms of pixel or PX. The default value for the thickness of the ellipse is taken as minus one PX, which will be used for filling the shape in the color specified by the user using the BGR matrix. |
line type | This parameter is optional. This parameter is used to represent that type offline, which is used to draw the ellipse boundary on the image the user has provided. |
shift | This parameter is used to denote the total number of fraction-based bits present in the coordinates with respect to the center coordinate and the value given to the axis. |
return value | This method is responsible for returning an output image the is to be loaded from the file which has been specified. |
Example of OpenCV ellipse
Below are the examples of OpenCV ellipse:
A program that has been coded in the Python programming language to explain the use of the cv2.ellipse() function.
Command used to import the OpenCV library to utilize the OpenCV erosion function.
Code:
import cv2
# path defined for selecting the image
path1 = r'C:\Users\Priyanka Banerjee\Desktop\EduCBA.png'
# command used for reading an image from the disk that has been instructed by the user
image1 = cv2.imread(path)
# Defining the name of the window in which the original image being loaded by the user will be displayed in
window_name1 = 'Image'
#Defining the values for the center coordinates
center_coordinates1 = (1200, 1000)
#defining the value to be given for the length of the axes
axesLength1 = (1000, 500)
#defining the angle for the ellipse
angle1 = 0
# defining the starting angle and the ending angle
startAngle1 = 0
endAngle1 = 360
# Red color being defined in BGR tuple vale
color1 = (0, 0, 255)
# Defining the thickness of the ellipse line at 5 px
thickness1 = 5
# Making use of the cv2.ellipse() function
# Draw an ellipse using a red coloured line with a thickness of 5px
Image2 = cv2.ellipse(image1, center_coordinates1, axesLength1,
angle1, startAngle1, endAngle1, color1, thickness1)
# Defining the name of the window in which the final output image being loaded by the user will be displayed in
window_name2 = 'Output'
# Displaying the output image
cv2.imshow(window_name2, image2)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
The original image entered by the user:
Final output image:
Conclusion
The OpenCV ellipse() function, which is used to draw an ellipse on top of an image that the user has provided, is one of the most utilized functions in the OpenCV library. This function has been designed to help coders, and the programming community development programs that aim to solve problems related to computer vision, especially object and face detection, on a real-time basis.
Recommended Articles
We hope that this EDUCBA information on “OpenCV ellipse” was beneficial to you. You can view EDUCBA’s recommended articles for more information.