-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathimage_processing.rs
More file actions
60 lines (50 loc) · 2.25 KB
/
Copy pathimage_processing.rs
File metadata and controls
60 lines (50 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! Example: Image processing pipeline.
//!
//! Demonstrates common imgproc operations:
//! 1. Load image
//! 2. Convert to grayscale
//! 3. Apply Gaussian blur
//! 4. Detect edges with Canny
//! 5. Apply threshold
//! 6. Save results
//!
//! Usage: cargo run --example image_processing -- <image_path> <output_dir>
use std::path::Path;
use yscv_imgproc::{canny, gaussian_blur_3x3, imread, imwrite, rgb_to_grayscale, threshold_binary};
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 3 {
eprintln!("Usage: image_processing <input_image> <output_dir>");
eprintln!();
eprintln!("Applies a chain of image processing operations and saves");
eprintln!("intermediate results to the output directory.");
eprintln!();
eprintln!("Example:");
eprintln!(" cargo run --example image_processing -- photo.jpg ./output/");
std::process::exit(1);
}
let input_path = Path::new(&args[1]);
let output_dir = Path::new(&args[2]);
std::fs::create_dir_all(output_dir).expect("Failed to create output directory");
// Step 1: Load as RGB.
println!("Loading: {}", input_path.display());
let rgb = imread(input_path).expect("Failed to load image");
println!(" Shape: {:?}", rgb.shape());
// Step 2: Convert to grayscale.
let gray = rgb_to_grayscale(&rgb).expect("Grayscale conversion failed");
println!(" Grayscale: {:?}", gray.shape());
imwrite(output_dir.join("01_grayscale.png"), &gray).expect("Save failed");
// Step 3: Gaussian blur.
let blurred = gaussian_blur_3x3(&gray).expect("Blur failed");
println!(" Blurred: {:?}", blurred.shape());
imwrite(output_dir.join("02_blurred.png"), &blurred).expect("Save failed");
// Step 4: Canny edge detection.
let edges = canny(&blurred, 0.1, 0.3).expect("Canny failed");
println!(" Edges: {:?}", edges.shape());
imwrite(output_dir.join("03_edges.png"), &edges).expect("Save failed");
// Step 5: Binary threshold.
let binary = threshold_binary(&gray, 0.5, 1.0).expect("Threshold failed");
println!(" Binary: {:?}", binary.shape());
imwrite(output_dir.join("04_binary.png"), &binary).expect("Save failed");
println!("\nResults saved to: {}", output_dir.display());
}