use image::{open, GrayImage, Luma, Pixel};
use imageproc::definitions::Clamp;
use imageproc::gradients::sobel_gradient_map;
use imageproc::map::map_colors;
use imageproc::seam_carving::*;
use std::env;
use std::fs;
use std::path::Path;
fn main() {
if env::args().len() != 3 {
panic!(
"Please enter an input file and a target directory. For example:
cargo run --release --example seam_carving PATH_TO_IMAGE temp"
);
}
let input_path = env::args().nth(1).unwrap();
let output_dir = env::args().nth(2).unwrap();
let input_path = Path::new(&input_path);
let output_dir = Path::new(&output_dir);
if !output_dir.is_dir() {
fs::create_dir(output_dir).expect("Failed to create output directory")
}
if !input_path.is_file() {
panic!("Input file does not exist");
}
let input_image = open(input_path)
.expect(&format!("Could not load image at {:?}", input_path))
.to_rgb();
let original_path = output_dir.join("original.png");
input_image.save(&original_path).unwrap();
let seams_to_remove = 300;
let mut shrunk = input_image.clone();
let mut seams = Vec::new();
for i in 0..seams_to_remove {
println!("Removing seam {}", i);
let vertical_seam = find_vertical_seam(&shrunk);
shrunk = remove_vertical_seam(&mut shrunk, &vertical_seam);
seams.push(vertical_seam);
}
let gray_image = map_colors(&input_image, |p| p.to_luma());
let annotated = draw_vertical_seams(&gray_image, &seams);
let annotated_path = output_dir.join("annotated.png");
annotated.save(&annotated_path).unwrap();
let gradients = sobel_gradient_map(&input_image, |p| {
let mean = (p[0] + p[1] + p[2]) / 3;
Luma([mean as u32])
});
let clamped_gradients: GrayImage = map_colors(&gradients, |p| Luma([Clamp::clamp(p[0])]));
let annotated_gradients = draw_vertical_seams(&clamped_gradients, &seams);
let gradients_path = output_dir.join("gradients.png");
clamped_gradients.save(&gradients_path).unwrap();
let annotated_gradients_path = output_dir.join("annotated_gradients.png");
annotated_gradients.save(&annotated_gradients_path).unwrap();
let shrunk_path = output_dir.join("shrunk.png");
shrunk.save(&shrunk_path).unwrap();
}