Practice and reinforce the concepts from Lesson 15
This exercise will guide you through various drawing techniques using OpenCV in Python. You'll practice drawing shapes and text on images to create visual outputs.
Access the exercise materials here: Drive Link
Time needed: 5 minutes
:bulb: For students without VSCode: You can use any Python IDE or online Python editor. Just make sure you have OpenCV installed!
Read, Think, Code :computer:
Time needed: 20-25 minutes
Working in
draw.py
file:Question One: Draw Rectangle
- Locate the Question 1 section in your code
- Use
cv2.rectangle()
function to draw a rectangle with these specifications:
- Top left corner:
(0, 0)
- Bottom right corner:
(250, 250)
- Color: Blue
(255, 0, 0)
in BGR format- Thickness:
2
pixelsQuestion 2: Draw Circle
- Find the Question 2 section
- Use
cv2.circle()
function to draw a circle with:
- Center point: Calculate the middle of the image using
(width//2, height//2)
- Radius:
100
pixels- Color: Green
(0, 255, 0)
in BGR format- Thickness:
4
pixels tip Hint: To find the middle of the image, useimg.shape
to get the dimensions, then divide by 2!
Question 3: Draw Line
cv2.line()
function to draw a line with:
(100, 0)
(0, 0, 255)
in BGR format4
pixelsQuestion 4: Write Text
cv2.putText()
function to add text with:
"Hello"
(200, 50)
cv2.FONT_HERSHEY_COMPLEX
1.0
(255, 255, 0)
in BGR format2
pixels:bulb: Remember: OpenCV uses BGR color format, not RGB! So (255, 255, 0) gives you cyan, not yellow.
Expected Output
Your final image should look like this:
Try it Yourself :art:
Time needed: 15-20 minutes
Advanced Challenge
- Create a new file called
advanced.py
- Copy the basic structure from
draw.py
- Create the following output by combining multiple shapes:
tip Challenge Hints:
"cv2 module not found" error
pip install opencv-python
Image not displaying
cv2.imshow()
and cv2.waitKey(0)
at the endWrong colors appearing
Shapes not appearing where expected
:warning: Warning Important: Submit your work!
Once you've completed both
draw.py
andadvanced.py
:
- Take screenshots of both output images
- Save your Python files
- Submit everything through the form below
Submission Deadline: Check with your instructor