This repository contains a Python script that performs edge detection on an input image using two different techniques: Canny Edge Detection and Marr-Hildreth Edge Detection. The results of both methods are visualized alongside the original image for easy comparison.
- Loads an image from a file and converts it to grayscale.
- Applies Canny Edge Detection to highlight edges in the image.
- Applies Marr-Hildreth Edge Detection using the Laplacian filter.
- Displays the original image, the edge-detection results, and the final output images in a 2x3 grid format for comparison.
- Uses Matplotlib to visualize and compare the results.
Before running the script, ensure you have the following Python libraries installed:
- OpenCV: For image processing.
- NumPy: For numerical operations.
- Matplotlib: For visualization.
You can install the required libraries using pip:
pip install opencv-python numpy matplotlib-
Clone this repository to your local machine:
git clone https://github.com/yourusername/edge-detection.git
-
Place the image you want to process (e.g.,
img.jpg) in the project directory. -
Run the script
edge_detection.py:python edge_detection.py
-
The script will process the image and display the results in a grid, including:
- The original image.
- The Canny edge detection result.
- The output of the Canny method overlaid on the original image.
- The Marr-Hildreth edge detection result.
- The output of the Marr-Hildreth method overlaid on the original image.
-
Image Loading & Preprocessing:
- The input image is read using OpenCV’s
cv2.imread(). - The image is converted to grayscale using
cv2.cvtColor()to simplify edge detection.
- The input image is read using OpenCV’s
-
Canny Edge Detection:
- The Canny algorithm is applied using
cv2.Canny(). It detects edges by finding areas of rapid intensity changes in the image.
- The Canny algorithm is applied using
-
Marr-Hildreth Edge Detection:
- The Laplacian of the grayscale image is calculated using
cv2.Laplacian(). - The absolute values of the Laplacian are taken to highlight edges and then scaled to 8-bit values with
cv2.convertScaleAbs().
- The Laplacian of the grayscale image is calculated using
-
Visualization:
- The images are displayed using Matplotlib. A 2x3 grid is used to show:
- The original image.
- The edge detection results (Canny and Marr-Hildreth).
- The original image with the detected edges overlaid.
- The images are displayed using Matplotlib. A 2x3 grid is used to show: