๐Ÿ“ฆ AlistairKeiller / rustoracer

๐Ÿ“„ skeleton.rs ยท 216 lines
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
216const BACKGROUND_COLOR: u8 = 255;

/// Classification of pixels in an image used for edge thinning.
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum Edge {
    /// The pixel does not contain the foreground color.
    Empty = 0,
    /// The pixel contains the foreground color.
    Filled = 1,
    /// The pixel is not a valid location within the image.
    DoesNotExist,
}

use image::Luma;

/// Struct with information describing the surrounding pixels.
pub struct NeighborInfo {
    /// The number of neighbor pixels with non-background color.
    pub filled: u8,
    /// The number of surrounding pixels.
    pub neighbors: u8,
    /// An array containing the [status](crate::Edge) of the neighboring pixels.
    pub edge_status: [Edge; 8],
}

/// Calculate and return a [`NeighborInfo`](crate::neighbors::NeighborInfo)
/// struct which contains the number of occupied neighbor pixels, number of
/// neighbor pixels, and the [edge status](crate::Edge) of the neighboring
/// pixels.
pub fn get_neighbor_info(
    img: &image::GrayImage,
    width: u32,
    height: u32,
    x: u32,
    y: u32,
) -> NeighborInfo {
    let mut filled = 0;
    let mut neighbors = 0;

    let p9 = if y > 0 && x > 0 {
        neighbors += 1;
        if img.get_pixel(x - 1, y - 1)[0] != BACKGROUND_COLOR {
            filled += 1;
            Edge::Filled
        } else {
            Edge::Empty
        }
    } else {
        Edge::DoesNotExist
    };
    let p2 = if y > 0 {
        neighbors += 1;
        if img.get_pixel(x, y - 1)[0] != BACKGROUND_COLOR {
            filled += 1;
            Edge::Filled
        } else {
            Edge::Empty
        }
    } else {
        Edge::DoesNotExist
    };
    let p3 = if y > 0 && x < u32::MAX && x + 1 < width {
        neighbors += 1;
        if img.get_pixel(x + 1, y - 1)[0] != BACKGROUND_COLOR {
            filled += 1;
            Edge::Filled
        } else {
            Edge::Empty
        }
    } else {
        Edge::DoesNotExist
    };
    let p8 = if x > 0 {
        neighbors += 1;
        if img.get_pixel(x - 1, y)[0] != BACKGROUND_COLOR {
            filled += 1;
            Edge::Filled
        } else {
            Edge::Empty
        }
    } else {
        Edge::DoesNotExist
    };
    let p4 = if x < u32::MAX && x + 1 < width {
        neighbors += 1;
        if img.get_pixel(x + 1, y)[0] != BACKGROUND_COLOR {
            filled += 1;
            Edge::Filled
        } else {
            Edge::Empty
        }
    } else {
        Edge::DoesNotExist
    };
    let p7 = if x > 0 && y < u32::MAX && y + 1 < height {
        neighbors += 1;
        if img.get_pixel(x - 1, y + 1)[0] != BACKGROUND_COLOR {
            filled += 1;
            Edge::Filled
        } else {
            Edge::Empty
        }
    } else {
        Edge::DoesNotExist
    };
    let p6 = if y < u32::MAX && y + 1 < height {
        neighbors += 1;
        if img.get_pixel(x, y + 1)[0] != BACKGROUND_COLOR {
            filled += 1;
            Edge::Filled
        } else {
            Edge::Empty
        }
    } else {
        Edge::DoesNotExist
    };
    let p5 = if x < u32::MAX && x + 1 < width && y < u32::MAX && y + 1 < height {
        neighbors += 1;
        if img.get_pixel(x + 1, y + 1)[0] != BACKGROUND_COLOR {
            filled += 1;
            Edge::Filled
        } else {
            Edge::Empty
        }
    } else {
        Edge::DoesNotExist
    };

    NeighborInfo {
        filled,
        neighbors,
        edge_status: [p2, p3, p4, p5, p6, p7, p8, p9],
    }
}

pub fn thin_image_edges(img_in: &image::GrayImage) -> image::GrayImage {
    let mut img = img_in.clone();
    let mut pixels_to_remove = Vec::new();
    let mut phase_one = true;

    loop {
        // Mark pixels to remove
        for (x, y, p) in img.enumerate_pixels() {
            if p.0[0] == BACKGROUND_COLOR {
                continue;
            }

            let info = get_neighbor_info(&img, img.width(), img.height(), x, y);
            let [p2, p3, p4, p5, p6, p7, p8, p9] = info.edge_status;

            if !(2..=7).contains(&info.filled) || info.neighbors != 8 {
                continue;
            }

            let mut transitions = 0;
            for pair in [p2, p3, p4, p5, p6, p7, p8, p9, p2].windows(2) {
                if pair[0] == Edge::Empty && pair[1] == Edge::Filled {
                    transitions += 1;
                }
            }

            if !(transitions == 1 || transitions == 2) {
                continue;
            }

            if phase_one {
                if transitions == 1 {
                    if (p2 == Edge::Empty || p4 == Edge::Empty || p6 == Edge::Empty)
                        && (p4 == Edge::Empty || p6 == Edge::Empty || p8 == Edge::Empty)
                    {
                        pixels_to_remove.push((x, y));
                    }
                } else {
                    if ((p2 == Edge::Filled && p4 == Edge::Filled)
                        && (p6 == Edge::Empty && p7 == Edge::Empty && p8 == Edge::Empty))
                        || ((p4 == Edge::Filled && p6 == Edge::Filled)
                            && (p2 == Edge::Empty && p8 == Edge::Empty && p9 == Edge::Empty))
                    {
                        pixels_to_remove.push((x, y));
                    }
                }
            } else {
                if transitions == 1 {
                    if (p2 == Edge::Empty || p4 == Edge::Empty || p8 == Edge::Empty)
                        && (p2 == Edge::Empty || p6 == Edge::Empty || p8 == Edge::Empty)
                    {
                        pixels_to_remove.push((x, y));
                    }
                } else {
                    if ((p2 == Edge::Filled && p8 == Edge::Filled)
                        && (p4 == Edge::Empty && p5 == Edge::Empty && p6 == Edge::Empty))
                        || ((p6 == Edge::Filled && p8 == Edge::Filled)
                            && (p2 == Edge::Empty && p3 == Edge::Empty && p4 == Edge::Empty))
                    {
                        pixels_to_remove.push((x, y));
                    }
                }
            }
        }

        phase_one = !phase_one;

        // Replace marked pixels with background color to thin the edges
        for &(x, y) in &pixels_to_remove {
            img.put_pixel(x, y, Luma([BACKGROUND_COLOR]));
        }

        if pixels_to_remove.is_empty() {
            return img;
        }

        pixels_to_remove.clear();
    }
}