diff --git a/src/lib.rs b/src/lib.rs index 2b26d05..06bc956 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ extern crate core; pub mod image; pub mod multithreading; +pub mod stdfeats; pub fn add(left: usize, right: usize) -> usize { left + right diff --git a/src/stdfeats/historgram.rs b/src/stdfeats/historgram.rs new file mode 100644 index 0000000..3bbea93 --- /dev/null +++ b/src/stdfeats/historgram.rs @@ -0,0 +1,44 @@ +use std::sync::Arc; + +use serde::{Serialize, Deserialize}; + +use crate::image::Image; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Histogram { + // vector of buckets in the histogram + buckets: Vec<(f32, f32, f32, f32)>, + // count elements in the buckets from the histogram + count: Vec +} + +impl Histogram { + + pub fn map_image(&mut self, image: &Arc>) where T: Default + Copy + From + std::cmp::PartialOrd + Into { + + } + + pub fn cos_sim(&self, histogram: &Histogram) -> Result { + let len: usize = self.count.len(); + if histogram.count.len() != len { + return Err("histograms must have the same length"); + } + + let mut dot = 0.0; + let mut a_sq = 0.0; + let mut b_sq = 0.0; + + let other = &histogram.count; + + for idx in 0..len { + let b = other[idx]; + let a = self.count[idx]; + + dot += a * b; + a_sq += a * a; + b_sq += b * b; + } + + Ok(dot / (a_sq * b_sq).sqrt()) + } +} \ No newline at end of file diff --git a/src/stdfeats/mod.rs b/src/stdfeats/mod.rs new file mode 100644 index 0000000..e98286a --- /dev/null +++ b/src/stdfeats/mod.rs @@ -0,0 +1 @@ +mod historgram; \ No newline at end of file