0

I have an image file containing a 4d array of MR image in the shape (240,240,155,4) and I want to crop it to the non zero values.

I tried this code:

def crop_image_only_outside(img,tol=0):
    # tol  is tolerance
    mask = img>tol
    mask = mask.all(3)
    print(mask.shape)
    m,n,s = mask.shape
    mask0,mask1,mask2 = mask.any(0),mask.any(1),mask.any(2)
    col_start,col_end = mask0.argmax(),n-mask0[::-1].argmax()
    row_start,row_end = mask1.argmax(),m-mask1[::-1].argmax()
    s_start,s_end = mask2.argmax(),s-mask2[::-1].argmax()
    return img[row_start:row_end,col_start:col_end,s_start:s_end]

but results gives me: TypeError: Invalid shape (0, 0, 0, 4) for image data

hint: I'm working on brats dataset

  • You need to give more information. What do the different axes represent in data? For example, is it (example, imgy, imgx, channels)? When you say non-zero, are all the channels padded the same for every example? If not, are you trying to crop to the smallest example such that no zeros exist in any? – Will.Evo Apr 19 '21 at 19:37
  • The first 3 dimensions are the X, Y, and Z values for each point in the 3D volume, which is commonly called a voxel. The 4th dimension is the values for 4 different sequences 0: FLAIR: "Fluid Attenuated Inversion Recovery" (FLAIR) 1: T1w: "T1-weighted" 2: t1gd: "T1-weighted with gadolinium contrast enhancement" (T1-Gd) 3: T2w: "T2-weighted" ......................................... yes, I'm trying to eliminate the black borders of the photo – Salma Ayman Apr 19 '21 at 19:59

0 Answers0