Skip to content

rkr14/reImage

Repository files navigation

reImage - Interactive Graph-Cut Image Segmentation

Professional image segmentation tool using graph-cut algorithm with AVX2-optimized C++ backend and PyQt6 GUI.

Features

  • Interactive Scribble Interface - Draw foreground/background seeds with adjustable brush
  • AVX2 Optimized - 3-4x faster with SIMD vectorization
  • Auto-Display Results - See segmentation overlay instantly
  • Export Results - Save segmented images with transparency
  • Standalone Executable - No installation required (Windows .exe provided)

Quick Start (Windows)

Download reImage.exe from releases and double-click to run. No installation needed!

Algorithm

Uses graph-cut segmentation with:

  • Boykov-Kolmogorov (BK) and Dinic max-flow algorithms for min-cut computation
  • Gaussian Mixture Models (GMM) with 5 components per model (fitted via Expectation-Maximization) and fallback to 8×8×8 RGB histograms
  • Adaptive β for pairwise smoothness terms
  • 4-neighborhood graph structure

Building from Source

Prerequisites

C++ Backend:

  • CMake 3.10+
  • GCC/Clang (with AVX2 support) or MSVC
  • C++23 compiler

Python GUI:

  • Python 3.12+
  • uv (package manager)

Build C++ Backend

Windows (MinGW):

cd cpp/build
cmake .. -G "MinGW Makefiles"
cmake --build . -j

Linux / macOS:

cd cpp
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)

Build

Install Python dependencies:

uv sync
uv pip install pyinstaller pyqt6

Create standalone executable:

uv run --no-sync pyinstaller reImage.spec --clean

Output: dist/reImage.exe

# Rectangle mode
./cpp/build/segment image.bin W H rect x0 y0 x1 y1 output.bin

# Mask mode (with scribbles)
./cpp/build/segment image.bin W H mask seed.bin output.bin

Optimizations

AVX2 SIMD & Memory Layout

  • pre-converted aligned double pixel array for direct SIMD loads
  • FMA-powered polynomial minimax approximations for exp and log (removing slow scalar math calls)
  • Batch color distance computations (4 pixels at once)
  • Vectorized operations in beta calculation
  • ~10-25x speedup on graph construction and cost evaluation

Compiler Flags

  • -O3 - Maximum optimization

  • -march=native - CPU-specific instructions

  • -mavx2 - Enable AVX2 explicitly

  • -ffast-math - Aggressive floating-point

  • LTO enabled for minimal binary size

  • Inline getColor() (called millions of times)

  • alignas(32) memory alignment for SIMD

  • Cache-friendly loop ordering (horizontal/vertical separation)

  • Const/constexpr where applicable

Project Structure

reImage/
├── cpp/                    # C++ backend
│   ├── main.cpp           # CLI interface
│   ├── Image.{h,cpp}      # Image loading (AVX2-aligned)
│   ├── SeedMask.{h,cpp}   # Foreground/background seeds
│   ├── Gmm.{h,cpp}        # Gaussian Mixture Models (EM fitting)
│   ├── DataModel.{h,cpp}  # GMM/Histogram-based unary costs
│   ├── GraphBuilder.{h,cpp} # Graph construction (AVX2)
│   ├── BoykovKolmogorov.{h,cpp} # BK Max-flow algorithm
│   ├── Dinic.{h,cpp}      # Dinic Max-flow algorithm
│   ├── Segmenter.{h,cpp}  # Orchestration
│   ├── MinCut.h           # Min-cut extraction
│   ├── SimdOps.h          # AVX2 intrinsics
│   └── CMakeLists.txt
├── gui_app.py             # PyQt6 GUI application
├── reImage.spec           # PyInstaller build config
└── pyproject.toml         # Python dependencies

Performance

  • Binary Size: 132KB (C++), 91MB (standalone GUI with dependencies)
  • Typical Segmentation: <0.3s for 640x480 images, <1.7s for 1080p images using GMM + BK
  • Max-Flow Speedup: Up to 25x speedup with Boykov-Kolmogorov compared to Dinic solver
  • Memory: ~40MB for graph construction on HD inputs

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors