VansFannel
VansFannel

Reputation: 45921

Is this a right Numpy reshape?

I've just started with Python and Numpy.

I have found this piece of code:

def preprocessing(FLAIR_array, T1_array):

    brain_mask = np.ndarray(np.shape(FLAIR_array), dtype=np.float32)
    brain_mask[FLAIR_array >=thresh] = 1
    brain_mask[FLAIR_array < thresh] = 0
    for iii in range(np.shape(FLAIR_array)[0]):
        brain_mask[iii,:,:] = scipy.ndimage.morphology.binary_fill_holes(brain_mask[iii,:,:])  #fill the holes inside brain

    FLAIR_array -=np.mean(FLAIR_array[brain_mask == 1])      #Gaussion Normalization
    FLAIR_array /=np.std(FLAIR_array[brain_mask == 1])

    rows_o = np.shape(FLAIR_array)[1]
    cols_o = np.shape(FLAIR_array)[2]
    FLAIR_array = FLAIR_array[:, int((rows_o-rows_standard)/2):int((rows_o-rows_standard)/2)+rows_standard, int((cols_o-cols_standard)/2):int((cols_o-cols_standard)/2)+cols_standard]

What are they doing in the last line? In this one:

FLAIR_array[:, int((rows_o-rows_standard)/2):int((rows_o-rows_standard)/2)+rows_standard, int((cols_o-cols_standard)/2):int((cols_o-cols_standard)/2)+cols_standard]

FLAIR_array has this shape: [48,240,240].

48 is the number of images.
240, 240 is its height and witdh.

Or maybe, they are slicing it.

Upvotes: 1

Views: 82

Answers (2)

Feelx234
Feelx234

Reputation: 330

Hard to tell, as rows standard is not defined inside the function. But if you rewrite it as (dropping some of the int(..) to increase readability)

rows_center = int(rows_o/2)
cols_center = int(cols_o/2)
delta_rows = int(rows_standard)
delta_cols = int(cols_standard)
FLAIR_array = FLAIR_array[:, rows_center - rows_delta/2:rows_center + rows_delta/2, cols_center - cols_delta/2:cols_center + cols_delta/2]

It seems that they are extracting for each image a small crop centered at rows_center and cols_center with the number of rows and columns equal to delta_rows, delta_cols

Upvotes: 1

Balraj Ashwath
Balraj Ashwath

Reputation: 1427

Yes, they're only performing Numpy slicing (and not reshaping) on FLAIR_array whose resultant dimensions will be:

  • All elements in the 0th dimension are retained from the original array (as indicated by :)
  • Elements int((rows_o-rows_standard)/2) to int((rows_o-rows_standard)/2)+rows_standard - 1 are used from 1st dimension from the original array
  • Elements int((cols_o-cols_standard)/2) to int((cols_o-cols_standard)/2)+cols_standard - 1 are used from 2nd dimension from the original array

Upvotes: 1

Related Questions