Numpy Resize/Rescale Image

后端 未结 7 2085
轻奢々
轻奢々 2020-12-02 06:38

I would like to take an image and change the scale of the image, while it is a numpy array.

For example I have this image of a coca-cola bottle: bottle-1

Whi

7条回答
  •  庸人自扰
    2020-12-02 06:55

    For people coming here from Google looking for a fast way to downsample images in numpy arrays for use in Machine Learning applications, here's a super fast method (adapted from here ). This method only works when the input dimensions are a multiple of the output dimensions.

    The following examples downsample from 128x128 to 64x64 (this can be easily changed).

    Channels last ordering

    # large image is shape (128, 128, 3)
    # small image is shape (64, 64, 3)
    input_size = 128
    output_size = 64
    bin_size = input_size // output_size
    small_image = large_image.reshape((output_size, bin_size, 
                                       output_size, bin_size, 3)).max(3).max(1)
    

    Channels first ordering

    # large image is shape (3, 128, 128)
    # small image is shape (3, 64, 64)
    input_size = 128
    output_size = 64
    bin_size = input_size // output_size
    small_image = large_image.reshape((3, output_size, bin_size, 
                                          output_size, bin_size)).max(4).max(2)
    

    For grayscale images just change the 3 to a 1 like this:

    Channels first ordering

    # large image is shape (1, 128, 128)
    # small image is shape (1, 64, 64)
    input_size = 128
    output_size = 64
    bin_size = input_size // output_size
    small_image = large_image.reshape((1, output_size, bin_size,
                                          output_size, bin_size)).max(4).max(2)
    

    This method uses the equivalent of max pooling. It's the fastest way to do this that I've found.

提交回复
热议问题