By the end of this lesson, you will:
:information_source: Image Processing is the process of performing operations on images to get useful output. Think of it like editing photos on your phone, but with code!
Image processing goes by many names:
But they all do the same thing: transform images to make them more useful!
:bulb: Why AI Needs Image Processing AI models need images in specific formats to learn effectively. For example, many AI models use grayscale images because they're simpler to process!
Image processing helps us in amazing ways:
:emoji: Photography
:emoji: Medical Field
:emoji: Robot Vision
:art: Color Processing
:iphone: Pattern Recognition
OpenCV is a powerful tool that lets us process images with Python. Today, we'll learn two essential skills:
:memo: Before we start coding, make sure OpenCV is installed and imported in your Python environment!
:emoji: Resizing Images
Images come in all sizes! Sometimes they're too big for our screen, or too small to see details. That's why we need to resize them.
How to Resize
We use OpenCV's resize function:
cv.resize(image, dim, interpolation = cv.INTER_AREA)
You can resize images in three ways:
- Scale - Make it proportionally smaller or larger (like 50% or 200%)
- Width - Set a specific width in pixels
- Height - Set a specific height in pixels
Example: Scaling an Image
Let's make an image 5 times smaller (scale = 0.2):
python
# resize image scale = 0.2 width = int(img.shape[1]*scale) height = int(img.shape[0]*scale) dim = (width, height) img = cv.resize(img, dim, interpolation = cv.INTER_AREA)
tip Keep Images Looking Good Always use scale to resize images proportionally. If you only change width or height, your image might look stretched or squished!
Just like filters on your phone, we can transform images in different ways. Each transformation has a special purpose!
:information_source: OpenCV's Color System OpenCV uses BGR (Blue, Green, Red) by default, which is different from the usual RGB order. Don't worry - we can easily convert between them!
Example for HSV.
Example for RGB.
Example for Canny Edge.
Examples for Blur.
Here are the main conversions you'll use:
Conversion | Function | What it Does |
---|---|---|
Grayscale | cv.cvtColor(image, cv.COLOR_BGR2GRAY) |
Removes all colors, keeping only shades of gray |
HSV | cv.cvtColor(image, cv.COLOR_BGR2HSV) |
Changes to Hue-Saturation-Value format for color analysis |
RGB | cv.cvtColor(image, cv.COLOR_BGR2RGB) |
Converts to standard Red-Green-Blue format |
Canny Edge | cv.Canny(image, 125, 175) |
Finds edges and outlines in images |
Blur | cv.GaussianBlur(image, (7,7), cv.BORDER_DEFAULT) |
Makes images softer and less sharp |
:memo: Important Parameters
- Canny Edge: Use minValue=125 and maxValue=175 for best results
- Blur: Box filter dimensions must be odd numbers like (3,3), (5,5), or (7,7)
Here's how to apply each conversion:
# Convert to gray
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# Convert to HSV
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
# Convert to RGB
rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
# Convert to Canny Edge
canny = cv.Canny(img, 125, 175)
# Convert to Blur
blur = cv.GaussianBlur(img, (7, 7), cv.BORDER_DEFAULT)
You've learned powerful image processing techniques:
These tools are the foundation for computer vision and AI applications!
Code with AI: Try using AI to perform image manipulation.
Prompts to Try:
:bulb: Next Steps Try combining these techniques! For example, you could resize an image first, then apply edge detection to find shapes more easily.