pub struct NormalizeImage {
pub alpha: Vec<f32>,
pub beta: Vec<f32>,
pub order: TensorLayout,
pub color_order: ColorOrder,
}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: TensorLayoutTensor data layout (CHW or HWC)
color_order: ColorOrderColor channel order (RGB or BGR)
Implementations§
Source§impl NormalizeImage
impl NormalizeImage
Sourcepub fn new(
scale: Option<f32>,
mean: Option<Vec<f32>>,
std: Option<Vec<f32>>,
order: Option<TensorLayout>,
color_order: Option<ColorOrder>,
) -> Result<NormalizeImage, OCRError>
pub fn new( scale: Option<f32>, mean: Option<Vec<f32>>, std: Option<Vec<f32>>, order: Option<TensorLayout>, color_order: Option<ColorOrder>, ) -> Result<NormalizeImage, 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 tensor data layout (defaults to CHW)color_order- Optional color channel order (defaults to BGR)
§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 with_color_order(
scale: Option<f32>,
mean: Option<Vec<f32>>,
std: Option<Vec<f32>>,
order: Option<TensorLayout>,
color_order: Option<ColorOrder>,
) -> Result<NormalizeImage, OCRError>
pub fn with_color_order( scale: Option<f32>, mean: Option<Vec<f32>>, std: Option<Vec<f32>>, order: Option<TensorLayout>, color_order: Option<ColorOrder>, ) -> Result<NormalizeImage, OCRError>
Creates a new NormalizeImage instance with the specified parameters including color order.
§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 tensor data layout (defaults to CHW)color_order- Optional color channel order (defaults to RGB)
§Mean/Std Semantics
mean and std must be provided in the output channel order specified by color_order.
For example, if color_order is BGR, pass mean/std as [B_mean, G_mean, R_mean].
Note: This function does not validate that mean/std values match the specified
color_order. Ensuring consistency is the caller’s responsibility. If you have stats
expressed in RGB order but need BGR output, prefer using
NormalizeImage::with_color_order_from_rgb_stats or
NormalizeImage::imagenet_bgr_from_rgb_stats which handle the reordering automatically.
§Returns
A Result containing the new NormalizeImage instance or an OCRError if validation fails.
Sourcepub fn validate_config(&self) -> Result<(), OCRError>
pub fn validate_config(&self) -> Result<(), OCRError>
Sourcepub fn for_ocr_recognition() -> Result<NormalizeImage, OCRError>
pub fn for_ocr_recognition() -> Result<NormalizeImage, 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 imagenet_rgb() -> Result<NormalizeImage, OCRError>
pub fn imagenet_rgb() -> Result<NormalizeImage, OCRError>
Creates an ImageNet-style RGB normalizer (mean/std in RGB order).
Sourcepub fn imagenet_bgr_from_rgb_stats() -> Result<NormalizeImage, OCRError>
pub fn imagenet_bgr_from_rgb_stats() -> Result<NormalizeImage, OCRError>
Creates an ImageNet-style BGR normalizer from RGB stats.
This is useful for PaddlePaddle-exported models that expect BGR input, while configuration commonly provides ImageNet mean/std in RGB order.
Sourcepub fn with_color_order_from_rgb_stats(
scale: Option<f32>,
mean_rgb: Vec<f32>,
std_rgb: Vec<f32>,
order: Option<TensorLayout>,
output_color_order: ColorOrder,
) -> Result<NormalizeImage, OCRError>
pub fn with_color_order_from_rgb_stats( scale: Option<f32>, mean_rgb: Vec<f32>, std_rgb: Vec<f32>, order: Option<TensorLayout>, output_color_order: ColorOrder, ) -> Result<NormalizeImage, OCRError>
Builds a normalizer for a given output color_order using RGB mean/std stats.
Invariant: mean/std passed to with_color_order are interpreted in the output channel
order (ColorOrder). This helper makes the conversion explicit at call sites.
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<ArrayBase<OwnedRepr<f32>, Dim<[usize; 4]>>, OCRError>
pub fn normalize_to( &self, img: DynamicImage, ) -> Result<ArrayBase<OwnedRepr<f32>, Dim<[usize; 4]>>, 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.