简体   繁体   中英

Stacking two 3D numpy arrays along the median of the z-axis and keep the size of the smaller array

I want to stack two sets of images (im-series1 = 32x32x16 and im_series2 = 32x32x21) based on the median of the z axis and keep the adjacent values with respect to the shape of im-series1 1 .

在此处输入图片说明

What you should do is first crop im_series2, and then stack. Note that there are two ways to crop im_series to fit im_series1, both are "median".

import numpy as np
im_series2 = np.ones((32, 32, n)) # this is im_series2 as an example
mid = n // 2
im_series2 = im_series2[:, :, mid-8:mid+8] # this is the cropping. [:,:,2:-3] is also valid
print(im_series2.shape)
im_series1 = np.ones((32, 32, 16)) # this is im_series1 as an example
print(im_series1 .shape)
c = np.concatenate((im_series1 , im_series2), axis=-1) # this concatenates them on the z_axis
print(c.shape)

This outputs:

(32, 32, 16)
(32, 32, 16)
(32, 32, 32)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM