This project implements an image segmentation tool using the Max-Flow Min-Cut algorithm (Edmonds-Karp). It treats an image as a graph where pixels are nodes and edges represent similarity. By finding the minimum cut in the graph, it separates the foreground object from the background.
While modern image segmentation often relies on Deep Learning, this project explores a classical algorithmic approach. It demonstrates how graph theory can be applied to computer vision tasks, providing interpretability and mathematical robustness.
Key Features:
- Graph Representation: Converts pixels into a grid graph.
- Intensity-Based Weights: Edge weights are based on pixel intensity differences.
- Max-Flow Min-Cut: Uses the Edmonds-Karp algorithm to find the optimal separation.
- Visualizations: Displays the original image and the resulting segmentation mask.
- Rich Output: Uses the
richlibrary for colorful console output and status updates.
- Preprocessing: The image is loaded, resized for performance, and converted to grayscale.
- Graph Construction:
- Nodes: Each pixel is a node. Two special nodes, Source (S) and Sink (T), are added.
- n-links (Neighbor Links): Adjacent pixels are connected. The weight is high if pixels are similar (strong bond) and low if they are different (weak bond).
- t-links (Terminal Links):
- Pixels at the image border are connected to the Sink (Background seeds).
- Pixels at the very center are connected to the Source (Foreground seeds).
- Max-Flow Calculation: The Edmonds-Karp algorithm finds the maximum flow from Source to Sink. The "bottlenecks" in this flow correspond to the edges that should be cut.
- Segmentation: The set of nodes reachable from the Source in the residual graph constitutes the foreground object.
.
βββ reports/ # Project reports and presentations
β βββ jules-review/
β β βββ jules_review.ipynb
β βββ presentation/
β βββ everything_is_a_graph.pdf
βββ src/ # Source code and assets
β βββ images/ # Directory containing input images and outputs
β β βββ graphics/ # Graphics and visualizations
β β βββ jules_tracker/ # Jules tracking related images
β β βββ output/ # Generated output images
β β βββ dragonite_og.jpeg # Default sample image
β βββ style.css # Styling for PDF viewer
βββ index.html # PDF viewer interface
βββ main.py # Main Python source code file
βββ requirements.txt # List of Python dependencies
βββ .gitignore # Git ignore rules
βββ LICENSE # Project license
βββ README.md # Project documentation
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Install dependencies: Ensure you have Python installed. Then run:
pip install -r requirements.txt
To run the segmentation on the default image:
python main.pyYou can modify main.py to change parameters:
- Image Path: Change the file path in the
ImageGraphinstantiation:processor = ImageGraph('path/to/your/image.jpg', width=40)
- Resolution: Adjust the
widthparameter. Higher values give better detail but increase computation time significantly (O(V E^2)). - Seeds: You can adjust the
seed_radiusor the logic for placing source/sink seeds in thebuild_t_linksmethod to suit different images.
Joshua PiΓ±a
Computer Science Department, Georgia State University
Data Science Senior | Program Manager | U.S Army Veteran