-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
use crate::image::Image; | ||
use anyhow::Result; | ||
|
||
/// Flip the input image horizontally. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `image` - The input image with shape (H, W, C). | ||
/// | ||
/// # Returns | ||
/// | ||
/// The flipped image. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use kornia_rs::image::{Image, ImageSize}; | ||
/// | ||
/// let image = Image::<f32, 3>::new( | ||
/// ImageSize { | ||
/// width: 2, | ||
/// height: 3, | ||
/// }, | ||
/// vec![0f32; 2 * 3 * 3], | ||
/// ) | ||
/// .unwrap(); | ||
/// let flipped: Image<f32, 3> = kornia_rs::flip::horizontal_flip(&image).unwrap(); | ||
/// assert_eq!(flipped.image_size().width, 2); | ||
/// assert_eq!(flipped.image_size().height, 3); | ||
/// ``` | ||
pub fn horizontal_flip<T, const CHANNELS: usize>( | ||
image: &Image<T, CHANNELS>, | ||
) -> Result<Image<T, CHANNELS>> | ||
where | ||
T: Copy, | ||
{ | ||
// NOTE: verify that this is efficient | ||
let mut img = image.clone(); | ||
|
||
img.data | ||
.axis_iter_mut(ndarray::Axis(0)) | ||
.for_each(|mut row| { | ||
row.as_slice_mut().unwrap().reverse(); | ||
}); | ||
|
||
Ok(img) | ||
} | ||
|
||
/// Flip the input image vertically. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `image` - The input image with shape (H, W, C). | ||
/// | ||
/// # Returns | ||
/// | ||
/// The flipped image. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use kornia_rs::image::{Image, ImageSize}; | ||
/// | ||
/// let image = Image::<f32, 3>::new( | ||
/// ImageSize { | ||
/// width: 2, | ||
/// height: 3, | ||
/// }, | ||
/// vec![0f32; 2 * 3 * 3], | ||
/// ) | ||
/// .unwrap(); | ||
/// let flipped: Image<f32, 3> = kornia_rs::flip::vertical_flip(&image).unwrap(); | ||
/// assert_eq!(flipped.image_size().width, 2); | ||
/// assert_eq!(flipped.image_size().height, 3); | ||
/// ``` | ||
pub fn vertical_flip<T, const CHANNELS: usize>( | ||
image: &Image<T, CHANNELS>, | ||
) -> Result<Image<T, CHANNELS>> | ||
where | ||
T: Copy, | ||
{ | ||
let mut img = image.clone(); | ||
|
||
img.data | ||
.axis_iter_mut(ndarray::Axis(1)) | ||
.for_each(|mut col| { | ||
let mut i = 0; | ||
let mut j = image.height() - 1; | ||
while i < j { | ||
for c in 0..CHANNELS { | ||
col.swap((i, c), (j, c)); | ||
} | ||
i += 1; | ||
j -= 1; | ||
} | ||
}); | ||
|
||
Ok(img) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::image::{Image, ImageSize}; | ||
|
||
#[test] | ||
fn test_hflip() { | ||
let image = Image::<_, 1>::new( | ||
ImageSize { | ||
width: 2, | ||
height: 3, | ||
}, | ||
vec![0u8, 1, 2, 3, 4, 5], | ||
) | ||
.unwrap(); | ||
let data_expected = vec![1u8, 0, 3, 2, 5, 4]; | ||
let flipped = horizontal_flip(&image).unwrap(); | ||
assert_eq!(flipped.data.as_slice().unwrap(), &data_expected); | ||
} | ||
|
||
#[test] | ||
fn test_vflip() { | ||
let image = Image::<_, 1>::new( | ||
ImageSize { | ||
width: 2, | ||
height: 3, | ||
}, | ||
vec![0u8, 1, 2, 3, 4, 5], | ||
) | ||
.unwrap(); | ||
let data_expected = vec![4u8, 5, 2, 3, 0, 1]; | ||
let flipped = vertical_flip(&image).unwrap(); | ||
assert_eq!(flipped.data.as_slice().unwrap(), &data_expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
pub mod color; | ||
pub mod distance_transform; | ||
pub mod flip; | ||
pub mod image; | ||
pub mod io; | ||
pub mod normalize; | ||
|