Struct fast_dhash::Dhash
source · pub struct Dhash { /* private fields */ }
Expand description
Dhash
basic usage
use fast_dhash::Dhash;
use image;
use std::path::Path;
fn main() {
let path = Path::new("../image.jpg");
let image = image::open(path);
if let Ok(image) = image {
let hash = Dhash::new(&image);
println!("hash: {}", hash);
// hash: d6a288ac6d5cce14
}
}
comparaison
Dhash implements PartialEq
instead of Eq
to match
cases where the compared hashes represent a variation
of the same image.
to check the equality use
hash.hamming_distance(&other_hash) == 0
example
use fast_dhash::Dhash;
fn main() {
let hash = Dhash::from_str("d6a288ac6d5cce14").unwrap();
let similar = Dhash::from_str("d6a088ac6d5cce14").unwrap();
let different = Dhash::from_str("a63ebccdfd5dbfff").unwrap();
assert!(hash == similar);
assert!(hash.hamming_distance(&similar) == 1);
assert!(hash != different);
assert!(hash.hamming_distance(&different) == 26);
}
Implementations§
source§impl Dhash
impl Dhash
sourcepub fn new(image: &DynamicImage) -> Self
pub fn new(image: &DynamicImage) -> Self
dhash algorithm
generates a new dhash from a DynamicImage
paincs
panics with images bigger than 34,952 x 34,952 (~1.2 billion pixels)
sourcepub fn from_string(hash_hex: &String) -> Result<Self, ParseIntError>
pub fn from_string(hash_hex: &String) -> Result<Self, ParseIntError>
Dhash from String
converts an hex String
to Dhash
example
use fast_dhash::Dhash;
fn main() {
let string = "d6a288ac6d5cce14".to_string();
let hash = Dhash::from_string(&string).unwrap();
let hash_u64 = hash.to_u64();
assert_eq!(hash_u64, 15466074344494255636u64);
}
sourcepub fn from_str(hash_hex: &str) -> Result<Self, ParseIntError>
pub fn from_str(hash_hex: &str) -> Result<Self, ParseIntError>
Dhash from str
converts an hex str
to Dhash
example
use fast_dhash::Dhash;
fn main() {
let hash = Dhash::from_str("d6a288ac6d5cce14").unwrap();
let hash_u64 = hash.to_u64();
assert_eq!(hash_u64, 15466074344494255636u64);
}
sourcepub fn from_u64(hash: u64) -> Self
pub fn from_u64(hash: u64) -> Self
Dhash from u64 hash
parse a u64
sourcepub fn hamming_distance(&self, other: &Self) -> u32
pub fn hamming_distance(&self, other: &Self) -> u32
dhash hamming distance
compares two hashes and returns a value between 0
and 64
0
identical
1~10
variation
>10
different
Trait Implementations§
source§impl<'de> Deserialize<'de> for Dhash
impl<'de> Deserialize<'de> for Dhash
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more