NormalizeImage

Struct NormalizeImage 

Source
pub struct NormalizeImage {
    pub alpha: Vec<f32>,
    pub beta: Vec<f32>,
    pub order: ChannelOrder,
}
Expand description

Normalizes images for OCR processing.

This struct encapsulates the parameters needed to normalize images, including scaling factors, mean values, standard deviations, and channel ordering. It provides methods to apply normalization to single images or batches of images.

Fields§

§alpha: Vec<f32>

Scaling factors for each channel (alpha = scale / std)

§beta: Vec<f32>

Offset values for each channel (beta = -mean / std)

§order: ChannelOrder

Channel ordering (CHW or HWC)

Implementations§

Source§

impl NormalizeImage

Source

pub fn new( scale: Option<f32>, mean: Option<Vec<f32>>, std: Option<Vec<f32>>, order: Option<ChannelOrder>, ) -> Result<Self, OCRError>

Creates a new NormalizeImage instance with the specified parameters.

§Arguments
  • scale - Optional scaling factor (defaults to 1.0/255.0)
  • mean - Optional mean values for each channel (defaults to [0.485, 0.456, 0.406])
  • std - Optional standard deviation values for each channel (defaults to [0.229, 0.224, 0.225])
  • order - Optional channel ordering (defaults to CHW)
§Returns

A Result containing the new NormalizeImage instance or an OCRError if validation fails.

§Errors

Returns an error if:

  • Scale is less than or equal to 0
  • Mean or std vectors don’t have exactly 3 elements
  • Any standard deviation value is less than or equal to 0
Source

pub fn validate_config(&self) -> Result<(), OCRError>

Validates the configuration of the NormalizeImage instance.

§Returns

A Result indicating success or an OCRError if validation fails.

§Errors

Returns an error if:

  • Alpha or beta vectors don’t have exactly 3 elements
  • Any alpha or beta value is not finite
Source

pub fn for_ocr_recognition() -> Result<Self, OCRError>

Creates a NormalizeImage instance with parameters suitable for OCR recognition.

This creates a normalization configuration with:

  • Scale: 2.0/255.0
  • Mean: [1.0, 1.0, 1.0]
  • Std: [1.0, 1.0, 1.0]
  • Order: CHW
§Returns

A Result containing the new NormalizeImage instance or an OCRError.

Source

pub fn apply(&self, imgs: Vec<DynamicImage>) -> Vec<Vec<f32>>

Applies normalization to a vector of images.

§Arguments
  • imgs - A vector of DynamicImage instances to normalize
§Returns

A vector of normalized images represented as vectors of f32 values

Source

pub fn apply_to_batch( &self, imgs: Vec<DynamicImage>, batch_tensor: &mut [f32], shapes: &[(usize, usize, usize)], ) -> Result<(), OCRError>

Applies normalization to a batch of images and stores the result in a pre-allocated tensor.

§Arguments
  • imgs - A vector of DynamicImage instances to normalize
  • batch_tensor - A mutable slice where the normalized batch will be stored
  • shapes - Shapes of the images as (channels, height, width) tuples
§Returns

A Result indicating success or an OCRError if validation fails.

Source

pub fn normalize_streaming_to_batch( &self, imgs: Vec<DynamicImage>, batch_tensor: &mut [f32], shapes: &[(usize, usize, usize)], ) -> Result<(), OCRError>

Applies normalization to a batch of images and stores the result in a pre-allocated tensor, processing images in a streaming fashion.

§Arguments
  • imgs - A vector of DynamicImage instances to normalize
  • batch_tensor - A mutable slice where the normalized batch will be stored
  • shapes - Shapes of the images as (channels, height, width) tuples
§Returns

A Result indicating success or an OCRError if validation fails.

Source

pub fn normalize_to(&self, img: DynamicImage) -> Result<Tensor4D, OCRError>

Normalizes a single image and returns it as a 4D tensor.

§Arguments
  • img - The DynamicImage to normalize
§Returns

A Result containing the normalized image as a 4D tensor or an OCRError.

Source

pub fn normalize_batch_to( &self, imgs: Vec<DynamicImage>, ) -> Result<Tensor4D, OCRError>

Normalizes a batch of images and returns them as a 4D tensor.

§Arguments
  • imgs - A vector of DynamicImage instances to normalize
§Returns

A Result containing the normalized batch as a 4D tensor or an OCRError.

§Errors

Returns an error if:

  • Images in the batch don’t all have the same dimensions

Trait Implementations§

Source§

impl Debug for NormalizeImage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more