- Project Overview
- Features
- Prerequisites
- Installation
- Usage
- Step-by-Step Guide
- Parameters
- Examples
- Troubleshooting
- Contributing
- License
This project implements the Canny edge detection algorithm, a multi-stage process to detect a wide range of edges in images. The Canny edge detector is widely used in computer vision applications for feature detection and feature extraction.
- Complete implementation of the Canny edge detection algorithm
- Customizable parameters for different edge detection needs
- Support for various image formats
- Visualization of intermediate processing steps
- Optimized for performance
Before you begin, ensure you have met the following requirements:
- Python 3.7 or higher
- pip package manager
- Basic understanding of image processing concepts
Follow these steps to set up the project:
- Clone the repository:
git clone https://github.com/shuvrajit-dey21/canny-edge-detector.git
cd canny-edge-detector- Create and activate a virtual environment (recommended):
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`- Install the required dependencies:
pip install -r requirements.txtTo run the Canny edge detector on an image:
python canny_edge_detector.py --input input_image.jpg --output output_image.pngThe Canny edge detection process consists of the following steps:
-
Noise Reduction
- The image is smoothed using a Gaussian filter to reduce noise
-
Gradient Calculation
- The intensity gradients of the image are calculated
- Both magnitude and direction of gradients are computed
-
Non-Maximum Suppression
- Only local maxima in gradient directions are kept
- This thins the edges
-
Double Thresholding
- Potential edges are determined by two threshold values
- Strong edges, weak edges, and non-edges are classified
-
Edge Tracking by Hysteresis
- Weak edges are either kept or discarded based on connectivity
- Only edges connected to strong edges are preserved
The script accepts the following parameters:
--input: Path to input image (required)--output: Path to save output image (required)--low_threshold: Low threshold for hysteresis (default: 50)--high_threshold: High threshold for hysteresis (default: 150)--sigma: Sigma value for Gaussian blur (default: 1.4)--kernel_size: Size of Gaussian kernel (default: 5)
- Basic edge detection:
python canny_edge_detector.py --input sample.jpg --output edges.png- Custom threshold values:
python canny_edge_detector.py --input sample.jpg --output edges.png --low_threshold 30 --high_threshold 100- Different blur settings:
python canny_edge_detector.py --input sample.jpg --output edges.png --sigma 2.0 --kernel_size 7- Image not found: Ensure the input path is correct
- Blurry output: Try adjusting the sigma and kernel_size parameters
- Too many/too few edges: Adjust the threshold values
- Memory issues: Reduce image size before processing
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a new branch (
git checkout -b feature-branch) - Commit your changes (
git commit -m 'Add new feature') - Push to the branch (
git push origin feature-branch) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.