Changes:
- added `Historgram` struct - added function `cos_sim`
This commit is contained in:
parent
243e94a506
commit
b472d0b773
|
@ -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
|
||||
|
|
|
@ -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<f32>
|
||||
}
|
||||
|
||||
impl Histogram {
|
||||
|
||||
pub fn map_image<T>(&mut self, image: &Arc<Image<T>>) where T: Default + Copy + From<u8> + std::cmp::PartialOrd + Into<f32> {
|
||||
|
||||
}
|
||||
|
||||
pub fn cos_sim(&self, histogram: &Histogram) -> Result<f32, &'static str> {
|
||||
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())
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
mod historgram;
|
Loading…
Reference in New Issue