1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! This crate provides an implementation of Chan-Vese level-sets
//! described in [Active contours without edges](http://ieeexplore.ieee.org/document/902291/)
//! by T. Chan and L. Vese.
//! It is a port of the Python implementation by Kevin Keraudren on
//! [Github](https://github.com/kevin-keraudren/chanvese)
//! and of the Matlab implementation by [Shawn Lankton](http://www.shawnlankton.com).
//!
//! # 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");
//! }
//! ```


extern crate distance_transform;

use distance_transform::dt2d;
use std::f64;

pub use distance_transform::{FloatGrid, BoolGrid};

#[cfg(feature = "image-utils")]
pub mod utils;
#[cfg(feature = "image-utils")]
mod viridis;

/// Runs the chanvese algorithm
/// 
/// Returns the resulting mask (`true` = foreground, `false` = background),
/// the level-set function and the number of iterations.
///
/// # Arguments
///
/// * `img` - the input image
/// * `init_mask` - in initial mask (`true` = foreground, `false` = background)
/// * `max_its` - number of iterations
/// * `alpha` - weight of smoothing term (default: 0.2)
/// * `thresh` - number of different pixels in masks of successive steps (default: 0)
pub fn chanvese(img: &FloatGrid,
            init_mask: &BoolGrid,
            max_its: u32,
            alpha: f64,
            thresh: u32) -> (BoolGrid, FloatGrid, u32) {
    // create a signed distance map (SDF) from mask
    let mut phi = mask2phi(init_mask);

    // main loop
    let mut its = 0u32;
    let mut stop = false;
    let mut prev_mask = init_mask.clone();
    let mut c = 0u32;

    while its < max_its && !stop {
        // get the curve's narrow band
        let idx = {
            let mut result = Vec::new();
            for (x, y, &val) in phi.iter() {
                if val >= -1.2 && val <= 1.2 {
                    result.push((x, y));
                }
            }
            result
        };

        if idx.len() > 0 {
            // intermediate output
            if its % 50 == 0 {
                println!("iteration: {}", its);
            }

            // find interior and exterior mean
            let (upts, vpts) = {
                let mut res1 = Vec::new();
                let mut res2 = Vec::new();
                for (x, y, value) in phi.iter() {
                    if *value <= 0. {
                        res1.push((x, y));
                    }
                    else {
                        res2.push((x, y));
                    }
                }
                (res1, res2)
            };

            let u = upts.iter().fold(0f64, |acc, &(x, y)| {
                acc + *img.get(x, y).unwrap()
            }) / (upts.len() as f64 + f64::EPSILON);
            let v = vpts.iter().fold(0f64, |acc, &(x, y)| {
                acc + *img.get(x, y).unwrap()
            }) / (vpts.len() as f64 + f64::EPSILON);

            // force from image information
            let f: Vec<f64> = idx.iter().map(|&(x, y)| {
                (*img.get(x, y).unwrap() - u)*(*img.get(x, y).unwrap() - u)
                -(*img.get(x, y).unwrap() - v)*(*img.get(x, y).unwrap() - v)
            }).collect();

            // force from curvature penalty
            let curvature = get_curvature(&phi, &idx);

            // gradient descent to minimize energy
            let dphidt: Vec<f64> = {
                let maxabs = f.iter().fold(0.0f64, |acc, &x| {
                    acc.max(x.abs())
                });
                f.iter().zip(curvature.iter()).map(|(f, c)| {
                    f/maxabs + alpha*c
                }).collect()
            };

            // maintain the CFL condition
            let dt = 0.45/(dphidt.iter().fold(0.0f64, |acc, &x| acc.max(x.abs())) + f64::EPSILON);

            // evolve the curve
            for i in 0..idx.len() {
                let (x, y) = idx[i];
                let val = *phi.get(x, y).unwrap();
                phi.set(x, y, val + dt*dphidt[i]);
            }

            // keep SDF smooth
            phi = sussman(&phi, &0.5);

            let new_mask = {
                let mut result = BoolGrid::new(phi.width(), phi.height());
                for (x, y, value) in phi.iter() {
                    result.set(x, y, *value <= 0.);
                }
                result
            };

            c = convergence(&prev_mask, &new_mask, thresh, c);

            if c <= 5 {
                its += 1;
                prev_mask = new_mask.clone();
            }
            else {
                stop = true;
            }
        }
        else {
            break;
        }
    }

    // make mask from SDF, get mask from levelset
    let seg = {
        let mut res = BoolGrid::new(phi.width(), phi.height());
        for (x, y, &value) in phi.iter() {
            res.set(x, y, value <= 0.);
        }
        res
    };

    (seg, phi, its)
}

fn bwdist(a: &BoolGrid) -> FloatGrid {
    let mut res = dt2d(&a);
    for (_, _, value) in res.iter_mut() {
        let newval = value.sqrt();
        *value = newval;
    }
    res
}

// Converts a mask to a SDF
fn mask2phi(init_a: &BoolGrid) -> FloatGrid {
    let inv_init_a = {
        let mut result = init_a.clone();
        for (_, _, value) in result.iter_mut() {
            *value = !*value;
        }
        result
    };

    let phi = {
        let dist_a = bwdist(&init_a);
        let dist_inv_a = bwdist(&inv_init_a);
        let mut result = FloatGrid::new(init_a.width(), init_a.height());
        for (x, y, value) in result.iter_mut() {
            *value = dist_a.get(x, y).unwrap()
                     - dist_inv_a.get(x, y).unwrap()
                     + if *init_a.get(x, y).unwrap() {1.} else {0.}
                     - 0.5;
        }
        result
    };

    phi
}

// Compute curvature along SDF
fn get_curvature(phi: &FloatGrid, idx: &Vec<(usize,usize)>) -> Vec<f64> {
    // get central derivatives of SDF at x,y
    let (phi_x, phi_y, phi_xx, phi_yy, phi_xy) = {
        let (mut res_x, mut res_y, mut res_xx, mut res_yy, mut res_xy)
            : (Vec<f64>, Vec<f64>, Vec<f64>, Vec<f64>, Vec<f64>) = (
            Vec::with_capacity(idx.len()), 
            Vec::with_capacity(idx.len()), 
            Vec::with_capacity(idx.len()), 
            Vec::with_capacity(idx.len()), 
            Vec::with_capacity(idx.len()));

        for &(x, y) in idx.iter() {
            let left = if x > 0 { x - 1 } else { 0 };
            let right = if x < phi.width() - 1 { x + 1 } else { phi.width() - 1 };
            let up = if y > 0 { y - 1 } else { 0 };
            let down = if y < phi.height() - 1 { y + 1 } else { phi.height() - 1 };

            res_x.push(-*phi.get(left, y).unwrap() + *phi.get(right, y).unwrap());
            res_y.push(-*phi.get(x, down).unwrap() + *phi.get(x, up).unwrap());
            res_xx.push(
                  *phi.get(left, y).unwrap()
                - 2.0 * *phi.get(x, y).unwrap()
                + *phi.get(right, y).unwrap());
            res_yy.push(
                  *phi.get(x, up).unwrap()
                - 2.0 * *phi.get(x, y).unwrap()
                + *phi.get(x, down).unwrap());
            res_xy.push(0.25*(
                -*phi.get(left, down).unwrap() - *phi.get(right, up).unwrap()
                +*phi.get(right, down).unwrap() + *phi.get(left, up).unwrap()
            ));
        }
        (res_x, res_y, res_xx, res_yy, res_xy)
    };
    let phi_x2: Vec<f64> = phi_x.iter().map(|x| x*x).collect();
    let phi_y2: Vec<f64> = phi_y.iter().map(|x| x*x).collect();

    // compute curvature (Kappa)
    let curvature: Vec<f64> = (0..idx.len()).map(|i| {
        ((phi_x2[i]*phi_yy[i] + phi_y2[i]*phi_xx[i] - 2.*phi_x[i]*phi_y[i]*phi_xy[i])/
        (phi_x2[i] + phi_y2[i] + f64::EPSILON).powf(1.5))*(phi_x2[i] + phi_y2[i]).powf(0.5)
    }).collect();

    curvature
}

// Level set re-initialization by the sussman method
fn sussman(grid: &FloatGrid, dt: &f64) -> FloatGrid {
    // forward/backward differences
    let (a, b, c, d) = {
        let mut a_res = FloatGrid::new(grid.width(), grid.height());
        let mut b_res = FloatGrid::new(grid.width(), grid.height());
        let mut c_res = FloatGrid::new(grid.width(), grid.height());
        let mut d_res = FloatGrid::new(grid.width(), grid.height());
        for y in 0..grid.height() {
            for x in 0..grid.width() {
                a_res.set(x, y,
                    grid.get(x, y).unwrap()
                    - grid.get((x + grid.width() - 1) % grid.width(), y).unwrap());
                b_res.set(x, y,
                    grid.get((x + 1) % grid.width(), y).unwrap()
                    - grid.get(x, y).unwrap());
                c_res.set(x, y,
                    grid.get(x, y).unwrap()
                    - grid.get(x, (y + 1) % grid.height()).unwrap());
                d_res.set(x, y,
                    grid.get(x, (y + grid.height() - 1) % grid.height()).unwrap()
                    - grid.get(x, y).unwrap());
            }
        }
        (a_res, b_res, c_res, d_res)
    };
    
    let (a_p, a_n, b_p, b_n, c_p, c_n, d_p, d_n) = {
        let mut a_p_res = FloatGrid::new(grid.width(), grid.height());
        let mut a_n_res = FloatGrid::new(grid.width(), grid.height());
        let mut b_p_res = FloatGrid::new(grid.width(), grid.height());
        let mut b_n_res = FloatGrid::new(grid.width(), grid.height());
        let mut c_p_res = FloatGrid::new(grid.width(), grid.height());
        let mut c_n_res = FloatGrid::new(grid.width(), grid.height());
        let mut d_p_res = FloatGrid::new(grid.width(), grid.height());
        let mut d_n_res = FloatGrid::new(grid.width(), grid.height());

        for y in 0..grid.height() {
            for x in 0..grid.width() {
                let a_p_dval = *a.get(x, y).unwrap();
                let a_n_dval = *a.get(x, y).unwrap();
                let b_p_dval = *b.get(x, y).unwrap();
                let b_n_dval = *b.get(x, y).unwrap();
                let c_p_dval = *c.get(x, y).unwrap();
                let c_n_dval = *c.get(x, y).unwrap();
                let d_p_dval = *d.get(x, y).unwrap();
                let d_n_dval = *d.get(x, y).unwrap();

                a_p_res.set(x, y, if a_p_dval >= 0.0 { a_p_dval } else { 0.0 });
                a_n_res.set(x, y, if a_n_dval <= 0.0 { a_n_dval } else { 0.0 });
                b_p_res.set(x, y, if b_p_dval >= 0.0 { b_p_dval } else { 0.0 });
                b_n_res.set(x, y, if b_n_dval <= 0.0 { b_n_dval } else { 0.0 });
                c_p_res.set(x, y, if c_p_dval >= 0.0 { c_p_dval } else { 0.0 });
                c_n_res.set(x, y, if c_n_dval <= 0.0 { c_n_dval } else { 0.0 });
                d_p_res.set(x, y, if d_p_dval >= 0.0 { d_p_dval } else { 0.0 });
                d_n_res.set(x, y, if d_n_dval <= 0.0 { d_n_dval } else { 0.0 });
            }
        }
        (a_p_res, a_n_res, b_p_res, b_n_res, c_p_res, c_n_res, d_p_res, d_n_res)
    };
    
    let mut d_d = FloatGrid::new(grid.width(), grid.height());
    let (d_neg_ind, d_pos_ind) = {
        let mut res = (Vec::new(), Vec::new());
        for (x, y, &value) in grid.iter() {
            if value < 0.0 {
                res.0.push((x, y));
            }
            else if value > 0.0 {
                res.1.push((x,y));
            }
        }
        res
    };

    for index in d_pos_ind {
        let mut ap = *a_p.get(index.0, index.1).unwrap();
        let mut bn = *b_n.get(index.0, index.1).unwrap();
        let mut cp = *c_p.get(index.0, index.1).unwrap();
        let mut dn = *d_n.get(index.0, index.1).unwrap();

        ap *= ap;
        bn *= bn;
        cp *= cp;
        dn *= dn;

        d_d.set(index.0, index.1, (ap.max(bn) + cp.max(dn)).sqrt() - 1.);
    }

    for index in d_neg_ind {
        let mut an = *a_n.get(index.0, index.1).unwrap();
        let mut bp = *b_p.get(index.0, index.1).unwrap();
        let mut cn = *c_n.get(index.0, index.1).unwrap();
        let mut dp = *d_p.get(index.0, index.1).unwrap();

        an *= an;
        bp *= bp;
        cn *= cn;
        dp *= dp;

        d_d.set(index.0, index.1, (an.max(bp) + cn.max(dp)).sqrt() - 1.);
    }

    let ss_d = sussman_sign(&grid);

    let mut res = FloatGrid::new(grid.width(), grid.height());
    for (x, y, value) in res.iter_mut() {
        let dval = grid.get(x, y).unwrap();
        let ss_dval = ss_d.get(x, y).unwrap();
        let d_dval = d_d.get(x, y).unwrap();
        *value = dval - dt*ss_dval*d_dval;
    }

    res
}

fn sussman_sign(d: &FloatGrid) -> FloatGrid {
    let mut res = FloatGrid::new(d.width(), d.height());
    for (x, y, value) in res.iter_mut() {
        let v = d.get(x, y).unwrap();
        *value = v/(v*v + 1.).sqrt();
    }
    res
}

// Convergence test
fn convergence(p_mask: &BoolGrid,
               n_mask: &BoolGrid,
               thresh: u32,
               c: u32) -> u32 {
    let n_diff = p_mask.iter().zip(n_mask.iter()).fold(0u32, |acc, ((_,_,p),(_,_,n))| {
        acc + if *p == *n { 1 } else { 0 }
    });

    if n_diff < thresh {
        c + 1
    }
    else {
        0
    }
}