pub struct Image<T, const C: usize>(pub Tensor<T, 3, CpuAllocator>);Expand description
Represents an image with pixel data.
The image is represented as a 3D Tensor with shape (H, W, C), where H is the height of the image,
Tuple Fields§
§0: Tensor<T, 3, CpuAllocator>Implementations§
Source§impl<T, const C: usize> Image<T, C>
impl<T, const C: usize> Image<T, C>
Sourcepub fn new(size: ImageSize, data: Vec<T>) -> Result<Image<T, C>, ImageError>where
T: Clone,
pub fn new(size: ImageSize, data: Vec<T>) -> Result<Image<T, C>, ImageError>where
T: Clone,
Create a new image from pixel data.
§Arguments
size- The size of the image in pixels.data- The pixel data of the image.
§Returns
A new image with the given pixel data.
§Errors
If the length of the pixel data does not match the image size, an error is returned.
§Examples
use kornia_image::{Image, ImageSize};
let image = Image::<u8, 3>::new(
ImageSize {
width: 10,
height: 20,
},
vec![0u8; 10 * 20 * 3],
).unwrap();
assert_eq!(image.size().width, 10);
assert_eq!(image.size().height, 20);
assert_eq!(image.num_channels(), 3);Sourcepub fn from_size_val(size: ImageSize, val: T) -> Result<Image<T, C>, ImageError>
pub fn from_size_val(size: ImageSize, val: T) -> Result<Image<T, C>, ImageError>
Create a new image with the given size and default pixel data.
§Arguments
size- The size of the image in pixels.val- The default value of the pixel data.
§Returns
A new image with the given size and default pixel data.
§Errors
If the length of the pixel data does not match the image size, an error is returned.
§Examples
use kornia_image::{Image, ImageSize};
let image = Image::<u8, 3>::from_size_val(
ImageSize {
width: 10,
height: 20,
}, 0u8).unwrap();
assert_eq!(image.size().width, 10);
assert_eq!(image.size().height, 20);
assert_eq!(image.num_channels(), 3);Sourcepub unsafe fn from_raw_parts(
size: ImageSize,
data: *const T,
len: usize,
) -> Result<Image<T, C>, ImageError>where
T: Clone,
pub unsafe fn from_raw_parts(
size: ImageSize,
data: *const T,
len: usize,
) -> Result<Image<T, C>, ImageError>where
T: Clone,
Sourcepub fn from_size_slice(
size: ImageSize,
data: &[T],
) -> Result<Image<T, C>, ImageError>where
T: Clone,
pub fn from_size_slice(
size: ImageSize,
data: &[T],
) -> Result<Image<T, C>, ImageError>where
T: Clone,
Create a new image from a slice of pixel data.
§Arguments
size- The size of the image in pixels.data- A slice containing the pixel data.
§Returns
A new image created from the given size and pixel data.
§Errors
Returns an error if the length of the data slice doesn’t match the image dimensions, or if there’s an issue creating the tensor or image.
Sourcepub fn cast<U>(&self) -> Result<Image<U, C>, ImageError>
pub fn cast<U>(&self) -> Result<Image<U, C>, ImageError>
Cast the pixel data of the image to a different type.
§Returns
A new image with the pixel data cast to the given type.
Sourcepub fn split_channels(&self) -> Result<Vec<Image<T, 1>>, ImageError>
pub fn split_channels(&self) -> Result<Vec<Image<T, 1>>, ImageError>
Split the image into its channels.
§Returns
A vector of images, each containing one channel of the original image.
§Examples
use kornia_image::{Image, ImageSize};
let image = Image::<f32, 2>::from_size_val(
ImageSize {
width: 10,
height: 20,
},
0.0f32).unwrap();
let channels = image.split_channels().unwrap();
assert_eq!(channels.len(), 2);Sourcepub fn num_channels(&self) -> usize
pub fn num_channels(&self) -> usize
Get the number of channels in the image.
Sourcepub fn cast_and_scale<U>(self, scale: U) -> Result<Image<U, C>, ImageError>
pub fn cast_and_scale<U>(self, scale: U) -> Result<Image<U, C>, ImageError>
Cast the pixel data to a different type and scale it.
§Arguments
scale- The scale to multiply the pixel data with.
§Returns
A new image with the pixel data cast to the new type and scaled.
§Errors
If the pixel data cannot be cast to the new type, an error is returned.
§Examples
use kornia_image::{Image, ImageSize};
let data = vec![0u8, 0, 255, 0, 0, 255];
let image_u8 = Image::<u8, 3>::new(
ImageSize {
height: 2,
width: 1,
},
data,
).unwrap();
let image_f32 = image_u8.cast_and_scale::<f32>(1. / 255.0).unwrap();
assert_eq!(image_f32.get([1, 0, 2]), Some(&1.0f32));Sourcepub fn scale_and_cast<U>(&self, scale: T) -> Result<Image<U, C>, ImageError>
pub fn scale_and_cast<U>(&self, scale: T) -> Result<Image<U, C>, ImageError>
Sourcepub fn get_pixel(&self, x: usize, y: usize, ch: usize) -> Result<&T, ImageError>
pub fn get_pixel(&self, x: usize, y: usize, ch: usize) -> Result<&T, ImageError>
Get the pixel data of the image.
NOTE: this is method is for convenience and not optimized for performance. We recommend using iterators over the data slice.
§Arguments
x- The x-coordinate of the pixel.y- The y-coordinate of the pixel.ch- The channel index of the pixel.
§Returns
The pixel value at the given coordinates.
Sourcepub fn set_pixel(
&mut self,
x: usize,
y: usize,
ch: usize,
val: T,
) -> Result<(), ImageError>
pub fn set_pixel( &mut self, x: usize, y: usize, ch: usize, val: T, ) -> Result<(), ImageError>
Set the pixel value at the given coordinates.
NOTE: this is method is for convenience and not optimized for performance. We recommend creating a mutable slice and operating on it directly.
§Arguments
x- The x-coordinate of the pixel.y- The y-coordinate of the pixel.ch- The channel index of the pixel.val- The value to set the pixel to.
§Returns
The pixel value at the given coordinates.
Methods from Deref<Target = Tensor<T, 3, CpuAllocator>>§
Sourcepub fn as_slice_mut(&mut self) -> &mut [T]
pub fn as_slice_mut(&mut self) -> &mut [T]
Get the data of the tensor as a mutable slice.
§Returns
A mutable slice containing the data of the tensor.
Sourcepub fn as_mut_ptr(&mut self) -> *mut T
pub fn as_mut_ptr(&mut self) -> *mut T
Get the data of the tensor as a mutable pointer.
§Returns
A mutable pointer to the data of the tensor.
Sourcepub fn get_iter_offset_unchecked(&self, index: [usize; N]) -> usize
pub fn get_iter_offset_unchecked(&self, index: [usize; N]) -> usize
Sourcepub fn get_index_unchecked(&self, offset: usize) -> [usize; N]
pub fn get_index_unchecked(&self, offset: usize) -> [usize; N]
Sourcepub fn get_unchecked(&self, index: [usize; N]) -> &T
pub fn get_unchecked(&self, index: [usize; N]) -> &T
Get the element at the given index without checking if the index is out of bounds.
§Arguments
index- The list of indices to get the element from.
§Returns
A reference to the element at the given index.
§Example
use kornia_tensor::{Tensor, CpuAllocator};
let data: Vec<u8> = vec![1, 2, 3, 4];
let t = Tensor::<u8, 2, CpuAllocator>::from_shape_vec([2, 2], data, CpuAllocator).unwrap();
assert_eq!(*t.get_unchecked([0, 0]), 1);
assert_eq!(*t.get_unchecked([0, 1]), 2);
assert_eq!(*t.get_unchecked([1, 0]), 3);
assert_eq!(*t.get_unchecked([1, 1]), 4);Sourcepub fn get(&self, index: [usize; N]) -> Option<&T>
pub fn get(&self, index: [usize; N]) -> Option<&T>
Get the element at the given index, checking if the index is out of bounds.
§Arguments
index- The list of indices to get the element from.
§Returns
A reference to the element at the given index.
§Errors
If the index is out of bounds, an error is returned.
§Example
use kornia_tensor::{Tensor, CpuAllocator};
let data: Vec<u8> = vec![1, 2, 3, 4];
let t = Tensor::<u8, 2, CpuAllocator>::from_shape_vec([2, 2], data, CpuAllocator).unwrap();
assert_eq!(t.get([0, 0]), Some(&1));
assert_eq!(t.get([0, 1]), Some(&2));
assert_eq!(t.get([1, 0]), Some(&3));
assert_eq!(t.get([1, 1]), Some(&4));
assert!(t.get([2, 0]).is_none());Sourcepub fn reshape<const M: usize>(
&self,
shape: [usize; M],
) -> Result<TensorView<'_, T, M, A>, TensorError>
pub fn reshape<const M: usize>( &self, shape: [usize; M], ) -> Result<TensorView<'_, T, M, A>, TensorError>
Reshape the tensor to a new shape.
§Arguments
shape- The new shape of the tensor.
§Returns
A new TensorView instance.
§Errors
If the number of elements in the new shape does not match the number of elements in the tensor, an error is returned.
§Example
use kornia_tensor::{Tensor, CpuAllocator};
let data: Vec<u8> = vec![1, 2, 3, 4];
let t = Tensor::<u8, 1, CpuAllocator>::from_shape_vec([4], data, CpuAllocator).unwrap();
let t2 = t.reshape([2, 2]).unwrap();
assert_eq!(t2.shape, [2, 2]);
assert_eq!(t2.as_slice(), vec![1, 2, 3, 4]);
assert_eq!(t2.strides, [2, 1]);
assert_eq!(t2.numel(), 4);Sourcepub fn permute_axes(&self, axes: [usize; N]) -> TensorView<'_, T, N, A>
pub fn permute_axes(&self, axes: [usize; N]) -> TensorView<'_, T, N, A>
Permute the dimensions of the tensor.
The permutation is given as an array of indices, where the value at each index is the new index of the dimension. The data is not moved, only the order of the dimensions is changed.
§Arguments
axes- The new order of the dimensions.
§Returns
A view of the tensor with the dimensions permuted.
Sourcepub fn view(&self) -> TensorView<'_, T, N, A>
pub fn view(&self) -> TensorView<'_, T, N, A>
Return a view of the tensor.
The view is a reference to the tensor storage with a different shape and strides.
§Returns
A TensorView instance.
Sourcepub fn map<U, F>(&self, f: F) -> Tensor<U, N, A>
pub fn map<U, F>(&self, f: F) -> Tensor<U, N, A>
Apply a function to each element of the tensor.
§Arguments
f- The function to apply to each element.
§Returns
A new Tensor instance.
§Example
use kornia_tensor::{Tensor, CpuAllocator};
let data: Vec<u8> = vec![1, 2, 3, 4];
let t = Tensor::<u8, 1, CpuAllocator>::from_shape_vec([4], data, CpuAllocator).unwrap();
let t2 = t.map(|x| *x + 1);
assert_eq!(t2.as_slice(), vec![2, 3, 4, 5]);Sourcepub fn cast<U>(&self) -> Tensor<U, N, CpuAllocator>
pub fn cast<U>(&self) -> Tensor<U, N, CpuAllocator>
Cast the tensor to a new type.
§Returns
A new Tensor instance.
§Example
use kornia_tensor::{Tensor, CpuAllocator};
let data: Vec<u8> = vec![1, 2, 3, 4];
let t = Tensor::<u8, 1, CpuAllocator>::from_shape_vec([4], data, CpuAllocator).unwrap();
let t2 = t.cast::<f32>();
assert_eq!(t2.as_slice(), vec![1.0, 2.0, 3.0, 4.0]);Sourcepub fn element_wise_op<F>(
&self,
other: &Tensor<T, N, CpuAllocator>,
op: F,
) -> Result<Tensor<T, N, CpuAllocator>, TensorError>
pub fn element_wise_op<F>( &self, other: &Tensor<T, N, CpuAllocator>, op: F, ) -> Result<Tensor<T, N, CpuAllocator>, TensorError>
Perform an element-wise operation on two tensors.
§Arguments
other- The other tensor to perform the operation with.op- The operation to perform.
§Returns
A new Tensor instance.
§Example
use kornia_tensor::{Tensor, CpuAllocator};
let data1: Vec<u8> = vec![1, 2, 3, 4];
let t1 = Tensor::<u8, 1, CpuAllocator>::from_shape_vec([4], data1, CpuAllocator).unwrap();
let data2: Vec<u8> = vec![1, 2, 3, 4];
let t2 = Tensor::<u8, 1, CpuAllocator>::from_shape_vec([4], data2, CpuAllocator).unwrap();
let t3 = t1.element_wise_op(&t2, |a, b| *a + *b).unwrap();
assert_eq!(t3.as_slice(), vec![2, 4, 6, 8]);
let t4 = t1.element_wise_op(&t2, |a, b| *a - *b).unwrap();
assert_eq!(t4.as_slice(), vec![0, 0, 0, 0]);
let t5 = t1.element_wise_op(&t2, |a, b| *a * *b).unwrap();
assert_eq!(t5.as_slice(), vec![1, 4, 9, 16]);
let t6 = t1.element_wise_op(&t2, |a, b| *a / *b).unwrap();
assert_eq!(t6.as_slice(), vec![1, 1, 1, 1]);Trait Implementations§
Source§impl<T> TryFrom<Tensor<T, 2, CpuAllocator>> for Image<T, 1>where
T: Clone,
helper to convert an single channel tensor to a kornia image with try into
impl<T> TryFrom<Tensor<T, 2, CpuAllocator>> for Image<T, 1>where
T: Clone,
helper to convert an single channel tensor to a kornia image with try into
Source§impl<T, const C: usize> TryFrom<Tensor<T, 3, CpuAllocator>> for Image<T, C>
helper to convert an multi channel tensor to a kornia image with try into
impl<T, const C: usize> TryFrom<Tensor<T, 3, CpuAllocator>> for Image<T, C>
helper to convert an multi channel tensor to a kornia image with try into
Auto Trait Implementations§
impl<T, const C: usize> Freeze for Image<T, C>
impl<T, const C: usize> RefUnwindSafe for Image<T, C>where
T: RefUnwindSafe,
impl<T, const C: usize> Send for Image<T, C>
impl<T, const C: usize> Sync for Image<T, C>
impl<T, const C: usize> Unpin for Image<T, C>
impl<T, const C: usize> UnwindSafe for Image<T, C>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more