Scipy rotate and zoom an image without changing its dimensions

前端 未结 2 1334
夕颜
夕颜 2021-02-01 21:44

For my neural network I want to augment my training data by adding small random rotations and zooms to my images. The issue I am having is that scipy is changing the size of my

2条回答
  •  北海茫月
    2021-02-01 22:17

    scipy.ndimage.rotate accepts a reshape= parameter:

    reshape : bool, optional

    If reshape is true, the output shape is adapted so that the input array is contained completely in the output. Default is True.

    So to "clip" the edges you can simply call scipy.ndimage.rotate(img, ..., reshape=False).

    from scipy.ndimage import rotate
    from scipy.misc import face
    from matplotlib import pyplot as plt
    
    img = face()
    rot = rotate(img, 30, reshape=False)
    
    fig, ax = plt.subplots(1, 2)
    ax[0].imshow(img)
    ax[1].imshow(rot)
    

    Things are more complicated for scipy.ndimage.zoom.

    A naive method would be to zoom the entire input array, then use slice indexing and/or zero-padding to make the output the same size as your input. However, in cases where you're increasing the size of the image it's wasteful to interpolate pixels that are only going to get clipped off at the edges anyway.

    Instead you could index only the part of the input that will fall within the bounds of the output array before you apply zoom:

    import numpy as np
    from scipy.ndimage import zoom
    
    
    def clipped_zoom(img, zoom_factor, **kwargs):
    
        h, w = img.shape[:2]
    
        # For multichannel images we don't want to apply the zoom factor to the RGB
        # dimension, so instead we create a tuple of zoom factors, one per array
        # dimension, with 1's for any trailing dimensions after the width and height.
        zoom_tuple = (zoom_factor,) * 2 + (1,) * (img.ndim - 2)
    
        # Zooming out
        if zoom_factor < 1:
    
            # Bounding box of the zoomed-out image within the output array
            zh = int(np.round(h * zoom_factor))
            zw = int(np.round(w * zoom_factor))
            top = (h - zh) // 2
            left = (w - zw) // 2
    
            # Zero-padding
            out = np.zeros_like(img)
            out[top:top+zh, left:left+zw] = zoom(img, zoom_tuple, **kwargs)
    
        # Zooming in
        elif zoom_factor > 1:
    
            # Bounding box of the zoomed-in region within the input array
            zh = int(np.round(h / zoom_factor))
            zw = int(np.round(w / zoom_factor))
            top = (h - zh) // 2
            left = (w - zw) // 2
    
            out = zoom(img[top:top+zh, left:left+zw], zoom_tuple, **kwargs)
    
            # `out` might still be slightly larger than `img` due to rounding, so
            # trim off any extra pixels at the edges
            trim_top = ((out.shape[0] - h) // 2)
            trim_left = ((out.shape[1] - w) // 2)
            out = out[trim_top:trim_top+h, trim_left:trim_left+w]
    
        # If zoom_factor == 1, just return the input array
        else:
            out = img
        return out
    

    For example:

    zm1 = clipped_zoom(img, 0.5)
    zm2 = clipped_zoom(img, 1.5)
    
    fig, ax = plt.subplots(1, 3)
    ax[0].imshow(img)
    ax[1].imshow(zm1)
    ax[2].imshow(zm2)
    

提交回复
热议问题