image_processing/filters/
morphological.rs

1use image::{DynamicImage, ImageBuffer, GenericImageView};
2use crate::utils::image_utils;
3
4#[allow(dead_code)]
5pub fn erosion<T: std::cmp::PartialEq<image::Rgba<u8>>, const M: usize, const N: usize>(gabarit: &[[T; N]; M], img : DynamicImage) -> DynamicImage
6{
7    let gabarit_taille = gabarit.len() as u32;
8    let gabarit_centre = gabarit_taille / 2;
9
10    let (width, height) = img.dimensions();
11    let mut img_erodee: image::RgbaImage = ImageBuffer::new(width, height); // WIDTH, HEIGHT
12
13    for y in gabarit_centre..height-gabarit_centre
14    {
15        for x in gabarit_centre..width-gabarit_centre
16        {
17            let mut match_gabarit = true;
18            // Vérification des pixels voisins avec le gabarit
19            for ky in 0..gabarit_taille {
20                for kx in 0..gabarit_taille {
21
22                    // mise à jours des coordonnées des pixels de l'image
23                    let nx = x + kx - gabarit_centre;
24                    let ny = y + ky - gabarit_centre;
25
26                    // récupération du pixel de l'image équivalent au gabarit
27                    let pixel = img.get_pixel(nx, ny);
28
29                    // tous les pixels objets du gabarit doivent treouver leurs équivalent dans les pixels voisins du pixel courant.
30                    if pixel == image_utils::WHITEPIXEL && gabarit[ky as usize][kx as usize] == image_utils::BLACKPIXEL
31                    {
32                        match_gabarit = false;
33                        break;
34                    }
35                }
36            }
37            // Application du pixel érodé
38            if match_gabarit // tous les pixels objets du gabarit ont trouvé leurs équivalent.
39            {
40                img_erodee.put_pixel(x, y, image_utils::BLACKPIXEL);
41            } else // au moins un pixel objet du gabarit n'a pas trouvé d'équivalent.
42            {
43                img_erodee.put_pixel(x, y, image_utils::WHITEPIXEL);
44            }
45        }
46    }
47    return image::DynamicImage::ImageRgba8(img_erodee);
48}
49
50#[allow(dead_code)]
51pub fn dilatation<T: std::cmp::PartialEq<image::Rgba<u8>>, const M: usize, const N: usize>(gabarit: &[[T; N]; M], img : DynamicImage) -> DynamicImage
52{
53    let gabarit_taille = gabarit.len() as u32;
54    let gabarit_centre = gabarit_taille / 2;
55
56    let (width, height) = img.dimensions();
57    let mut img_dilatee: image::RgbaImage = ImageBuffer::new(width, height); // WIDTH, HEIGHT
58
59    for y in gabarit_centre..height-gabarit_centre
60    {
61        for x in gabarit_centre..width-gabarit_centre
62        {
63            let mut match_gabarit = false;
64            // Vérification des pixels voisins avec le gabarit
65            for ky in 0..gabarit_taille
66            {
67                for kx in 0..gabarit_taille
68                {
69                    // mise à jours des coordonnées des pixels de l'image
70                    let nx = x + kx - gabarit_centre;
71                    let ny = y + ky - gabarit_centre;
72
73                    // récupération du pixel de l'image équivalent au gabarit
74                    let pixel = img.get_pixel(nx, ny);
75
76                    // Si au moins un pixel objet du gabarit correspond à un pixel objet du pixel courant et de son voisinage alors, le pixel central devient un pixel objet.
77                    if pixel == image_utils::BLACKPIXEL && gabarit[ky as usize][kx as usize] == image_utils::BLACKPIXEL
78                    {
79                        match_gabarit = true;
80                        break;
81                    }
82                }
83            }
84            // Application du pixel érodé
85            if match_gabarit // au moins un pixel objet du gabarit à trouvé son équivalent
86            {
87                img_dilatee.put_pixel(x, y, image_utils::BLACKPIXEL);
88            } else // aucun pixel objet du gabarit n'a trouvé d'équivalent
89            {
90                img_dilatee.put_pixel(x, y, image_utils::WHITEPIXEL);
91            }
92        }
93    }
94    return image::DynamicImage::ImageRgba8(img_dilatee);
95}