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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Functions for finding border contours within binary images.

use crate::point::Point;
use image::GrayImage;
use num::{cast, Num, NumCast};
use std::collections::VecDeque;

/// Whether a border of a foreground region borders an enclosing background region or a contained background region.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum BorderType {
    /// A border between a foreground region and the backround region enclosing it.
    /// All points in the border lie within the foreground region.
    Outer,
    /// A border between a foreground region and a background region contained within it.
    /// All points in the border lie within the foreground region.
    Hole,
}

/// A border of an 8-connected foreground region.
#[derive(Debug)]
pub struct Contour<T> {
    /// The points in the border.
    pub points: Vec<Point<T>>,
    /// Whether this is an outer border or a hole border.
    pub border_type: BorderType,
    /// Calls to `find_contours` and `find_contours_with_threshold` return a `Vec` of all borders
    /// in an image. This field provides the index for the parent of the current border in that `Vec`.
    pub parent: Option<usize>,
}

impl<T> Contour<T> {
    /// Construct a contour.
    pub fn new(points: Vec<Point<T>>, border_type: BorderType, parent: Option<usize>) -> Self {
        Contour {
            points,
            border_type,
            parent,
        }
    }
}

/// Finds all borders of foreground regions in an image. All non-zero pixels are
/// treated as belonging to the foreground.
///
/// Based on the algorithm proposed by Suzuki and Abe: Topological Structural
/// Analysis of Digitized Binary Images by Border Following.
pub fn find_contours<T>(image: &GrayImage) -> Vec<Contour<T>>
where
    T: Num + NumCast + Copy + PartialEq + Eq,
{
    find_contours_with_threshold(image, 0)
}

/// Finds all borders of foreground regions in an image. All pixels with intensity strictly greater
/// than `threshold` are treated as belonging to the foreground.
///
/// Based on the algorithm proposed by Suzuki and Abe: Topological Structural
/// Analysis of Digitized Binary Images by Border Following.
pub fn find_contours_with_threshold<T>(image: &GrayImage, threshold: u8) -> Vec<Contour<T>>
where
    T: Num + NumCast + Copy + PartialEq + Eq,
{
    let width = image.width() as usize;
    let height = image.height() as usize;
    let mut image_values = vec![vec![0i32; height]; width];

    for y in 0..height {
        for x in 0..width {
            if image.get_pixel(x as u32, y as u32).0[0] > threshold {
                image_values[x][y] = 1;
            }
        }
    }

    let mut diffs = VecDeque::from(vec![
        Point::new(-1, 0),  // w
        Point::new(-1, -1), // nw
        Point::new(0, -1),  // n
        Point::new(1, -1),  // ne
        Point::new(1, 0),   // e
        Point::new(1, 1),   // se
        Point::new(0, 1),   // s
        Point::new(-1, 1),  // sw
    ]);

    let mut contours: Vec<Contour<T>> = Vec::new();
    let mut curr_border_num = 1;

    for y in 0..height {
        let mut parent_border_num = 1;

        for x in 0..width {
            if image_values[x][y] == 0 {
                continue;
            }

            if let Some((adj, border_type)) =
                if image_values[x][y] == 1 && x > 0 && image_values[x - 1][y] == 0 {
                    Some((Point::new(x - 1, y), BorderType::Outer))
                } else if image_values[x][y] > 0 && x + 1 < width && image_values[x + 1][y] == 0 {
                    if image_values[x][y] > 1 {
                        parent_border_num = image_values[x][y] as usize;
                    }
                    Some((Point::new(x + 1, y), BorderType::Hole))
                } else {
                    None
                }
            {
                curr_border_num += 1;

                let parent = if parent_border_num > 1 {
                    let parent_index = parent_border_num - 2;
                    let parent_contour = &contours[parent_index];
                    if (border_type == BorderType::Outer)
                        ^ (parent_contour.border_type == BorderType::Outer)
                    {
                        Some(parent_index)
                    } else {
                        parent_contour.parent
                    }
                } else {
                    None
                };

                let mut contour_points = Vec::new();
                let curr = Point::new(x, y);
                rotate_to_value(&mut diffs, adj.to_i32() - curr.to_i32());

                if let Some(pos1) = diffs.iter().find_map(|diff| {
                    get_position_if_non_zero_pixel(&image_values, curr.to_i32() + *diff)
                }) {
                    let mut pos2 = pos1;
                    let mut pos3 = curr;

                    loop {
                        contour_points
                            .push(Point::new(cast(pos3.x).unwrap(), cast(pos3.y).unwrap()));
                        rotate_to_value(&mut diffs, pos2.to_i32() - pos3.to_i32());
                        let pos4 = diffs
                            .iter()
                            .rev() // counter-clockwise
                            .find_map(|diff| {
                                get_position_if_non_zero_pixel(&image_values, pos3.to_i32() + *diff)
                            })
                            .unwrap();

                        let mut is_right_edge = false;
                        for diff in diffs.iter().rev() {
                            if *diff == (pos4.to_i32() - pos3.to_i32()) {
                                break;
                            }
                            if *diff == Point::new(1, 0) {
                                is_right_edge = true;
                                break;
                            }
                        }

                        if pos3.x + 1 == width || is_right_edge {
                            image_values[pos3.x][pos3.y] = -curr_border_num;
                        } else if image_values[pos3.x][pos3.y] == 1 {
                            image_values[pos3.x][pos3.y] = curr_border_num;
                        }

                        if pos4 == curr && pos3 == pos1 {
                            break;
                        }

                        pos2 = pos3;
                        pos3 = pos4;
                    }
                } else {
                    contour_points.push(Point::new(cast(x).unwrap(), cast(y).unwrap()));
                    image_values[x][y] = -curr_border_num;
                }
                contours.push(Contour::new(contour_points, border_type, parent));
            }

            if image_values[x][y] != 1 {
                parent_border_num = image_values[x][y].abs() as usize;
            }
        }
    }

    contours
}

fn rotate_to_value<T: Eq + Copy>(values: &mut VecDeque<T>, value: T) {
    let rotate_pos = values.iter().position(|x| *x == value).unwrap();
    values.rotate_left(rotate_pos);
}

fn get_position_if_non_zero_pixel(image: &[Vec<i32>], curr: Point<i32>) -> Option<Point<usize>> {
    let (width, height) = (image.len() as i32, image[0].len() as i32);
    let in_bounds = curr.x > -1 && curr.x < width && curr.y > -1 && curr.y < height;

    if in_bounds && image[curr.x as usize][curr.y as usize] != 0 {
        Some(Point::new(curr.x as usize, curr.y as usize))
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::point::Point;

    // Checks that a contour has the expected border type and parent, and
    // that it contains each of a given set of points.
    fn check_contour<T: Eq>(
        contour: &Contour<T>,
        expected_border_type: BorderType,
        expected_parent: Option<usize>,
        required_points: &[Point<T>],
    ) {
        for point in required_points {
            assert!(contour.points.contains(point));
        }
        assert_eq!(contour.border_type, expected_border_type);
        assert_eq!(contour.parent, expected_parent);
    }

    #[test]
    fn test_contours_structured() {
        use crate::drawing::draw_polygon_mut;
        use image::Luma;

        let white = Luma([255u8]);
        let black = Luma([0u8]);

        let mut image = GrayImage::from_pixel(300, 300, black);
        // border 1 (outer)
        draw_polygon_mut(
            &mut image,
            &[
                Point::new(20, 20),
                Point::new(280, 20),
                Point::new(280, 280),
                Point::new(20, 280),
            ],
            white,
        );
        // border 2 (hole)
        draw_polygon_mut(
            &mut image,
            &[
                Point::new(40, 40),
                Point::new(260, 40),
                Point::new(260, 260),
                Point::new(40, 260),
            ],
            black,
        );
        // border 3 (outer)
        draw_polygon_mut(
            &mut image,
            &[
                Point::new(60, 60),
                Point::new(240, 60),
                Point::new(240, 240),
                Point::new(60, 240),
            ],
            white,
        );
        // border 4 (hole)
        draw_polygon_mut(
            &mut image,
            &[
                Point::new(80, 80),
                Point::new(220, 80),
                Point::new(220, 220),
                Point::new(80, 220),
            ],
            black,
        );
        // rectangle in the corner (outer)
        draw_polygon_mut(
            &mut image,
            &[
                Point::new(290, 290),
                Point::new(300, 290),
                Point::new(300, 300),
                Point::new(290, 300),
            ],
            white,
        );

        let contours = find_contours::<i32>(&image);
        assert_eq!(contours.len(), 5);

        // border 1
        check_contour(
            &contours[0],
            BorderType::Outer,
            None,
            &[
                Point::new(20, 20),
                Point::new(280, 20),
                Point::new(280, 280),
                Point::new(20, 280),
            ],
        );

        // border 2
        check_contour(
            &contours[1],
            BorderType::Hole,
            Some(0),
            &[
                Point::new(39, 40),
                Point::new(261, 40),
                Point::new(261, 260),
                Point::new(39, 260),
            ],
        );

        // border 3
        check_contour(
            &contours[2],
            BorderType::Outer,
            Some(1),
            &[
                Point::new(60, 60),
                Point::new(240, 60),
                Point::new(240, 240),
                Point::new(60, 220),
            ],
        );

        // border 4
        check_contour(
            &contours[3],
            BorderType::Hole,
            Some(2),
            &[
                Point::new(79, 80),
                Point::new(221, 80),
                Point::new(221, 220),
                Point::new(79, 220),
            ],
        );

        // rectangle in the corner
        check_contour(
            &contours[4],
            BorderType::Outer,
            None,
            &[
                Point::new(290, 290),
                Point::new(299, 290),
                Point::new(299, 299),
                Point::new(290, 299),
            ],
        );
    }

    #[test]
    fn find_contours_basic_test() {
        use crate::definitions::HasWhite;
        use crate::drawing::draw_polygon_mut;
        use image::Luma;

        let mut image = GrayImage::new(15, 20);
        draw_polygon_mut(
            &mut image,
            &[Point::new(5, 5), Point::new(11, 5)],
            Luma::white(),
        );

        draw_polygon_mut(
            &mut image,
            &[Point::new(11, 5), Point::new(11, 9)],
            Luma::white(),
        );

        draw_polygon_mut(
            &mut image,
            &[Point::new(11, 9), Point::new(5, 9)],
            Luma::white(),
        );

        draw_polygon_mut(
            &mut image,
            &[Point::new(5, 5), Point::new(5, 9)],
            Luma::white(),
        );

        draw_polygon_mut(
            &mut image,
            &[Point::new(8, 5), Point::new(8, 9)],
            Luma::white(),
        );

        *image.get_pixel_mut(13, 6) = Luma::white();

        let contours = find_contours::<u32>(&image);
        assert_eq!(contours.len(), 4);

        check_contour(
            &contours[0],
            BorderType::Outer,
            None,
            &[
                Point::new(5, 5),
                Point::new(11, 5),
                Point::new(5, 9),
                Point::new(11, 9),
            ],
        );
        assert!(!contours[0].points.contains(&Point::new(13, 6)));

        check_contour(
            &contours[1],
            BorderType::Hole,
            Some(0),
            &[
                Point::new(5, 6),
                Point::new(8, 6),
                Point::new(6, 9),
                Point::new(8, 8),
            ],
        );
        assert!(!contours[1].points.contains(&Point::new(10, 5)));
        assert!(!contours[1].points.contains(&Point::new(10, 9)));
        assert!(!contours[1].points.contains(&Point::new(13, 6)));

        check_contour(
            &contours[2],
            BorderType::Hole,
            Some(0),
            &[
                Point::new(8, 6),
                Point::new(10, 5),
                Point::new(8, 8),
                Point::new(10, 9),
            ],
        );
        assert!(!contours[2].points.contains(&Point::new(6, 9)));
        assert!(!contours[2].points.contains(&Point::new(5, 6)));
        assert!(!contours[2].points.contains(&Point::new(13, 6)));

        assert_eq!(contours[3].border_type, BorderType::Outer);
        assert_eq!(contours[3].points, [Point::new(13, 6)]);
        assert_eq!(contours[3].parent, None);
    }

    #[test]
    fn get_contours_approx_points() {
        use crate::drawing::draw_polygon_mut;
        use image::{GrayImage, Luma};
        let mut image = GrayImage::from_pixel(300, 300, Luma([0]));
        let white = Luma([255]);

        let star = vec![
            Point::new(100, 20),
            Point::new(120, 35),
            Point::new(140, 30),
            Point::new(115, 45),
            Point::new(130, 60),
            Point::new(100, 50),
            Point::new(80, 55),
            Point::new(90, 40),
            Point::new(60, 25),
            Point::new(90, 35),
        ];
        draw_polygon_mut(&mut image, &star, white);
        let contours = find_contours::<u32>(&image);

        let c1_approx = crate::geometry::approximate_polygon_dp(
            &contours[0].points,
            crate::geometry::arc_length(&contours[0].points, true) * 0.01,
            true,
        );
        assert_eq!(
            c1_approx,
            vec![
                Point::new(100, 20),
                Point::new(90, 35),
                Point::new(60, 25),
                Point::new(90, 40),
                Point::new(80, 55),
                Point::new(101, 50),
                Point::new(130, 60),
                Point::new(115, 45),
                Point::new(140, 30),
                Point::new(120, 35)
            ]
        );
    }
}