By the end of this lesson, you will be able to:
:information_source: What is Drawing on Images? Drawing on images means adding shapes, lines, or text to an image. This is how we show the results of computer vision tasks like object detection or face recognition.
We draw on images to show what our computer vision programs found. For example:
Before we start drawing, let's learn how OpenCV locates points on an image:
:memo: Coordinate System
- X-axis: Goes from left to right (horizontal)
- Y-axis: Goes from top to bottom (vertical)
- Origin (0,0): The top-left corner of the image
:bulb: Getting Started Before we start drawing, make sure you have OpenCV installed and imported in your Python code:
python
import cv2 as cv
In this lesson, we'll master four essential drawing tools:
Rectangles are the most common shape we draw. They're perfect for showing where objects are located.
cv.rectangle(image, top_left_point, bottom_right_point, color, thickness=thickness)
Let's draw a green rectangle in the top-left corner:
# Draw Rectangle
cv.rectangle(img, (0, 0), (50, 50), (0, 255, 0), thickness = 2)
:bulb: Color Values Remember: OpenCV uses BGR (Blue, Green, Red) instead of RGB!
- (0, 255, 0) = Pure Green
- (255, 0, 0) = Pure Blue
- (0, 0, 255) = Pure Red
Circles are great for marking specific points or creating rounded highlights.
cv.circle(image, center_point, radius, color, thickness=thickness)
Let's draw a white circle in the middle of our image:
# Draw Circle
cv.circle(img, (image.shape[1]//2, image.shape[0]//2), 100, (255, 255, 255), thickness = 4)
:memo: Finding the Center
image.shape[1]//2
gives us the horizontal center (width / 2)image.shape[0]//2
gives us the vertical center (height / 2)//
means integer division (no decimals)
Lines help us connect points or show directions and paths.
cv.line(image, starting_point, ending_point, color, thickness=thickness)
Let's draw a thick blue line going diagonally:
# Draw Line
cv.line(img, (0, 50), (250, 250), (255, 0, 0), thickness = 10)
:bulb: Line Uses Lines are perfect for:
- Showing connections between objects
- Drawing paths or trajectories
- Creating simple diagrams
Adding text labels makes your images more informative and easier to understand.
cv.putText(image, text, starting_point, font_style, size, color, thickness=thickness)
Let's write "Telebort" in red with a fancy font:
# Write Text
cv.putText(img, "Telebort", (100,100), cv.FONT_HERSHEY_COMPLEX, 2.0, (0, 0, 255), thickness = 3)
:memo: Font Styles OpenCV includes several font styles:
cv.FONT_HERSHEY_SIMPLEX
- Simple and cleancv.FONT_HERSHEY_COMPLEX
- More decorativecv.FONT_HERSHEY_PLAIN
- Very basicFor more font styles, check out: Fonts in OpenCV
You've learned four powerful drawing tools in OpenCV:
These tools are the foundation for visualizing computer vision results. Every time your program detects something, you can use these tools to show what was found!
Code with AI: Try using AI to learn OpenCV drawing functions.
Prompts:
Try these exercises to master drawing with OpenCV:
:bulb: Remember
- Always test your coordinates on a small scale first
- Use different colors to make your drawings stand out
- Combine multiple shapes to create complex drawings