13
13
ValueType ,
14
14
clip ,
15
15
clipped ,
16
+ contiguous ,
16
17
convert_value ,
17
18
get_max_value ,
18
19
get_num_channels ,
@@ -574,16 +575,16 @@ def to_float(img: np.ndarray, max_value: float | None = None) -> np.ndarray:
574
575
return to_float_numpy (img , max_value )
575
576
576
577
577
- def from_float_numpy (img : np .ndarray , dtype : np .dtype , max_value : float | None = None ) -> np .ndarray :
578
+ def from_float_numpy (img : np .ndarray , target_dtype : np .dtype , max_value : float | None = None ) -> np .ndarray :
578
579
if max_value is None :
579
- max_value = get_max_value (dtype )
580
- return clip (np .rint (img * max_value ), dtype )
580
+ max_value = get_max_value (target_dtype )
581
+ return clip (np .rint (img * max_value ), target_dtype )
581
582
582
583
583
584
@preserve_channel_dim
584
- def from_float_opencv (img : np .ndarray , dtype : np .dtype , max_value : float | None = None ) -> np .ndarray :
585
+ def from_float_opencv (img : np .ndarray , target_dtype : np .dtype , max_value : float | None = None ) -> np .ndarray :
585
586
if max_value is None :
586
- max_value = get_max_value (dtype )
587
+ max_value = get_max_value (target_dtype )
587
588
588
589
img_float = img .astype (np .float32 )
589
590
@@ -592,14 +593,62 @@ def from_float_opencv(img: np.ndarray, dtype: np.dtype, max_value: float | None
592
593
if num_channels > MAX_OPENCV_WORKING_CHANNELS :
593
594
# For images with more than 4 channels, create a full-sized multiplier
594
595
max_value_array = np .full_like (img_float , max_value )
595
- return clip (np .rint (cv2 .multiply (img_float , max_value_array )), dtype )
596
+ return clip (np .rint (cv2 .multiply (img_float , max_value_array )), target_dtype )
596
597
597
598
# For images with 4 or fewer channels, use scalar multiplication
598
- return clip (np .rint (img * max_value ), dtype )
599
+ return clip (np .rint (img * max_value ), target_dtype )
599
600
600
601
601
- def from_float (img : np .ndarray , dtype : np .dtype , max_value : float | None = None ) -> np .ndarray :
602
+ def from_float (img : np .ndarray , target_dtype : np .dtype , max_value : float | None = None ) -> np .ndarray :
603
+ """Convert a floating-point image to the specified target data type.
604
+
605
+ This function converts an input floating-point image to the specified target data type,
606
+ scaling the values appropriately based on the max_value parameter or the maximum value
607
+ of the target data type.
608
+
609
+ Args:
610
+ img (np.ndarray): Input floating-point image array.
611
+ target_dtype (np.dtype): Target numpy data type for the output image.
612
+ max_value (float | None, optional): Maximum value to use for scaling. If None,
613
+ the maximum value of the target data type will be used. Defaults to None.
614
+
615
+ Returns:
616
+ np.ndarray: Image converted to the target data type.
617
+
618
+ Notes:
619
+ - If the input image is of type float32, the function uses OpenCV for faster processing.
620
+ - For other input types, it falls back to a numpy-based implementation.
621
+ - The function clips values to ensure they fit within the range of the target data type.
622
+ """
602
623
if img .dtype == np .float32 :
603
- return from_float_opencv (img , dtype , max_value )
624
+ return from_float_opencv (img , target_dtype , max_value )
625
+
626
+ return from_float_numpy (img , target_dtype , max_value )
627
+
628
+
629
+ @contiguous
630
+ def hflip_numpy (img : np .ndarray ) -> np .ndarray :
631
+ return img [:, ::- 1 , ...]
632
+
633
+
634
+ @preserve_channel_dim
635
+ def hflip_cv2 (img : np .ndarray ) -> np .ndarray :
636
+ return cv2 .flip (img , 1 )
637
+
638
+
639
+ def hflip (img : np .ndarray ) -> np .ndarray :
640
+ return hflip_cv2 (img )
641
+
642
+
643
+ @preserve_channel_dim
644
+ def vflip_cv2 (img : np .ndarray ) -> np .ndarray :
645
+ return cv2 .flip (img , 0 )
646
+
647
+
648
+ @contiguous
649
+ def vflip_numpy (img : np .ndarray ) -> np .ndarray :
650
+ return img [::- 1 , ...]
651
+
604
652
605
- return from_float_numpy (img , dtype , max_value )
653
+ def vflip (img : np .ndarray ) -> np .ndarray :
654
+ return vflip_cv2 (img )
0 commit comments