File size: 3,987 Bytes
533891e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import numpy as np

def pad_to_center(img,img_height, img_width, req_height, req_width):
    """
    Goal of this function is to increase original image size, upto the
    req_height and width.

    Parameters:
        img -> 3D numpy array of shape Height x Width x Channels
        img_height -> height of current image
        img_width -> width of current image
        req_height -> max height you want your image to be padded to.
        req_width -> max width you want your image to be padded to.
    """

    # How many rows and columns needs to be added to make current image
    # upto what is needed
    rem_height = req_height - img_height
    rem_width = req_width - img_width

    # split the remaining height to be added evenly, to pad on top 
    # and bottom
    pad_top = rem_height // 2
    pad_bottom = rem_height - pad_top

    # split the remaining width to be added evenly, to pad on left
    # and right
    pad_left = rem_width // 2
    pad_right = rem_width - pad_left

    # Don't pad along batch size and channels dimension, pad everything else to required height and width
    # we are basically telling how many values to add on each of the 4 sides of image
    return np.pad(
                        img, 
                    (   
                        (pad_top, pad_bottom),
                        (pad_left, pad_right),
                        (0,0)
                    ),

                    mode = 'reflect'
                )


def crop_to_center(img, img_height, img_width, req_height, req_width, req_channel = 3):
    """
    Goal of this function is to reduce the original image(s) size to the required height and width,
    by making sure that we only trim the edges of images and not the middle part.

    Parameters:
        img -> 4d numpy array batch_size/num frames x Height x width x Channels
    """

    # difference in height and widths of an image, divided into equal halfs
    toph = (img_height - req_height)//2
    leftw = (img_width - req_width)//2

    # from bottom of image, how far up to go, so that top-bottom is center of image
    bothen = img_height - toph

    # from right of image, how far left to go, so that left-right is center of image
    rightwen = img_width - leftw
    
    cropped_image = img[toph:bothen, leftw:rightwen, :]

    assert cropped_image.shape == (req_height, req_width, req_channel)
    return cropped_image


def preprocess_images(img, req_height, req_width):

    """
    Crops the input image array to the specified height and width, 
    centered around the middle.

    Args:
        img (np.ndarray): The image to crop, represented as a NumPy array 
                          (height, width, channels).
        crop_height (int): The desired height of the cropped image.
        crop_width (int): The desired width of the cropped image.

    Returns:
        np.ndarray: The center-cropped image.
    """
    
    image_shape_tuple = img.shape
    assert len(image_shape_tuple) == 3, f"Please pass a 3D image with height, width and channels , you passed: {image_shape_tuple}"

    # assuming it to be H,W,C
    img_height, img_width, img_channel = image_shape_tuple

    # if original image height is less than req_height or original width less than req_width
    # pad them until required dimensions and return
    if img_height < req_height or img_width < req_width:
        return pad_to_center(img, img_height, img_width, req_height, req_width)

    # if your image size is same as cropped size, nothing to crop
    elif img_height == req_height and img_width == req_width:
        return img

    # your image height is greater than required height and width, so crop it to center frames.
    else:
        return crop_to_center(img,
                                img_height,
                                img_width,
                                req_height,
                                req_width,
                                img_channel
                                )