Skip to content

mrazza/Leiter

Repository files navigation

Leiter

A pure-C# image processing library designed for image segmentation, color space conversions, and visual saliency map computation. It is fully generic, optimized, and has zero external dependencies on its core algorithms.

Features

  • Multi-OS Platform Support: Integrates seamlessly with Native Windows GDI+ / Bitmap (System.Drawing.Common) and Linux/Cross-Platform SixLabors ImageSharp via platform-specific extension packages.
  • Efficient Graph-Based Image Segmentation (EGBI): Fully multi-threaded implementation of the Felzenszwalb-Huttenlocher segmentation algorithm.
  • Saliency Detection:
    • Global Contrast Saliency: Histogram-based saliency region detection with optional color space smoothing.
    • Regional Contrast Saliency: Region-based contrast algorithm leveraging image segmentation, spatial distance, and border region handling.
  • Advanced Color Space Conversions: Support for conversions across sRGB, CIEXYZ (Xyz32), and CIELAB (Lab32), including CIEDE2000 color distance calculations.
  • Image Blurring: Multi-threaded Gaussian Blur implementation.
  • Generic Pixel Types: Strongly-typed representation of pixel data (Rgb8, Rgb64, Lab32, Xyz32, DoublePixel, LongPixel) supporting generic pixel/scalar mathematics.

Example Usage

Here is a comprehensive example demonstrating how to load an image, perform Gaussian blurring, segment it, and compute both global and regional visual saliency maps.

using System;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Leiter.Core;
using Leiter.Pixels;
using Leiter.Pixels.ColorSpaces;
using Leiter.Algorithms;
using Leiter.Algorithms.DataStructures;
using Leiter.Algorithms.Segmentation;
using Leiter.Algorithms.Saliency;
using Leiter.Platform.Linux; // Or Leiter.Platform.Windows on Windows

class Program
{
    static void Main()
    {
        // 1. Load an image using ImageSharp
        using var image = Image.Load<Rgb24>("input.png");
        
        // 2. Convert SixLabors Image to a generic Leiter Matrix
        Matrix<Rgb8> imageMatrix = image.ToSequentialMatrix();
        
        // 3. Smooth the image using Gaussian Blur
        Matrix<Rgb8> blurredImageMatrix = Blur.GaussianBlur(imageMatrix, 0.8f, 2);
        
        // 4. Convert colors to Lab32 color space for precise delta-E calculations
        var labImageMatrix = blurredImageMatrix.Map(pixel => pixel.ToLab32(RgbColorSpace.sRGB));
        
        // 5. Segment the image using EGBI (Felzenszwalb algorithm)
        IDisjointSet segmentationMatrix = EgbiSegmentation.Segment(labImageMatrix, kFactor: 50, minSegmentSize: 200);
        
        // 6. Color image by segments using the Average Color Assigner
        var imageSegmented = imageMatrix.Clone();
        EgbiSegmentation.ColorImageBySegmentation(
            imageSegmented, 
            segmentationMatrix.ToRegions(), 
            EgbiSegmentation.AverageColorAssigner()
        );
        using var segmentedImg = imageSegmented.ToImage();
        segmentedImg.Save("segmented.png");
        
        // 7. Compute Global Contrast Saliency Map
        var globalSaliencyMatrix = GlobalContrastSaliency.ComputeSaliency(imageMatrix);
        var globalSaliencyImageMatrix = globalSaliencyMatrix.Map(s => new Rgb8((byte)(s.Value * 255), (byte)(s.Value * 255), (byte)(s.Value * 255)));
        using var globalSaliencyImg = globalSaliencyImageMatrix.ToImage();
        globalSaliencyImg.Save("global_saliency.png");
        
        // 8. Compute Regional Contrast Saliency Map
        var regionalSaliencyMatrix = RegionalContrastSaliency.ComputeSaliency(imageMatrix, segmentationMatrix);
        var regionalSaliencyImageMatrix = regionalSaliencyMatrix.Map(s => new Rgb8((byte)(s.Value * 255), (byte)(s.Value * 255), (byte)(s.Value * 255)));
        using var regionalSaliencyImg = regionalSaliencyImageMatrix.ToImage();
        regionalSaliencyImg.Save("regional_saliency.png");
    }
}

Visual Saliency & Segmentation Showcase

Below is an actual example demonstrating the outputs of the library processing a studio-lit red tulip.

Original Input Image Segmentation (EGBI)
Original image loaded into Leiter matrix Segmented regions colored with segment averages
Global Contrast Saliency Regional Contrast Saliency
Saliency calculated using per-pixel global color contrast Saliency calculated incorporating segment regions & spatial weights

Library Architecture

  • Leiter: Core library hosting all pure generic image structures, conversions, algorithms, segmentation, and saliency models.
  • Leiter.Platform.Windows: Integrates with GDI+ / System.Drawing.Common to support Native Windows platforms.
  • Leiter.Platform.Linux: Integrates with SixLabors ImageSharp to support Linux and generic cross-platform deployments.
  • Leiter.Tests: High-coverage unit test suite validating mathematical, platform, and algorithmic consistency.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

About

A self-contained, pure-C#, image processing library.

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages