Filter Type | Description | Example Kernel |
---|---|---|
Mean (Average) | Blurs image by averaging surrounding pixels |
1/9 * [[1,1,1], [1,1,1], [1,1,1]] |
Gaussian | Smooths image with Gaussian-weighted average | cv2.GaussianBlur(image, (5,5), sigmaX) |
Laplacian | Detects rapid intensity changes | cv2.Laplacian(image, cv2.CV_64F) |
Sobel | Finds gradients in x or y direction | cv2.Sobel(image, cv2.CV_64F, dx, dy) |
!pip install opencv-python-headless matplotlib
import cv2
import numpy as np
import matplotlib.pyplot as plt
from google.colab import files
from google.colab.patches import cv2_imshow
uploaded = files.upload()
img_path = next(iter(uploaded))
image = cv2.imread(img_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2_imshow(gray)
# 1. Smoothing (Gaussian Blur)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
cv2_imshow(blur)
# 2. Sobel Edge Detection
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
cv2_imshow(cv2.convertScaleAbs(sobelx))
cv2_imshow(cv2.convertScaleAbs(sobely))
# 3. Canny Edge Detection
canny = cv2.Canny(gray, 100, 200)
cv2_imshow(canny)
# 4. Histogram Equalization
equalized = cv2.equalizeHist(gray)
cv2_imshow(equalized)
In industrial inspection, edge detection is used to verify product shape, completeness, or detect defects in manufacturing lines.
Objective: Practice edge detection and image enhancement with OpenCV.
Due: End of Week 3