Create stats module

This commit is contained in:
cdo committed 2026-11-02 11:16:03 +00:00
1 parent f68fa6f0b7
commit c0372a15cb
3 files changed
+22 -22

No files matched your search

+2 -22
View File
@@ -40,8 +40,8 @@ pub fn lines(
) -> Vec<Line> {
let hough = transform(img);
let mu = mean((&hough.pixels).into_iter().map(|p| *p));
let sigma = stddev((&hough.pixels).into_iter().map(|p| *p), mu);
let mu = stats::mean((&hough.pixels).into_iter().map(|p| *p));
let sigma = stats::stddev((&hough.pixels).into_iter().map(|p| *p), mu);
let mut lines = Vec::<Line>::new();
for y in 0..hough.extent.y {
@@ -65,23 +65,3 @@ pub fn lines(
fn theta_from(x: usize) -> f32 {
x as f32 * std::f32::consts::PI / THETA_N as f32
}
fn mean(xs: impl Iterator<Item = f32>) -> f32 {
let mut n = 0;
let mut sum = 0.0;
for x in xs {
sum += x;
n += 1;
}
sum / n as f32
}
fn stddev(xs: impl Iterator<Item = f32>, mean: f32) -> f32 {
let mut n = 0;
let mut sum = 0.0;
for x in xs {
sum += (x - mean).powf(2.0);
n += 1;
}
(sum / (n - 1) as f32).sqrt()
}
+1
View File
@@ -5,3 +5,4 @@ pub mod filter;
pub mod hough;
pub mod image;
pub mod kernel;
pub mod stats;
+19
View File
@@ -0,0 +1,19 @@
pub fn mean(xs: impl Iterator<Item = f32>) -> f32 {
let mut n = 0;
let mut sum = 0.0;
for x in xs {
sum += x;
n += 1;
}
sum / n as f32
}
pub fn stddev(xs: impl Iterator<Item = f32>, mean: f32) -> f32 {
let mut n = 0;
let mut sum = 0.0;
for x in xs {
sum += (x - mean).powf(2.0);
n += 1;
}
(sum / (n - 1) as f32).sqrt()
}