15 Mar Draw an ellipse on an image with OpenCV
In OpenCV, use the cv2.ellipse() function to draw a rectangle on an image. First, we will read the image using the imread() function in OpenCV.
Let us now see the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Draw an ellipse on an image import cv2 # Load an image image = cv2.imread(r'C:\Users\hp\Downloads\amit.jpeg', 1) # Define new dimensions (width, height) new_dimensions = (600, 600) # Resize the image resized_image = cv2.resize(image, new_dimensions) # Draw an ellipse on an image cv2.ellipse(resized_image, (150, 50), (80, 20), 5, 0, 360, (255,0,0), -1) # Display the resized image cv2.imshow("Image Resized:", resized_image) # Wait for a key press and close the window cv2.waitKey(0) cv2.destroyAllWindows() |
The following is the output:
Explanation:
This Python script employs the OpenCV library to load, resize, and draw an ellipse on an image:
- It begins by importing the OpenCV library, and then reads an image from the specified file path, loading it in color.
- The image is resized to dimensions of 600×600 pixels using the
cv2.resize()
function. - After resizing, the script draws a blue ellipse on the image. The
cv2.ellipse()
function is used, with the first argument specifying the image, the second argument specifying the center coordinates of the ellipse (150, 50), the third argument specifying the axes lengths (80, 20), the fourth argument specifying the rotation angle (5 degrees), the fifth and sixth arguments specifying the start and end angles (0 and 360 degrees), the seventh argument specifying the color (blue), and the eighth argument specifying the thickness (where-1
fills the ellipse). - Finally, the script displays the resized image with the drawn ellipse in a window titled “Image Resized:” using
cv2.imshow()
, waits for a key press withcv2.waitKey(0)
, and closes all OpenCV windows withcv2.destroyAllWindows()
. - This process allows users to view the modified image with the drawn ellipse and close the display window at their convenience.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
- Generative AI Tutorial
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- Copilot Tutorial
- Gemini Tutorial
- ChatGPT Tutorial
No Comments