|
| 1 | +use super::{signals, Bits64}; |
| 2 | +use crate::definitions::Image; |
| 3 | +use image::{imageops, math::Rect, Luma}; |
| 4 | +use std::borrow::Cow; |
| 5 | + |
| 6 | +/// Stores the result of the [`phash`]. |
| 7 | +/// Implements [`Hash`] trait. |
| 8 | +/// |
| 9 | +/// # Note |
| 10 | +/// The hash value may vary between versions. |
| 11 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 12 | +pub struct PHash(Bits64); |
| 13 | + |
| 14 | +impl PHash { |
| 15 | + /// Compute the [hamming distance] between hashes. |
| 16 | + /// |
| 17 | + /// # Example |
| 18 | + /// ```no_run |
| 19 | + /// use imageproc::image_hash; |
| 20 | + /// |
| 21 | + /// # fn main() { |
| 22 | + /// # let img1 = image::open("first.png").unwrap().to_luma32f(); |
| 23 | + /// # let img2 = image::open("second.png").unwrap().to_luma32f(); |
| 24 | + /// let hash1 = image_hash::phash(&img1); |
| 25 | + /// let hash2 = image_hash::phash(&img2); |
| 26 | + /// dbg!(hash1.hamming_distance(hash2)); |
| 27 | + /// # } |
| 28 | + /// ``` |
| 29 | + /// |
| 30 | + /// [hamming distance]: https://en.wikipedia.org/wiki/Hamming_distance |
| 31 | + pub fn hamming_distance(self, PHash(other): PHash) -> u32 { |
| 32 | + self.0.hamming_distance(other) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/// Compute the [pHash] using [DCT]. |
| 37 | +/// |
| 38 | +/// # Example |
| 39 | +/// |
| 40 | +/// ```no_run |
| 41 | +/// use imageproc::image_hash; |
| 42 | +/// |
| 43 | +/// # fn main() { |
| 44 | +/// let img1 = image::open("first.png").unwrap().to_luma32f(); |
| 45 | +/// let img2 = image::open("second.png").unwrap().to_luma32f(); |
| 46 | +/// let hash1 = image_hash::phash(&img1); |
| 47 | +/// let hash2 = image_hash::phash(&img2); |
| 48 | +/// dbg!(hash1.hamming_distance(hash2)); |
| 49 | +/// # } |
| 50 | +/// ``` |
| 51 | +/// |
| 52 | +/// [pHash]: https://phash.org/docs/pubs/thesis_zauner.pdf |
| 53 | +/// [DCT]: https://en.wikipedia.org/wiki/Discrete_cosine_transform |
| 54 | +pub fn phash(img: &Image<Luma<f32>>) -> PHash { |
| 55 | + const N: u32 = 8; |
| 56 | + const HASH_FACTOR: u32 = 4; |
| 57 | + let img = imageops::resize( |
| 58 | + img, |
| 59 | + HASH_FACTOR * N, |
| 60 | + HASH_FACTOR * N, |
| 61 | + imageops::FilterType::Lanczos3, |
| 62 | + ); |
| 63 | + let dct = signals::dct2d(Cow::Owned(img)); |
| 64 | + let topleft = Rect { |
| 65 | + x: 1, |
| 66 | + y: 1, |
| 67 | + width: N, |
| 68 | + height: N, |
| 69 | + }; |
| 70 | + let topleft_dct = crate::compose::crop(&dct, topleft); |
| 71 | + debug_assert_eq!(topleft_dct.dimensions(), (N, N)); |
| 72 | + assert_eq!(topleft_dct.len(), (N * N) as usize); |
| 73 | + let mean = |
| 74 | + topleft_dct.iter().copied().reduce(|a, b| a + b).unwrap() / (topleft_dct.len() as f32); |
| 75 | + let bits = topleft_dct.iter().map(|&x| x > mean); |
| 76 | + PHash(Bits64::new(bits)) |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::*; |
| 82 | + #[test] |
| 83 | + fn test_phash() { |
| 84 | + let img1 = gray_image!(type: f32, |
| 85 | + 1., 2., 3.; |
| 86 | + 4., 5., 6. |
| 87 | + ); |
| 88 | + let mut img2 = img1.clone(); |
| 89 | + *img2.get_pixel_mut(0, 0) = Luma([0f32]); |
| 90 | + let mut img3 = img2.clone(); |
| 91 | + *img3.get_pixel_mut(0, 1) = Luma([0f32]); |
| 92 | + |
| 93 | + let hash1 = phash(&img1); |
| 94 | + let hash2 = phash(&img2); |
| 95 | + let hash3 = phash(&img3); |
| 96 | + |
| 97 | + assert_eq!(0, hash1.hamming_distance(hash1)); |
| 98 | + assert_eq!(0, hash2.hamming_distance(hash2)); |
| 99 | + assert_eq!(0, hash3.hamming_distance(hash3)); |
| 100 | + |
| 101 | + assert_eq!(hash1.hamming_distance(hash2), hash2.hamming_distance(hash1)); |
| 102 | + |
| 103 | + assert!(hash1.hamming_distance(hash2) > 0); |
| 104 | + assert!(hash1.hamming_distance(hash3) > 0); |
| 105 | + assert!(hash2.hamming_distance(hash3) > 0); |
| 106 | + |
| 107 | + assert!(hash1.hamming_distance(hash2) < hash1.hamming_distance(hash3)); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg(not(miri))] |
| 112 | +#[cfg(test)] |
| 113 | +mod proptests { |
| 114 | + use super::*; |
| 115 | + use crate::proptest_utils::arbitrary_image; |
| 116 | + use proptest::prelude::*; |
| 117 | + |
| 118 | + const N: usize = 100; |
| 119 | + |
| 120 | + proptest! { |
| 121 | + #[test] |
| 122 | + fn proptest_phash(img in arbitrary_image(0..N, 0..N)) { |
| 123 | + let hash = phash(&img); |
| 124 | + assert_eq!(0, hash.hamming_distance(hash)); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +#[cfg(not(miri))] |
| 130 | +#[cfg(test)] |
| 131 | +mod benches { |
| 132 | + use super::*; |
| 133 | + use crate::utils::luma32f_bench_image; |
| 134 | + use test::{black_box, Bencher}; |
| 135 | + |
| 136 | + const N: u32 = 600; |
| 137 | + |
| 138 | + #[bench] |
| 139 | + fn bench_phash(b: &mut Bencher) { |
| 140 | + let img = luma32f_bench_image(N, N); |
| 141 | + b.iter(|| { |
| 142 | + let img = black_box(&img); |
| 143 | + black_box(phash(img)); |
| 144 | + }); |
| 145 | + } |
| 146 | +} |
0 commit comments