Practice and reinforce the concepts from Lesson 14
This exercise will guide you through various image processing techniques using OpenCV in Python. You'll practice image resizing and conversion to different color spaces.
Access the exercise materials here: Drive Link
⏱️ Time needed: 5 minutes
:bulb: Make sure you have OpenCV installed by running
pip install opencv-python
in your terminal before starting!
:computer: Read, Think, Code
⏱️ Time needed: 30 minutes
Question One: Rescale image
⏱️ Estimated time: 10 minutes
In
resize_image.py
file:
- Resize the image to scale 0.2
- Create variables for scale, width, height and dim
- Apply the
cv.resize()
syntax and replace the original image- Show the resized image using
cv.imshow()
tip Remember that scale 0.2 means the image will be 20% of its original size. Calculate the new dimensions using:
width = int(img.shape[1] * scale)
height = int(img.shape[0] * scale)
⏱️ Estimated time: 20 minutes
In convert_image.py
file:
Convert image to Gray color
cv.cvtColor()
syntax with cv.COLOR_BGR2GRAY
and store it as graycv.imshow('Gray', gray)
Convert image to HSV color
cv.cvtColor()
syntax with cv.COLOR_BGR2HSV
and store it as hsvcv.imshow('HSV', hsv)
Convert image to RGB color
cv.cvtColor()
syntax with cv.COLOR_BGR2RGB
and store it as rgbcv.imshow('RGB', rgb)
Convert image to Canny Edge
cv.Canny()
syntax with threshold values (100, 200) and store it as cannycv.imshow('Canny', canny)
Convert image to Blur
cv.GaussianBlur()
syntax with kernel size (7,7) and store as blurcv.imshow('Blur', blur)
:bulb: For Canny Edge detection, experiment with different threshold values to see how they affect edge detection sensitivity!
The final output will be shown as follows:
:art: Try it Yourself
⏱️ Time needed: 15 minutes
Create a
main.py
file and practice with your own image:
- Find and load an image
- Use any image from your computer or download one
- Load it using
cv.imread('your_image.jpg')
- Apply resize operations
- Try different scale factors (0.5, 0.75, 1.5)
- Experiment with different interpolation methods
- Apply multiple conversions
- Combine different effects (e.g., convert to gray then apply blur)
- Save your favorite results using
cv.imwrite()
tip Try chaining effects! For example, convert to grayscale first, then apply Canny Edge detection for cleaner edge results.
:warning: Important! Make sure to submit your completed work before the deadline!
Please submit your work through this link: Exercise Submission Form
resize_image.py
fileconvert_image.py
filemain.py
file with custom experimentsImage not loading?
OpenCV not found?
pip install opencv-python
conda install opencv
Window not showing?
cv.waitKey(0)
after cv.imshow()
cv.destroyAllWindows()
at the end of your scriptColors look wrong?
cv.COLOR_BGR2RGB
when needed