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
// //! These are operations that can be conceptually performed on GrayImages which serve as masks
// //!
//
// use crate::image::{GrayImage, Luma};
// use imageproc::distance_transform::Norm;
// use imageproc::morphology::{dilate_mut, erode_mut};
//
// pub trait MaskValue {
// fn set_masked(&mut self);
// fn set_unmasked(&mut self);
// fn is_masked(&self) -> bool;
// fn is_unmasked(&self) -> bool {
// !self.is_masked()
// }
// }
//
// impl MaskValue for Luma<u8> {
// fn set_masked(&mut self) {
// self[0] = 255;
// }
//
// fn set_unmasked(&mut self) {
// self[0] = 0;
// }
//
// fn is_masked(&self) -> bool {
// self[0] == 255
// }
// }
//
// pub trait MaskOperations {
// /// Returns an inverted version of the image mask
// fn inverted(&self) -> Self;
//
// /// Returns a mask which is all the pixels in the mask which are part of an unmasked region
// /// which touches the edge of the image. This is useful for finding the exterior of a mask.
// /// For example, if a mask consists of a circle in the center of the image with black holes
// /// in it, this method will return a white image with a solid black circle in the center.
// fn get_unmasked_exterior(&self) -> Self;
//
// /// Return a mask which is the difference of the current mask with positive pixels in the
// /// other mask removed.
// fn subtract(&self, other: &Self) -> Self;
//
// /// Return a mask which is the union of the current mask with the other mask.
// fn union(&self, other: &Self) -> Self;
//
// /// Return a mask which is the intersection of the current mask with the other mask.
// /// This is the same as the logical AND of the two masks.
// fn intersection(&self, other: &Self) -> Self;
//
// /// Returns true if the pixel at the given coordinates exists AND is masked, otherwise false.
// /// This is NOT the same as !is_pixel_unmasked(x, y) because both will return false if the pixel
// /// does not exist.
// fn is_pixel_masked(&self, x: i32, y: i32) -> bool;
//
// /// Returns true if the pixel at the given coordinates exists AND is unmasked, otherwise false.
// /// This is NOT the same as !is_pixel_masked(x, y) because both will return false if the pixel
// /// does not exist.
// fn is_pixel_unmasked(&self, x: i32, y: i32) -> bool;
//
// /// Set a pixel to be masked (i.e. set to 255)
// fn set_masked(&mut self, x: u32, y: u32);
//
// /// Set a pixel to be unmasked (i.e. set to 0)
// fn set_unmasked(&mut self, x: u32, y: u32);
//
// /// Erode the mask with alternating L1 and LInf norms
// fn erode_alternating(&mut self, count: usize);
//
// /// Dilate the mask with alternating L1 and LInf norms
// fn dilate_alternating(&mut self, count: usize);
//
// fn eroded_alternating(&self, count: usize) -> Self;
// fn dilated_alternating(&self, count: usize) -> Self;
//
// fn invert(&mut self);
// }
//
// impl MaskOperations for GrayImage {
// fn inverted(&self) -> GrayImage {
// let mut inverted = self.clone();
// for (_, _, v) in inverted.enumerate_pixels_mut() {
// if v.is_masked() {
// v.set_unmasked();
// } else {
// v.set_masked();
// }
// }
// inverted
// }
//
// fn get_unmasked_exterior(&self) -> GrayImage {
// let mut output = GrayImage::new(self.width(), self.height());
// let mut stack = Vec::new();
//
// // Push any top and bottom row pixels which are unmasked onto the stack
// for i in 0..self.width() {
// if self.get_pixel(i, 0).is_unmasked() {
// stack.push((i, 0));
// output.set_masked(i, 0);
// }
// if self.get_pixel(i, self.height() - 1).is_unmasked() {
// stack.push((i, self.height() - 1));
// output.set_masked(i, self.height() - 1);
// }
// }
//
// // Push any left or right column pixels which are unmasked onto the stack
// for i in 0..self.height() {
// if self.get_pixel(0, i).is_unmasked() {
// stack.push((0, i));
// output.set_masked(0, i);
// }
// if self.get_pixel(self.width() - 1, i).is_unmasked() {
// stack.push((self.width() - 1, i));
// output.set_masked(self.width() - 1, i);
// }
// }
//
// // Now, for each pixel in the stack, set the output pixel to unmasked and push any
// // unmasked neighbors onto the stack
// while let Some((x, y)) = stack.pop() {
// for (xn, yn) in &[
// (x as i32 - 1, y as i32),
// (x as i32 + 1, y as i32),
// (x as i32, y as i32 - 1),
// (x as i32, y as i32 + 1),
// ] {
// if output.is_pixel_unmasked(*xn, *yn)
// && self.get_pixel(*xn as u32, *yn as u32).is_unmasked()
// {
// stack.push((*xn as u32, *yn as u32));
// output.set_masked(*xn as u32, *yn as u32);
// }
// }
// }
//
// output
// }
//
// fn subtract(&self, other: &Self) -> Self {
// let mut output = self.clone();
// for (x, y, v) in output.enumerate_pixels_mut() {
// if other.get_pixel(x, y)[0] == 255 {
// *v = Luma([0]);
// }
// }
// output
// }
//
// fn union(&self, other: &Self) -> Self {
// let mut output = self.clone();
// for (x, y, v) in output.enumerate_pixels_mut() {
// if other.get_pixel(x, y)[0] == 255 {
// *v = Luma([255]);
// }
// }
// output
// }
//
// fn intersection(&self, other: &Self) -> Self {
// let mut output = GrayImage::new(self.width(), self.height());
// for (x, y, v) in output.enumerate_pixels_mut() {
// if self.get_pixel(x, y)[0] == 255 && other.get_pixel(x, y)[0] == 255 {
// *v = Luma([255]);
// }
// }
// output
// }
//
// /// Returns true if the pixel at the given coordinates exists AND is masked, otherwise false.
// /// Masked means that the pixel value is 255, and conceptually it represents a real pixel with
// /// a known value.
// fn is_pixel_masked(&self, x: i32, y: i32) -> bool {
// if x < 0 || x >= self.width() as i32 || y < 0 || y >= self.height() as i32 {
// false
// } else {
// self.get_pixel(x as u32, y as u32).is_masked()
// }
// }
//
// /// Returns true if the pixel at the given coordinates exists AND is unmasked, otherwise false.
// /// Unmasked means that the pixel value is 0, and conceptually it represents a pixel that is
// /// not part of the dataset (similar to null or NaN).
// fn is_pixel_unmasked(&self, x: i32, y: i32) -> bool {
// if x < 0 || x >= self.width() as i32 || y < 0 || y >= self.height() as i32 {
// false
// } else {
// self.get_pixel(x as u32, y as u32).is_unmasked()
// }
// }
//
// fn set_masked(&mut self, x: u32, y: u32) {
// self.get_pixel_mut(x, y).set_masked();
// }
//
// fn set_unmasked(&mut self, x: u32, y: u32) {
// self.get_pixel_mut(x, y).set_unmasked();
// }
//
// fn erode_alternating(&mut self, count: usize) {
// for i in 0..count {
// if i % 2 == 0 {
// erode_mut(self, Norm::L1, 1);
// } else {
// erode_mut(self, Norm::LInf, 1);
// }
// }
// }
//
// fn dilate_alternating(&mut self, count: usize) {
// for i in 0..count {
// if i % 2 == 0 {
// dilate_mut(self, Norm::L1, 1);
// } else {
// dilate_mut(self, Norm::LInf, 1);
// }
// }
// }
//
// fn eroded_alternating(&self, count: usize) -> Self {
// let mut working = self.clone();
// working.erode_alternating(count);
// working
// }
//
// fn dilated_alternating(&self, count: usize) -> Self {
// let mut working = self.clone();
// working.dilate_alternating(count);
// working
// }
//
// fn invert(&mut self) {
// for (_, _, v) in self.enumerate_pixels_mut() {
// if v.is_masked() {
// v.set_unmasked();
// } else {
// v.set_masked();
// }
// }
// }
// }