@@ -1537,6 +1537,62 @@ def pad_with_params(
15371537 return pad_fn (img )
15381538
15391539
1540+ def pad_images_with_params (
1541+ images : np .ndarray ,
1542+ h_pad_top : int ,
1543+ h_pad_bottom : int ,
1544+ w_pad_left : int ,
1545+ w_pad_right : int ,
1546+ border_mode : int ,
1547+ value : tuple [float , ...] | float | None ,
1548+ ) -> np .ndarray :
1549+ """Pad a batch of images with explicitly defined padding on each side.
1550+
1551+ This function adds specified amounts of padding to each side of the image for each
1552+ image in the batch.
1553+
1554+ Args:
1555+ images (np.ndarray): Input batch of images to pad.
1556+ h_pad_top (int): Number of pixels to add at the top.
1557+ h_pad_bottom (int): Number of pixels to add at the bottom.
1558+ w_pad_left (int): Number of pixels to add on the left.
1559+ w_pad_right (int): Number of pixels to add on the right.
1560+ border_mode (int): OpenCV border mode for padding.
1561+ value (tuple[float, ...] | float | None): Value(s) to fill the border pixels.
1562+
1563+ Returns:
1564+ np.ndarray: Padded batch of images.
1565+
1566+ """
1567+ no_channel_dim = images .ndim == 3
1568+ if no_channel_dim :
1569+ images = images [..., np .newaxis ]
1570+
1571+ cv2np_border_modes = {
1572+ cv2 .BORDER_CONSTANT : "constant" ,
1573+ cv2 .BORDER_REPLICATE : "edge" ,
1574+ cv2 .BORDER_REFLECT : "symmetric" ,
1575+ cv2 .BORDER_WRAP : "wrap" ,
1576+ cv2 .BORDER_REFLECT_101 : "reflect" ,
1577+ cv2 .BORDER_REFLECT101 : "reflect" ,
1578+ cv2 .BORDER_DEFAULT : "reflect" , # same as cv2.BORDER_REFLECT_101
1579+ }
1580+ mode = cv2np_border_modes [border_mode ]
1581+
1582+ pad_width = ((0 , 0 ), (h_pad_top , h_pad_bottom ), (w_pad_left , w_pad_right ), (0 , 0 ))
1583+ if mode == "constant" :
1584+ constant_values = np .array (((0 , 0 ), (value , value ), (value , value ), (0 , 0 )), dtype = object )
1585+ kwargs = {"constant_values" : constant_values }
1586+ else :
1587+ kwargs = {}
1588+
1589+ images = np .pad (images , pad_width = pad_width , mode = mode , ** kwargs )
1590+ if no_channel_dim :
1591+ images = images [..., 0 ]
1592+
1593+ return images
1594+
1595+
15401596@preserve_channel_dim
15411597def remap (
15421598 img : np .ndarray ,
0 commit comments