Skip to content

Commit

Permalink
fixed default values for grayscale_dilate and grayscale_erode + t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
morgane.baizeau committed Apr 29, 2024
1 parent 0afc16e commit 10f6b72
Showing 1 changed file with 60 additions and 4 deletions.
64 changes: 60 additions & 4 deletions src/morphology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,15 +692,15 @@ pub fn grayscale_dilate(image: &GrayImage, mask: &Mask) -> GrayImage {
.apply(image, x, y)
.map(|l| l.0[0])
.max()
.unwrap_or(u8::MAX)])
.unwrap_or(u8::MIN)]) // default is u8::MIN because it's the neutral element of max
});
#[cfg(not(feature = "rayon"))]
let result = GrayImage::from_fn(image.width(), image.height(), |x, y| {
Luma([mask
.apply(image, x, y)
.map(|l| l.0[0])
.max()
.unwrap_or(u8::MAX)])
.unwrap_or(u8::MIN)]) // default is u8::MIN because it's the neutral element of max
});
result
}
Expand Down Expand Up @@ -803,11 +803,19 @@ pub fn grayscale_dilate_mut(image: &mut GrayImage, mask: &Mask) {
pub fn grayscale_erode(image: &GrayImage, mask: &Mask) -> GrayImage {
#[cfg(feature = "rayon")]
let result = GrayImage::from_par_fn(image.width(), image.height(), |x, y| {
Luma([mask.apply(image, x, y).map(|l| l.0[0]).min().unwrap_or(0)])
Luma([mask
.apply(image, x, y)
.map(|l| l.0[0])
.min()
.unwrap_or(u8::MAX)]) // default is u8::MIN because it's the neutral element of max
});
#[cfg(not(feature = "rayon"))]
let result = GrayImage::from_fn(image.width(), image.height(), |x, y| {
Luma([mask.apply(image, x, y).map(|l| l.0[0]).min().unwrap_or(0)])
Luma([mask
.apply(image, x, y)
.map(|l| l.0[0])
.min()
.unwrap_or(u8::MAX)]) // default is u8::MIN because it's the neutral element of max
});
result
}
Expand Down Expand Up @@ -1812,6 +1820,30 @@ mod tests {
assert_eq!(grayscale_dilate(&image, &mask), dilated);
}

#[test]
fn test_grayscale_dilate_default_value() {
let mask = Mask::from_image(&gray_image!(), 0, 0);
let image = gray_image!(
80, 80, 80, 80, 80, 80, 80;
80, 80, 80, 80, 80, 212, 80;
80, 80, 80, 80, 212, 212, 80;
0, 80, 80, 80, 80, 80, 80;
0, 0, 80, 80, 80, 80, 80;
0, 0, 0, 80, 80, 80, 80;
0, 0, 0, 80, 80, 80, 80
);
let dilated = gray_image!(
u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN;
u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN;
u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN;
u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN;
u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN;
u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN;
u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN, u8::MIN
);
assert_eq!(grayscale_dilate(&image, &mask), dilated);
}

#[test]
fn test_grayscale_erode_0() {
let image = gray_image!(
Expand Down Expand Up @@ -1997,6 +2029,30 @@ mod tests {
assert_eq!(grayscale_erode(&image, &mask), eroded);
}

#[test]
fn test_grayscale_erode_default_value() {
let mask = Mask::from_image(&gray_image!(), 0, 0);
let image = gray_image!(
80, 80, 80, 80, 80, 80, 80;
80, 80, 80, 80, 80, 212, 80;
80, 80, 80, 80, 212, 212, 80;
0, 80, 80, 80, 80, 80, 80;
0, 0, 80, 80, 80, 80, 80;
0, 0, 0, 80, 80, 80, 80;
0, 0, 0, 80, 80, 80, 80
);
let dilated = gray_image!(
u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX;
u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX;
u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX;
u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX;
u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX;
u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX;
u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX, u8::MAX
);
assert_eq!(grayscale_erode(&image, &mask), dilated);
}

fn square() -> GrayImage {
GrayImage::from_fn(500, 500, |x, y| {
if min(x, y) > 100 && max(x, y) < 300 {
Expand Down

0 comments on commit 10f6b72

Please sign in to comment.