Skip to content

Commit

Permalink
Remove noisy ImageBuffer type (#636)
Browse files Browse the repository at this point in the history
* refactored `ImageBuffer` type to `Image` and `GrayImage` type synonyms
  • Loading branch information
ripytide authored May 23, 2024
1 parent 167efd0 commit 611ed99
Show file tree
Hide file tree
Showing 26 changed files with 141 additions and 142 deletions.
8 changes: 4 additions & 4 deletions examples/blend.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Demonstrates the current incorrect handling of gamma for RGB images
use image::{ImageBuffer, Rgb};
use imageproc::pixelops::interpolate;
use image::Rgb;
use imageproc::{definitions::Image, pixelops::interpolate};

fn main() {
let red = Rgb::<u8>([255, 0, 0]);
Expand All @@ -12,7 +12,7 @@ fn main() {

let naive_blend = |x| interpolate(red, green, left_weight(x));

let mut naive_image = ImageBuffer::new(800, 400);
let mut naive_image = Image::new(800, 400);
for y in 0..naive_image.height() {
for x in 0..naive_image.width() {
naive_image.put_pixel(x, y, naive_blend(x));
Expand All @@ -39,7 +39,7 @@ fn main() {
])
};

let mut gamma_image = ImageBuffer::new(800, 400);
let mut gamma_image = Image::new(800, 400);
for y in 0..gamma_image.height() {
for x in 0..gamma_image.width() {
gamma_image.put_pixel(x, y, gamma_blend(x));
Expand Down
5 changes: 3 additions & 2 deletions examples/hog.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Demonstrates computing and visualising HoG gradients.
use image::{open, ImageBuffer};
use image::open;
use imageproc::definitions::Image;
use imageproc::hog::*;
use std::env;
use std::path::Path;
Expand Down Expand Up @@ -32,7 +33,7 @@ fn create_hog_image(input: &Path, signed: bool) {

// Crop image to a suitable size
let (cropped_width, cropped_height) = (10 * (width / 10), 10 * (height / 10));
let mut cropped = ImageBuffer::new(cropped_width, cropped_height);
let mut cropped = Image::new(cropped_width, cropped_height);
for y in 0..cropped_height {
for x in 0..cropped_width {
cropped.put_pixel(x, y, *image.get_pixel(x, y));
Expand Down
15 changes: 5 additions & 10 deletions src/binary_descriptors/brief.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//! described in [Calonder, et. al. (2010)].
///
/// [Calonder, et. al. (2010)]: https://www.cs.ubc.ca/~lowe/525/papers/calonder_eccv10.pdf
use image::{GenericImageView, GrayImage, ImageBuffer, Luma};
use image::{GenericImageView, GrayImage, Luma};
use rand_distr::{Distribution, Normal};

use crate::{corners::Corner, integral_image::integral_image, point::Point};
use crate::{corners::Corner, definitions::Image, integral_image::integral_image, point::Point};

use super::{
constants::{BRIEF_PATCH_DIAMETER, BRIEF_PATCH_RADIUS},
Expand Down Expand Up @@ -67,12 +67,7 @@ pub struct TestPair {
pub p1: Point<u32>,
}

fn local_pixel_average(
integral_image: &ImageBuffer<Luma<u32>, Vec<u32>>,
x: u32,
y: u32,
radius: u32,
) -> u8 {
fn local_pixel_average(integral_image: &Image<Luma<u32>>, x: u32, y: u32, radius: u32) -> u8 {
if radius == 0 {
return 0;
}
Expand Down Expand Up @@ -114,7 +109,7 @@ fn local_pixel_average(
}

pub(crate) fn brief_impl(
integral_image: &ImageBuffer<Luma<u32>, Vec<u32>>,
integral_image: &Image<Luma<u32>>,
keypoints: &[Point<u32>],
test_pairs: &[TestPair],
length: usize,
Expand Down Expand Up @@ -318,7 +313,7 @@ mod tests {
5, 237, 254, 171, 172, 165, 50, 39;
92, 31, 238, 88, 44, 67, 140, 255
);
let integral_image: ImageBuffer<Luma<u32>, Vec<u32>> = integral_image(&image);
let integral_image: Image<Luma<u32>> = integral_image(&image);
assert_eq!(local_pixel_average(&integral_image, 3, 3, 2), 117);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/contrast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::cmp::{max, min};

use image::{GrayImage, ImageBuffer, Luma};
use image::{GrayImage, Luma};
#[cfg(feature = "rayon")]
use rayon::prelude::*;

Expand All @@ -19,7 +19,7 @@ use crate::stats::{cumulative_histogram, histogram};
pub fn adaptive_threshold(image: &GrayImage, block_radius: u32, delta: i32) -> GrayImage {
assert!(block_radius > 0);
let integral = integral_image::<_, u32>(image);
let mut out = ImageBuffer::from_pixel(image.width(), image.height(), Luma::black());
let mut out = GrayImage::from_pixel(image.width(), image.height(), Luma::black());

for y in 0..image.height() {
for x in 0..image.width() {
Expand Down
7 changes: 3 additions & 4 deletions src/distance_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! image from the nearest pixel of interest.
use crate::definitions::Image;
use image::{GenericImage, GenericImageView, GrayImage, ImageBuffer, Luma};
use image::{GenericImage, GenericImageView, GrayImage, Luma};
use std::cmp::min;

/// How to measure distance between coordinates.
Expand Down Expand Up @@ -115,8 +115,7 @@ pub(crate) fn distance_transform_impl(image: &mut GrayImage, norm: Norm, from: D
.iter_mut()
.for_each(|p| *p = if *p == 0 { 1 } else { 0 }),
}
let float_dist: ImageBuffer<Luma<f64>, Vec<f64>> =
euclidean_squared_distance_transform(image);
let float_dist: Image<Luma<f64>> = euclidean_squared_distance_transform(image);
image
.iter_mut()
.zip(float_dist.iter())
Expand Down Expand Up @@ -222,7 +221,7 @@ unsafe fn check(
/// [Distance Transforms of Sampled Functions]: https://www.cs.cornell.edu/~dph/papers/dt.pdf
pub fn euclidean_squared_distance_transform(image: &Image<Luma<u8>>) -> Image<Luma<f64>> {
let (width, height) = image.dimensions();
let mut result = ImageBuffer::new(width, height);
let mut result = Image::new(width, height);
let mut column_envelope = LowerEnvelope::new(height as usize);

// Compute 1d transforms of each column
Expand Down
4 changes: 2 additions & 2 deletions src/drawing/bezier.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::definitions::Image;
use crate::drawing::line::draw_line_segment_mut;
use crate::drawing::Canvas;
use image::{GenericImage, ImageBuffer};
use image::GenericImage;

/// Draws a cubic Bézier curve on an image.
///
Expand All @@ -18,7 +18,7 @@ pub fn draw_cubic_bezier_curve<I>(
where
I: GenericImage,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_cubic_bezier_curve_mut(&mut out, start, end, control_a, control_b, color);
out
Expand Down
10 changes: 5 additions & 5 deletions src/drawing/conics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::definitions::Image;
use crate::drawing::draw_if_in_bounds;
use crate::drawing::line::draw_line_segment_mut;
use crate::drawing::Canvas;
use image::{GenericImage, ImageBuffer};
use image::GenericImage;

/// Draws the outline of an ellipse on an image.
///
Expand All @@ -25,7 +25,7 @@ pub fn draw_hollow_ellipse<I>(
where
I: GenericImage,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_hollow_ellipse_mut(&mut out, center, width_radius, height_radius, color);
out
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn draw_filled_ellipse<I>(
where
I: GenericImage,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_filled_ellipse_mut(&mut out, center, width_radius, height_radius, color);
out
Expand Down Expand Up @@ -179,7 +179,7 @@ pub fn draw_hollow_circle<I>(
where
I: GenericImage,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_hollow_circle_mut(&mut out, center, radius, color);
out
Expand Down Expand Up @@ -228,7 +228,7 @@ pub fn draw_filled_circle<I>(
where
I: GenericImage,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_filled_circle_mut(&mut out, center, radius, color);
out
Expand Down
4 changes: 2 additions & 2 deletions src/drawing/cross.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::definitions::Image;
use crate::drawing::Canvas;
use image::{GenericImage, ImageBuffer};
use image::GenericImage;

/// Draws a colored cross on an image.
///
Expand All @@ -10,7 +10,7 @@ pub fn draw_cross<I>(image: &I, color: I::Pixel, x: i32, y: i32) -> Image<I::Pix
where
I: GenericImage,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_cross_mut(&mut out, color, x, y);
out
Expand Down
6 changes: 3 additions & 3 deletions src/drawing/line.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::definitions::Image;
use crate::drawing::Canvas;
use image::{GenericImage, ImageBuffer, Pixel};
use image::{GenericImage, Pixel};
use std::mem::{swap, transmute};

/// Iterates over the coordinates in a line segment using
Expand Down Expand Up @@ -174,7 +174,7 @@ pub fn draw_line_segment<I>(
where
I: GenericImage,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_line_segment_mut(&mut out, start, end, color);
out
Expand Down Expand Up @@ -220,7 +220,7 @@ where

B: Fn(I::Pixel, I::Pixel, f32) -> I::Pixel,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_antialiased_line_segment_mut(&mut out, start, end, color, blend);
out
Expand Down
6 changes: 3 additions & 3 deletions src/drawing/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::definitions::Image;
use crate::drawing::line::{draw_antialiased_line_segment_mut, draw_line_segment_mut};
use crate::drawing::Canvas;
use crate::point::Point;
use image::{GenericImage, ImageBuffer};
use image::GenericImage;
use std::cmp::{max, min};

/// Draws a polygon and its contents on an image.
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn draw_hollow_polygon<I>(
where
I: GenericImage,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_hollow_polygon_mut(&mut out, poly, color);
out
Expand Down Expand Up @@ -138,7 +138,7 @@ where
I: GenericImage,
L: Fn(&mut Image<I::Pixel>, (f32, f32), (f32, f32), I::Pixel),
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_polygon_with_mut(&mut out, poly, color, plotter);
out
Expand Down
6 changes: 3 additions & 3 deletions src/drawing/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::definitions::Image;
use crate::drawing::line::draw_line_segment_mut;
use crate::drawing::Canvas;
use crate::rect::Rect;
use image::{GenericImage, ImageBuffer};
use image::GenericImage;
use std::f32;

/// Draws the outline of a rectangle on an image.
Expand All @@ -13,7 +13,7 @@ pub fn draw_hollow_rect<I>(image: &I, rect: Rect, color: I::Pixel) -> Image<I::P
where
I: GenericImage,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_hollow_rect_mut(&mut out, rect, color);
out
Expand Down Expand Up @@ -42,7 +42,7 @@ pub fn draw_filled_rect<I>(image: &I, rect: Rect, color: I::Pixel) -> Image<I::P
where
I: GenericImage,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_filled_rect_mut(&mut out, rect, color);
out
Expand Down
4 changes: 2 additions & 2 deletions src/drawing/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use image::{GenericImage, ImageBuffer, Pixel};
use image::{GenericImage, Pixel};
use std::f32;

use crate::definitions::{Clamp, Image};
Expand Down Expand Up @@ -62,7 +62,7 @@ where
I: GenericImage,
<I::Pixel as Pixel>::Subpixel: Into<f32> + Clamp<f32>,
{
let mut out = ImageBuffer::new(image.width(), image.height());
let mut out = Image::new(image.width(), image.height());
out.copy_from(image, 0, 0).unwrap();
draw_text_mut(&mut out, color, x, y, scale, font, text);
out
Expand Down
24 changes: 10 additions & 14 deletions src/edges.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Functions for detecting edges in images.
use crate::definitions::{HasBlack, HasWhite};
use crate::definitions::{HasBlack, HasWhite, Image};
use crate::filter::{filter_clamped, gaussian_blur_f32};
use crate::kernel::{self};
use image::{GenericImageView, GrayImage, ImageBuffer, Luma};
use image::{GenericImageView, GrayImage, Luma};
use std::f32;

/// Runs the canny edge detection algorithm.
Expand Down Expand Up @@ -43,7 +43,7 @@ pub fn canny(image: &GrayImage, low_threshold: f32, high_threshold: f32) -> Gray
.map(|(h, v)| (*h as f32).hypot(*v as f32))
.collect::<Vec<f32>>();

let g = ImageBuffer::from_raw(image.width(), image.height(), g).unwrap();
let g = Image::from_raw(image.width(), image.height(), g).unwrap();

// 3. Non-maximum-suppression (Make edges thinner)
let thinned = non_maximum_suppression(&g, &gx, &gy);
Expand All @@ -54,12 +54,12 @@ pub fn canny(image: &GrayImage, low_threshold: f32, high_threshold: f32) -> Gray

/// Finds local maxima to make the edges thinner.
fn non_maximum_suppression(
g: &ImageBuffer<Luma<f32>, Vec<f32>>,
gx: &ImageBuffer<Luma<i16>, Vec<i16>>,
gy: &ImageBuffer<Luma<i16>, Vec<i16>>,
) -> ImageBuffer<Luma<f32>, Vec<f32>> {
g: &Image<Luma<f32>>,
gx: &Image<Luma<i16>>,
gy: &Image<Luma<i16>>,
) -> Image<Luma<f32>> {
const RADIANS_TO_DEGREES: f32 = 180f32 / f32::consts::PI;
let mut out = ImageBuffer::from_pixel(g.width(), g.height(), Luma([0.0]));
let mut out = Image::from_pixel(g.width(), g.height(), Luma([0.0]));
for y in 1..g.height() - 1 {
for x in 1..g.width() - 1 {
let x_gradient = gx[(x, y)][0] as f32;
Expand Down Expand Up @@ -111,15 +111,11 @@ fn non_maximum_suppression(

/// Filter out edges with the thresholds.
/// Non-recursive breadth-first search.
fn hysteresis(
input: &ImageBuffer<Luma<f32>, Vec<f32>>,
low_thresh: f32,
high_thresh: f32,
) -> ImageBuffer<Luma<u8>, Vec<u8>> {
fn hysteresis(input: &Image<Luma<f32>>, low_thresh: f32, high_thresh: f32) -> Image<Luma<u8>> {
let max_brightness = Luma::white();
let min_brightness = Luma::black();
// Init output image as all black.
let mut out = ImageBuffer::from_pixel(input.width(), input.height(), min_brightness);
let mut out = Image::from_pixel(input.width(), input.height(), min_brightness);
// Stack. Possible optimization: Use previously allocated memory, i.e. gx.
let mut edges = Vec::with_capacity(((input.width() * input.height()) / 2) as usize);
for y in 1..input.height() - 1 {
Expand Down
Loading

0 comments on commit 611ed99

Please sign in to comment.