Python plot - stacked image slices

前端 未结 3 761
北海茫月
北海茫月 2020-12-01 22:19

I have a series of basic 2D images (3 for simplicity for now) and these are related to each other, analogous to frames from a movie:

Within python how may I stack the

3条回答
  •  北海茫月
    2020-12-01 22:55

    Here is a completely silly way to accomplish using matplotlib and shear transformations (you probably need to tweak the transform matrix some more so the stacked images look correct):

    import numpy as np
    import matplotlib.pyplot as plt
    
    from scipy.ndimage.interpolation import affine_transform
    
    
    nimages = 4
    img_height, img_width = 512, 512
    bg_val = -1 # Some flag value indicating the background.
    
    # Random test images.
    rs = np.random.RandomState(123)
    img = rs.randn(img_height, img_width)*0.1
    images = [img+(i+1) for i in range(nimages)]
    
    stacked_height = 2*img_height
    stacked_width  = img_width + (nimages-1)*img_width/2
    stacked = np.full((stacked_height, stacked_width), bg_val)
    
    # Affine transform matrix.
    T = np.array([[1,-1],
                  [0, 1]])
    
    for i in range(nimages):
        # The first image will be right most and on the "bottom" of the stack.
        o = (nimages-i-1) * img_width/2
        out = affine_transform(images[i], T, offset=[o,-o],
                               output_shape=stacked.shape, cval=bg_val)
        stacked[out != bg_val] = out[out != bg_val]
    
    plt.imshow(stacked, cmap=plt.cm.viridis)
    plt.show()
    

提交回复
热议问题