summaryrefslogtreecommitdiff
path: root/histogrammer/src/main.rs
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2020-10-16 13:03:59 -0700
committeriximeow <me@iximeow.net>2020-10-16 13:03:59 -0700
commitc3f1887b4e9b1a4eaf42b51bcbdbe92e888f96d7 (patch)
tree27c0d64be4e99155da40ae44033b8d26d870e4af /histogrammer/src/main.rs
parent1f014480b45b5a224aa61c4ee99c62905f032bf9 (diff)
add histogrammer
Diffstat (limited to 'histogrammer/src/main.rs')
-rw-r--r--histogrammer/src/main.rs83
1 files changed, 83 insertions, 0 deletions
diff --git a/histogrammer/src/main.rs b/histogrammer/src/main.rs
new file mode 100644
index 0000000..9025787
--- /dev/null
+++ b/histogrammer/src/main.rs
@@ -0,0 +1,83 @@
+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<I: IntoIterator<Item=(f64, f64)>>(hist_data: I, start: usize, end: usize) -> Vec<u8> {
+ 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");
+ }
+}