use image; use plotters::prelude::*; fn main() { let im = image::open("/misc/camera_recorder/ngc6888_o3_20201002_set2_1.png").expect("can load image"); let im = match im { image::DynamicImage::ImageLuma16(ref p) => Some(p), _ => None, }; if let Some(img) = im { let (w, h) = img.dimensions(); println!("got 16-bit greyscale image: {}x{}", w, h); let total = w * h; // let mut histogram = [0u32; 65536]; // for px in img.enumerate_pixels() { // histogram[px.2[0] as usize] += 1; // } let hi = img.enumerate_pixels().map(|x| ((x.2[0]) as f64, if x.2[0] == 0 { 0f64 } else { 1f64 / (total as f64) })); // println!("hist: {:?}", &histogram[..]); fn draw_hist>(hist_data: I, start: usize, end: usize) -> Vec { assert!(start <= end); let mut data = vec![0; 640 * 480 * 3]; { let mut root = BitMapBackend::with_buffer(&mut data, (640, 480)).into_drawing_area(); root.fill(&WHITE); let mut chart = ChartBuilder::on(&root) .margin(5) .caption("Pixel histogram", ("sans-serif", 30)) .set_label_area_size(LabelAreaPosition::Left, 60) .set_label_area_size(LabelAreaPosition::Bottom, 60) .set_label_area_size(LabelAreaPosition::Right, 60) .build_cartesian_2d((start as f64)..(end as f64), 0f64..2.0).unwrap() .set_secondary_coord( ((start as f64)..(end as f64)).step(1.0).use_round().into_segmented(), 0f64..0.05f64, ); chart .configure_mesh() .disable_x_mesh() .disable_y_mesh() .y_desc("Percentage") .draw().unwrap(); chart.configure_secondary_axes().y_desc("Count").draw().unwrap(); let actual = Histogram::vertical(chart.borrow_secondary()) .style(GREEN.filled()) .margin(3) .data(hist_data); chart .draw_secondary_series(actual).unwrap() .label("Observed") .legend(|(x, y)| Rectangle::new([(x, y - 5), (x + 10, y + 5)], GREEN.filled())); chart.configure_series_labels().draw().unwrap(); } data } // let data = draw_hist(&histogram[..], 0, 6000); let data = draw_hist(hi, 0, 6000); use show_image::make_window; let window = make_window("image").expect("can make the window"); let image_info = show_image::ImageInfo::rgb8(640, 480); window.set_image(&(data, image_info), "histogram"); loop { std::thread::sleep(std::time::Duration::from_millis(10000)); } } else { panic!("image was not 16-bit greyscale"); } }