1 unstable release

Uses old Rust 2015

0.1.0 Feb 25, 2018

#772 in #input

MIT/Apache

31KB
591 lines

This crate provides an implementation of Chan-Vese level-sets described in Active contours without edges by T. Chan and L. Vese. It is a port of the Python implementation by Kevin Keraudren on Github and of the Matlab implementation by Shawn Lankton.

Examples

To use the functions inside module chanvese::utils you need to compile this crate with the feature image-utils.

extern crate image;
extern crate rand;
extern crate chanvese;

use std::f64;
use std::fs::File;
use image::ImageBuffer;
use rand::distributions::{Sample, Range};
use chanvese::{FloatGrid, BoolGrid, chanvese};

use chanvese::utils;

fn main() {
    // create an input image (blurred and noisy ellipses)
    let img = {
        let mut img = ImageBuffer::new(256, 128);
        for (x, y, pixel) in img.enumerate_pixels_mut() {
            if (x-100)*(x-100)+(y-70)*(y-70) <= 35*35 {
                *pixel = image::Luma([200u8]);
            }
            if (x-128)*(x-128)/2+(y-50)*(y-50) <= 30*30 {
                *pixel = image::Luma([150u8]);
            }
        }
        img = image::imageops::blur(&img, 5.);
        let mut noiserange = Range::new(0.0f32, 30.);
        let mut rng = rand::thread_rng();
        for (_, _, pixel) in img.enumerate_pixels_mut() {
            *pixel = image::Luma([pixel.data[0] + noiserange.sample(&mut rng) as u8]);
        }
        let ref mut imgout = File::create("image.png").unwrap();
        image::ImageLuma8(img.clone()).save(imgout, image::PNG).unwrap();
        let mut result = FloatGrid::new(256, 128);

        for (x, y, pixel) in img.enumerate_pixels() {
            result.set(x as usize, y as usize, pixel.data[0] as f64);
        }
        result
    };

    // create a rough mask
    let mask = {
        let mut result = BoolGrid::new(img.width(), img.height());
        for (x, y, value) in result.iter_mut() {
            if (x >= 65 && x <= 180) && (y >= 20 && y <= 100) {
                *value = true;
            }
        }
        result
    };
    utils::save_boolgrid(&mask, "mask.png");

    // level-set segmentation by Chan-Vese
    let (seg, phi, _) = chanvese(&img, &mask, 500, 1.0, 0);
    utils::save_boolgrid(&seg, "out.png");
    utils::save_floatgrid(&phi, "phi.png");
}

Dependencies

~11–600KB