use image::{open, Rgb};
use imageproc::edges::canny;
use imageproc::hough::{detect_lines, draw_polar_lines, LineDetectionOptions, PolarLine};
use imageproc::map::map_colors;
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")
}
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_luma8();
let gray_path = output_dir.join("grey.png");
input_image.save(&gray_path).unwrap();
let edges = canny(&input_image, 50.0, 100.0);
let canny_path = output_dir.join("canny.png");
edges.save(&canny_path).unwrap();
let options = LineDetectionOptions {
vote_threshold: 40,
suppression_radius: 8,
};
let lines: Vec<PolarLine> = detect_lines(&edges, options);
let white = Rgb::<u8>([255, 255, 255]);
let green = Rgb::<u8>([0, 255, 0]);
let black = Rgb::<u8>([0, 0, 0]);
let color_edges = map_colors(&edges, |p| if p[0] > 0 { white } else { black });
let lines_image = draw_polar_lines(&color_edges, &lines, green);
let lines_path = output_dir.join("lines.png");
lines_image.save(&lines_path).unwrap();
}