smlm_renderer/render_styles/
integral_renderer.rs1use crate::{gaussian, utils, DataType, Properties, RenderPolicy};
2use crate::patch::{PatchFactory, SigmaFactory};
3
4use imp::{ImageMut, OwnedImage};
5
6use locs::{UncertainLocalisation};
7
8fn render_patch<I: ImageMut<Data=DataType>>(localisation: (f64, f64), pixel: (usize, usize), patch: (usize, usize), sigma_nm: f64, mut image: I, properties: &Properties) -> Result<(), String>
9{
10 let (n_rows, n_cols) = image.shape();
11 let (x, y) = localisation;
12 let (row, col) = pixel;
13 let (patch_height, patch_width) = patch;
15 let patch_rows = (patch_height / 2) + 1;
16 let patch_cols = (patch_width / 2) + 1;
17 let image_row_pos = row + patch_rows;
19 let image_row_neg = row.checked_sub(patch_rows);
21 let image_col_pos = col + patch_cols;
23 let image_col_neg = col.checked_sub(patch_cols);
25 let row_start = if image_row_neg.is_some() {image_row_neg.unwrap()} else{0};
28 let row_end = std::cmp::min(image_row_pos, n_rows);
29 let col_start = if image_col_neg.is_some() {image_col_neg.unwrap()} else{0};
30 let col_end = std::cmp::min(image_col_pos, n_cols);
31 for row in row_start..row_end
32 {
33 for col in col_start..col_end
34 {
35 let ((x_lower, x_upper), (y_lower, y_upper)) = utils::pixel_boundaries((row, col), properties.pixel_size());
36 let x_lower = x - x_lower;
37 let x_upper = x_upper - x;
38
39 let y_lower = y - y_lower;
40 let y_upper = y_upper - y;
41 let value = gaussian::integral_over(x_lower, x_upper, y_lower, y_upper, sigma_nm);
43 *image.get_at_mut(row, col) += value;
44 }
45 }
46 Ok(())
47}
48
49#[derive(Debug, Clone)]
50pub struct IntegralPatchRenderer<F>
51{
52 factory: F
53}
54
55impl IntegralPatchRenderer<SigmaFactory>
56{
57 pub fn with_sigma(sigma: f64) -> Self
58 {
59 Self::new(SigmaFactory::new(sigma))
60 }
61}
62
63impl<F> IntegralPatchRenderer<F>
64{
65 pub fn new(factory: F) -> Self
66 {
67 Self{factory}
68 }
69}
70
71impl<F: PatchFactory> IntegralPatchRenderer<F>
72{
73 fn render_as_patch<L: UncertainLocalisation, I: ImageMut<Data=DataType>>(&self, localisation: L, image: I, properties: &Properties) -> Result<(), String>
74 {
75 let col = properties.get_column(localisation.x());
76 let row = properties.get_row(localisation.y());
78 let sigma_nm = localisation.uncertainty();
80 let patch_size = self.factory.get_patch(sigma_nm, &properties)?;
82 let l = (localisation.x(), localisation.y());
84 let pixel = (row, col);
85 let patch = (patch_size.height(), patch_size.width());
86 let _ = render_patch(l, pixel, patch, sigma_nm, image, properties);
88 Ok(())
89 }
90}
91
92impl<F: PatchFactory, L: UncertainLocalisation> RenderPolicy<L> for IntegralPatchRenderer<F>
93{
94 type Output = ();
95 type Error = String;
96 fn render(&self, localisation: L, image: &mut OwnedImage<DataType>, properties: &Properties) -> Result<Self::Output, Self::Error>
97 {
98 self.render_as_patch(localisation, image, properties)
99 }
100}
101
102#[cfg(test)]
103mod tests
104{
105 }