How to Draw on Image Using OpenCV in Computer Vision
To draw on an image in OpenCV, use functions like
cv2.line(), cv2.rectangle(), cv2.circle(), and cv2.putText(). These functions let you add lines, shapes, and text directly onto the image matrix for visualization or annotation.Syntax
OpenCV provides several drawing functions to add shapes and text on images. Each function requires the image, coordinates, color, thickness, and other optional parameters.
- cv2.line(img, pt1, pt2, color, thickness): Draws a line from
pt1topt2. - cv2.rectangle(img, pt1, pt2, color, thickness): Draws a rectangle with opposite corners
pt1andpt2. - cv2.circle(img, center, radius, color, thickness): Draws a circle at
centerwith givenradius. - cv2.putText(img, text, org, font, fontScale, color, thickness): Writes
textstarting atorgpoint.
python
cv2.line(img, (x1, y1), (x2, y2), (B, G, R), thickness)
cv2.rectangle(img, (x1, y1), (x2, y2), (B, G, R), thickness)
cv2.circle(img, (x, y), radius, (B, G, R), thickness)
cv2.putText(img, 'text', (x, y), cv2.FONT_HERSHEY_SIMPLEX, fontScale, (B, G, R), thickness)Example
This example loads a blank image and draws a blue line, a green rectangle, a red circle, and white text on it.
python
import cv2 import numpy as np # Create a black image img = np.zeros((400, 600, 3), dtype=np.uint8) # Draw a blue line from top-left to bottom-right cv2.line(img, (0, 0), (599, 399), (255, 0, 0), 5) # Draw a green rectangle cv2.rectangle(img, (50, 50), (200, 150), (0, 255, 0), 3) # Draw a red circle cv2.circle(img, (300, 200), 75, (0, 0, 255), -1) # filled circle # Put white text cv2.putText(img, 'OpenCV Drawing', (50, 350), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) # Show the image cv2.imshow('Drawings', img) cv2.waitKey(0) cv2.destroyAllWindows()
Output
A window opens showing a black image with a blue diagonal line, green rectangle, filled red circle, and white text 'OpenCV Drawing'.
Common Pitfalls
- Using incorrect color format: OpenCV uses BGR, not RGB, so red is (0,0,255), not (255,0,0).
- For thickness, -1 fills the shape; positive values draw outlines.
- Coordinates must be integers within image bounds; otherwise, drawing may fail or cause errors.
- For
cv2.putText, fontScale controls text size and must be positive. - Remember to call
cv2.imshow()andcv2.waitKey()to see the drawing.
python
import cv2 import numpy as np img = np.zeros((100, 200, 3), dtype=np.uint8) # Wrong color (RGB instead of BGR) - text will appear blue instead of red cv2.putText(img, 'Wrong Color', (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) # Correct color (BGR) - text appears red cv2.putText(img, 'Correct Color', (5, 90), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow('Color Pitfall', img) cv2.waitKey(0) cv2.destroyAllWindows()
Output
A window shows two texts: the first in blue (wrong color), the second in red (correct color).
Quick Reference
Remember these key points when drawing on images with OpenCV:
- Colors use BGR format, not RGB.
- Thickness -1 fills shapes; positive values draw outlines.
- Coordinates are (x, y) pixels, integers within image size.
- Use
cv2.FONT_HERSHEY_SIMPLEXor other fonts for text. - Always display image with
cv2.imshow()and wait for key press.
Key Takeaways
Use OpenCV drawing functions like cv2.line, cv2.rectangle, cv2.circle, and cv2.putText to add shapes and text on images.
Colors in OpenCV are in BGR order, so specify colors accordingly.
Thickness -1 fills shapes; positive thickness draws outlines.
Coordinates must be integers within the image dimensions.
Always use cv2.imshow and cv2.waitKey to display the image with drawings.