0

a seg img A - 3d numpy array image, shape (150, 418, 406), only consists of two values 0, 1
resized img B - the resized version of the above (160, 192, 128), many array values [-0.26 ~ 1.28 ]

The resizing process was run by this code:

def resize(img, shape, mode='constant', orig_shape=None):
    """
    Wrapper for scipy.ndimage.zoom suited for MRI images.
    """

    if orig_shape == None: orig_shape = img.shape

    assert len(shape) == 3, "Can not have more than 3 dimensions"
    factors = (
        shape[0]/orig_shape[0],
        shape[1]/orig_shape[1], 
        shape[2]/orig_shape[2]
    )

    # Resize to the given shape
    return zoom(img, factors, mode=mode)

And the below images are the 80th slices of each 3d image. (A, B)

img A img B

I want to just downsample mri segmentation images and don't want to the weird modification of the images. Any help would be appreciated.

Crispy13
  • 230
  • 1
  • 3
  • 16

1 Answers1

1

For the fact that you get values different from 0 & 1, I am not too sure because I cannot manage to reproduce, but potentially setting order=0 in the call to zoom could solve it.

Then, for your images comparison, I am pretty sure you are not comparing comparable images. When you call zoom, the boundaries of your image stay the same, but the number of sample points changes. As a consequence, in the case of a matrix representation, the shape of the matrix changes, in your case from (150, 418, 406) to (160, 192, 128). As a result, accessing the 80th element along the first axis of the original image does not map to accessing the 80th element along the first axis of the newly generated image. It would actually be closer to 80 / 150 * 160, which is roughly 85. As an example, see the snippet and figure below. Notice how the images in the first row are comparable, but not the ones in the second row. Applying the correction I just mentioned, images in the third row become comparable.

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import zoom

image = np.random.default_rng(0).integers(2, size=(100, 25, 25))
imageZ = zoom(image, zoom=(1.2, 0.8, 0.8), order=0)
fig, axarr = plt.subplots(nrows=3, ncols=2, figsize=(8, 12))
axarr[0, 0].imshow(image[-1, :, :], cmap='binary')
axarr[0, 1].imshow(imageZ[-1, :, :], cmap='binary')
axarr[1, 0].imshow(image[70, :, :], cmap='binary')
axarr[1, 1].imshow(imageZ[70, :, :], cmap='binary')
axarr[2, 0].imshow(image[70, :, :], cmap='binary')
axarr[2, 1].imshow(imageZ[84, :, :], cmap='binary')
fig.savefig('so.png', bbox_inches='tight', dpi=300)

enter image description here

Patol75
  • 4,342
  • 1
  • 17
  • 28
  • Thank you. Setting order to 0 solved this problem. Could you give me a link that explains the reason? – Crispy13 Dec 12 '19 at 01:07
  • No worries. Please do also consider the change in index when the array is resized, as it matters. For explanations, see [Wikipedia](https://www.wikiwand.com/en/B-spline) and [Scipy Tutorial](https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html#spline-interpolation). Finally, please [upvote and accept](https://stackoverflow.com/help/someone-answers) my answer if you found it useful. – Patol75 Dec 12 '19 at 02:10
  • I posted a new question related to this post, It would be glad if you help me. https://stackoverflow.com/questions/61953238/any-recommendation-to-resize-3d-image-array-which-consists-of-only-0-and-1 – Crispy13 May 22 '20 at 10:56