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: ChannelOrderChannel ordering (CHW or HWC)
Implementations§
Source§impl NormalizeImage
impl NormalizeImage
Sourcepub fn new(
scale: Option<f32>,
mean: Option<Vec<f32>>,
std: Option<Vec<f32>>,
order: Option<ChannelOrder>,
) -> Result<Self, OCRError>
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
Sourcepub fn validate_config(&self) -> Result<(), OCRError>
pub fn validate_config(&self) -> Result<(), OCRError>
Sourcepub fn for_ocr_recognition() -> Result<Self, OCRError>
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.
Sourcepub fn apply_to_batch(
&self,
imgs: Vec<DynamicImage>,
batch_tensor: &mut [f32],
shapes: &[(usize, usize, usize)],
) -> Result<(), OCRError>
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 normalizebatch_tensor- A mutable slice where the normalized batch will be storedshapes- Shapes of the images as (channels, height, width) tuples
§Returns
A Result indicating success or an OCRError if validation fails.
Sourcepub fn normalize_streaming_to_batch(
&self,
imgs: Vec<DynamicImage>,
batch_tensor: &mut [f32],
shapes: &[(usize, usize, usize)],
) -> Result<(), OCRError>
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 normalizebatch_tensor- A mutable slice where the normalized batch will be storedshapes- Shapes of the images as (channels, height, width) tuples
§Returns
A Result indicating success or an OCRError if validation fails.
Sourcepub fn normalize_to(&self, img: DynamicImage) -> Result<Tensor4D, OCRError>
pub fn normalize_to(&self, img: DynamicImage) -> Result<Tensor4D, OCRError>
Sourcepub fn normalize_batch_to(
&self,
imgs: Vec<DynamicImage>,
) -> Result<Tensor4D, OCRError>
pub fn normalize_batch_to( &self, imgs: Vec<DynamicImage>, ) -> Result<Tensor4D, OCRError>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for NormalizeImage
impl RefUnwindSafe for NormalizeImage
impl Send for NormalizeImage
impl Sync for NormalizeImage
impl Unpin for NormalizeImage
impl UnwindSafe for NormalizeImage
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.