extern crate image;
extern crate imageproc;
use std::env;
use std::path::Path;
use image::{open, ImageBuffer};
use imageproc::hog::*;
fn create_hog_image(input: &Path, signed: bool) {
let image = open(input)
.ok()
.expect(&format!("Could not load image at {:?}", input))
.to_luma();
let opts = HogOptions {
orientations: 8,
signed: signed,
cell_side: 5,
block_side: 2,
block_stride: 1
};
let (width, height) = image.dimensions();
assert!(width >= 10 && height >= 10, "input file must have width and height both >= 10");
let (cropped_width, cropped_height) = (10 * (width / 10), 10 * (height / 10));
let mut cropped = ImageBuffer::new(cropped_width, cropped_height);
for y in 0..cropped_height {
for x in 0..cropped_width {
cropped.put_pixel(x, y, *image.get_pixel(x, y));
}
}
let _ = cropped.save(input.with_file_name("cropped.png")).unwrap();
let spec = HogSpec::from_options(cropped_width, cropped_height, opts).unwrap();
let mut hist = cell_histograms(&cropped, spec);
let star_side = 20;
let hog = render_hist_grid(star_side, &hist.view_mut(), signed);
let output_name = if signed { "hog_signed.png" } else { "hog_unsigned.png" };
let _ = hog.save(input.with_file_name(output_name)).unwrap();
}
fn main() {
let arg = if env::args().count() == 2 {
env::args().nth(1).unwrap()
} else {
panic!("Please enter an input file")
};
let path = Path::new(&arg);
create_hog_image(path, true);
create_hog_image(path, false);
}