Lecture

Advanced Image Processing Techniques Using OpenCV

In this lesson, we will cover advanced image processing techniques using OpenCV, such as edge detection, blurring, and object detection.


1. Edge Detection - Canny Edge Detection

Edge detection is a technique used to identify boundaries within images.

Canny Edge Detection is one of the most widely used edge detection algorithms.

Canny Edge Detection Example
import cv2 # Load the image image = cv2.imread("sample.jpg", cv2.IMREAD_GRAYSCALE) # Apply Canny edge detection edges = cv2.Canny(image, 100, 200) cv2.imshow("Canny Edges", edges) cv2.waitKey(0) cv2.destroyAllWindows()

2. Blur and Noise Reduction

To reduce noise or smooth (blur) the image, you can use GaussianBlur().

Gaussian Blur Example
blurred = cv2.GaussianBlur(image, (5, 5), 0) cv2.imshow("Blurred Image", blurred) cv2.waitKey(0) cv2.destroyAllWindows()

Here, (5, 5) is the kernel size, and larger values result in a stronger blur effect.


3. Contour Detection

Contours are used to find the boundaries of objects within images.

You can use OpenCV's cv2.findContours() to detect them.

Contour Detection Example
# Load and convert the image image = cv2.imread("sample.jpg") gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # Detect contours contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Draw detected contours cv2.drawContours(image, contours, -1, (0, 255, 0), 2) cv2.imshow("Contours", image) cv2.waitKey(0) cv2.destroyAllWindows()

In this code, after thresholding, cv2.findContours() is used to detect contours, and cv2.drawContours() displays them in green.


4. Face and Object Detection (Haar Cascade)

OpenCV allows you to detect faces using pre-trained Haar Cascade Classifiers.

Face Detection Example
# Load the Haar Cascade classifier face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") # Load and convert the image gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Detect faces faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) # Draw rectangles around detected faces for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)

This code example detects faces and draws rectangles around them.


In this lesson, we covered advanced OpenCV features such as edge detection, blurring, contour detection, and face detection.

In the next lesson, we will solve a simple quiz based on what we have learned today!


Reference Materials

Mission
0 / 1

Canny edge detection is one of the most widely used algorithms for detecting edges in images.

True
False

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help