refactors and new dct binary

This commit is contained in:
2024-05-17 13:43:18 +02:00
parent 8c3112f9f2
commit 1b2536f1e0
7 changed files with 196 additions and 36 deletions
+66
View File
@@ -0,0 +1,66 @@
use std::env;
use std::f64::consts::{PI, SQRT_2};
use image::{save_buffer, GenericImageView, GrayImage};
fn dct(img: &image::DynamicImage) -> GrayImage {
//let mut dct_values: = [0.0; img.width() * img.height()];
let w = img.width();
let h = img.height();
let mut dct_img = GrayImage::new(w, h);
//let mut dct_values: Vec<f64> = Vec::new();
for u in 0..w {
for v in 0..h {
let w = w as f64;
let h = h as f64;
//let k = (v*img.width())+u;
let mut alpha = 0.25;
if u == 0 {
alpha = alpha / SQRT_2
}
if v == 0 {
alpha = alpha / SQRT_2
}
let v: f64 = v as f64;
let u: f64 = u as f64;
let mut sum: f64 = 0.0;
for (x, y, pix) in img.pixels() {
let x: f64 = x as f64;
let y: f64 = y as f64;
let pixel = pix[0] as f64;
// sum +=
// pixel *
// (x*u*PI/16.0).cos() *
// (y*v*PI/16.0).cos()
sum +=
pixel *
(
PI/w*
(x+0.5)*u
).cos()
*
(
PI/h*
(y+0.5)*v
).cos()
}
//dct_values[k] = alpha * sum;
//dct_values.push(alpha * sum);
print!("{sum}\t");
//sum = (sum + 200000.0)/784.0;
print!("{}\n", sum * alpha);
let pixel = (sum).abs() as u8;
println!("{u}, {v}:\t {sum}");
dct_img.put_pixel(u as u32, v as u32, image::Luma([pixel]));
}
}
dct_img
}
fn main() {
let args: Vec<String> = env::args().collect();
let path = args[1].clone();
let img = image::open(&path).unwrap();
let dct = dct(&img);
dct.save("dct.png").unwrap();
}
+6 -18
View File
@@ -1,34 +1,22 @@
//use image_similarity::descriptors::dct::DCT;
use image_similarity::descriptors::{DCT, Descriptor};
use std::env;
use std::path::PathBuf;
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
use log::debug;
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cfg {
path: String
}
impl Cfg{
fn load(args: &[String]) -> Result<Cfg, &'static str> {
if args.len() < 2 {
return Err("Filename argument required");
}
let path = args[1].clone();
Ok(Cfg { path })
}
path: PathBuf
}
fn main() {
env_logger::init();
let cfg = Cfg::parse();
//Init all descriptors:
let desc = DCT::new().with_quality(50);
let args: Vec<String> = env::args().collect();
let cfg = Cfg::load(&args)
.expect("Error loading config");
let img = image::open(cfg.path)
.expect("Unable to open file");
let phash: u64 = desc.describe(&img);
-1
View File
@@ -3,7 +3,6 @@ use image_similarity::descriptors::{DCT, Descriptor};
use image_similarity::store::DescriptorStore;
use std::env;
use log::{info, debug, error};
use viuer::{Config, print};
struct Cfg {
path: String
+50 -8
View File
@@ -1,17 +1,59 @@
use image_similarity::store::DescriptorStore;
use image_similarity::descriptors::DCT;
use image_similarity::descriptors::{Descriptor, DCT};
use log::info;
use std::path::PathBuf;
use std::thread;
use clap::Parser;
fn main() {
env_logger::init();
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cfg {
/// Path that contains the input images. Will not traverse directories.
path: PathBuf
}
fn make_store<T: Descriptor>(descriptor: T, input_path: PathBuf, save_path: PathBuf) -> () {
let mut store =
DescriptorStore::new()
.with_file("store.messagepack");
.with_file(save_path);
info!("{}", store);
let desc =
DCT::new()
.with_quality(30);
store.insert_directory("data", desc);
store.insert_directory(input_path, descriptor);
info!("{}", store);
store.save().expect("Error saving");
}
fn main() {
env_logger::init();
let cfg = Cfg::parse();
let mut threads = vec![];
let dct_50 =
DCT::new()
.with_quality(50);
let dct_30 =
DCT::new()
.with_quality(30);
let dct_10 =
DCT::new()
.with_quality(10);
threads.push(
thread::spawn(move || {
make_store(dct_50, "data".into(), "dct50.messagepack".into());
})
);
threads.push(
thread::spawn(move || {
make_store(dct_30, "data".into(), "dct30.messagepack".into());
})
);
threads.push(
thread::spawn(move || {
make_store(dct_10, "data".into(), "dct10.messagepack".into());
})
);
for thread in threads {
let _ = thread.join();
}
}
+2 -2
View File
@@ -8,7 +8,6 @@ use std::path::Path;
use std::fmt;
use log::{info, debug, error};
use bktree::*;
use viuer::{Config, print};
#[derive(Debug)]
pub enum SaveError {
@@ -66,6 +65,7 @@ impl DescriptorStore {
/// Inserts a single value into the store
pub fn insert(&mut self, key: u64, value: String) {
self.map.insert(key, value);
self.bktree.insert(key);
}
/// Calculates all descriptions with a given descriptor
@@ -112,7 +112,7 @@ impl DescriptorStore {
match name {
Some(n) => {
println!("{element:b}: {n} (distance: {distance})");
if x+16 >= term_width {
if x+16 >= term_width {
x = 0;
y += 8;
}