# MIP

**URL:** https://discourse.itk.org/t/mip/4521
**Category:** Beginner Questions
**Tags:** python, nifti, simpleitk
**Created:** [October 25, 2021, 2:16pm UTC](https://discourse.itk.org/t/mip/4521 "2021-10-25T14:16:43Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Aleena\_Suhail](https://discourse.itk.org/user_avatar/discourse.itk.org/aleena_suhail/32/2335_2.png) [@Aleena\_Suhail](https://discourse.itk.org/u/Aleena_Suhail)
#### Post date: [October 25, 2021, 2:16pm UTC](https://discourse.itk.org/t/mip/4521/1 "2021-10-25T14:16:43Z")

</div>

Hi  
I am trying to generate MIP for 3d brain mra images for aneurysm detection however I am struggling with which libraries to use and how to achieve this with python code

---

<div class="post-metadata">

### Author: ![Aleena\_Suhail](https://discourse.itk.org/user_avatar/discourse.itk.org/aleena_suhail/32/2335_2.png) [@Aleena\_Suhail](https://discourse.itk.org/u/Aleena_Suhail)
#### Post date: [October 25, 2021, 2:40pm UTC](https://discourse.itk.org/t/mip/4521/2 "2021-10-25T14:40:45Z")

</div>

![Screenshot 2021-10-25 at 15.39.45](https://discourse.itk.org/uploads/default/original/2X/3/367e7481f48243e8697dd5507eb6df16f658560c.jpeg)

```auto
import os

import numpy as np
import SimpleITK as sitk

def make_mips(image_path, output_dir):
    image = sitk.ReadImage(image_path)
    image_size = image.GetSize()

    basename = os.path.basename(image_path)
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)

    for dim in range(3):
        projection = sitk.MaximumProjection(image, dim)

        if image_size[dim] % 2: # odd number
            voxel = [0, 0, 0]
            voxel[dim] = (image_size[dim] - 1) / 2
            origin = image.TransformIndexToPhysicalPoint(voxel)
        else: # even
            voxel1 = np.array([0, 0, 0], int)
            voxel2 = np.array([0, 0, 0], int)
            voxel1[dim] = image_size[dim] / 2 - 1
            voxel2[dim] = image_size[dim] / 2
            point1 = np.array(image.TransformIndexToPhysicalPoint(voxel1.tolist()))
            point2 = np.array(image.TransformIndexToPhysicalPoint(voxel2.tolist()))
            origin = np.mean(np.vstack((point1, point2)), 0)
        projection.SetOrigin(origin)
        projection.SetDirection(image.GetDirection())
        proj_basename = basename.replace('.nii.gz', '_mip_{}.nii.gz'.format(dim))
        sitk.WriteImage(projection, os.path.join(output_dir, proj_basenam

```

this code is generating blank images and not the MIP for brain vessels

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [October 25, 2021, 2:41pm UTC](https://discourse.itk.org/t/mip/4521/3 "2021-10-25T14:41:35Z")

</div>

Hello @Aleena_Suhail,

Welcome to SimpleITK!

There are a variety of projection filters ([MaximumProjectionImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1MaximumProjectionImageFilter.html) is one of them). Note that the projection filters do not change the dimensionality of the result.

```auto
import SimpleITK as sitk

image = sitk.ReadImage('training_001_ct.mha')

# projection maintains original image dimensionality so output is [size_x,size_y,1]
# so we need to extract a 2D version
mip_axial = sitk.MaximumProjection(image, projectionDimension=2)[:,:,0]
sitk.Show(mip_axial, 'axial MIP')

```

---

<div class="post-metadata">

### Author: ![Aleena\_Suhail](https://discourse.itk.org/user_avatar/discourse.itk.org/aleena_suhail/32/2335_2.png) [@Aleena\_Suhail](https://discourse.itk.org/u/Aleena_Suhail)
#### Post date: [October 25, 2021, 2:43pm UTC](https://discourse.itk.org/t/mip/4521/4 "2021-10-25T14:43:43Z")

</div>

> [@zivy](#):
>
> ```auto
> import SimpleITK as sitk
> 
> image = sitk.ReadImage('training_001_ct.mha')
> 
> # projection maintains original image dimensionality so output is [size_x,size_y,1]
> # so we need to extract a 2D version
> mip_axial = sitk.MaximumProjection(image, projectionDimension=2)[:,:,0]
> sitk.Show(mip_axial, 'axial MIP')
> 
> ```

I am getting Exception thrown in SimpleITK Show: /tmp/SimpleITK/Code/IO/src/sitkImageViewer.cxx:620:  
sitk::ERROR: No ImageJ/Fiji application found.  
this error

---

<div class="post-metadata">

### Author: ![Aleena\_Suhail](https://discourse.itk.org/user_avatar/discourse.itk.org/aleena_suhail/32/2335_2.png) [@Aleena\_Suhail](https://discourse.itk.org/u/Aleena_Suhail)
#### Post date: [October 25, 2021, 2:45pm UTC](https://discourse.itk.org/t/mip/4521/5 "2021-10-25T14:45:16Z")

</div>

Is there a way to download the MIP as png format and view it without using imagj

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [October 25, 2021, 2:49pm UTC](https://discourse.itk.org/t/mip/4521/6 "2021-10-25T14:49:53Z")

</div>

Yes, just write it out (requires rescaling the intensities to [0,255]). Replace the `sitk.Show` with:

```auto
sitk.WriteImage(sitk.Cast(sitk.RescaleIntensity(mip_axial), sitk.sitkUInt8), 'axialMIP.png')

```

---

<div class="post-metadata">

### Author: ![Aleena\_Suhail](https://discourse.itk.org/user_avatar/discourse.itk.org/aleena_suhail/32/2335_2.png) [@Aleena\_Suhail](https://discourse.itk.org/u/Aleena_Suhail)
#### Post date: [October 25, 2021, 2:59pm UTC](https://discourse.itk.org/t/mip/4521/7 "2021-10-25T14:59:30Z")

</div>

```auto
import SimpleITK as sitk

image = sitk.ReadImage('/content/drive/MyDrive/ADAM-Filtered-Data/10068F_TOF.nii.gz')

# projection maintains original image dimensionality so output is [size_x,size_y,1]

# so we need to extract a 2D version

mip_axial = sitk.MaximumProjection(image, projectionDimension=2)[:,:,0]

sitk.WriteImage(sitk.Cast(sitk.RescaleIntensity(mip_axial), sitk.sitkUInt8), 'axialMIP.png')

image = sitk.ReadImage("axialMIP.png”)
plt.imshow(image, cmap='gray')

plt.show

```

with this code I am getting the following error:

```nohighlight
TypeError Traceback (most recent call last)

[<ipython-input-49-5973988eeef4>](https://xgeiymqwb-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20211020-060059-RC00_404451640#) in <module>() ----> 1 plt.imshow(image, cmap='gray') 2 plt.show

5 frames

[/usr/local/lib/python3.7/dist-packages/matplotlib/image.py](https://xgeiymqwb-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20211020-060059-RC00_404451640#) in set_data(self, A) 697 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]): 698 raise TypeError("Invalid shape {} for image data" --> 699 .format(self._A.shape)) 700 701 if self._A.ndim == 3:

TypeError: Invalid shape (1048576,) for image data

```

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [October 25, 2021, 3:11pm UTC](https://discourse.itk.org/t/mip/4521/8 "2021-10-25T15:11:14Z")

</div>

A SimpleITK image is not a numpy array.

You can get a numpy array via the `sitk.GetArrayFromImage(image)`.

---

<div class="post-metadata">

### Author: ![Aleena\_Suhail](https://discourse.itk.org/user_avatar/discourse.itk.org/aleena_suhail/32/2335_2.png) [@Aleena\_Suhail](https://discourse.itk.org/u/Aleena_Suhail)
#### Post date: [October 25, 2021, 4:07pm UTC](https://discourse.itk.org/t/mip/4521/9 "2021-10-25T16:07:45Z")

</div>

> [@Aleena\_Suhail](#):
>
> plt.imshow(image, cmap=‘gray’)
> 
> plt.show

Thank you so much I have been able to generate MIP for one image I am trying to do the same for the whole dataset The code is executing without any error however it is not generating anything

 ![Screenshot 2021-10-25 at 17.05.33](https://discourse.itk.org/uploads/default/original/2X/4/448602cfe71bb0974cbed0b090e11eacd70e1955.jpeg)

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [October 25, 2021, 4:50pm UTC](https://discourse.itk.org/t/mip/4521/10 "2021-10-25T16:50:19Z")

</div>

This sounds like a Python programming problem, not ITK/SimpleITK. From the screenshot it appears that the WriteImage isn’t using a valid syntax. Possibly try:

```auto
sitk.WriteImage(sitk.Cast(sitk.RescaleIntensity(mip_axial),sitk.sitkUInt8), f'axialMIP{index}.png')

```

For future reference, please do not use screenshots for sharing code. Share a minimal working example of the code illustrating the specific problem.

---

<div class="post-metadata">

### Author: ![Aleena\_Suhail](https://discourse.itk.org/user_avatar/discourse.itk.org/aleena_suhail/32/2335_2.png) [@Aleena\_Suhail](https://discourse.itk.org/u/Aleena_Suhail)
#### Post date: [October 25, 2021, 9:01pm UTC](https://discourse.itk.org/t/mip/4521/11 "2021-10-25T21:01:01Z")

</div>

dir = ‘content/drive/MyDrive/ADAM-Filtered-Data/’  
output=’/content/drive/MyDrive/Slices’  
for index,file\_name in enumerate(sorted(os.listdir(’/content/drive/MyDrive/ADAM-Filtered-Data’))):  
if file\_name.endswith(‘TOF.nii.gz’):  
image=sitk.ReadImage(’/content/drive/MyDrive/ADAM-Filtered-Data/’ + file\_name)  
mip\_axial = sitk.MaximumProjection(image, projectionDimension=2)[:,:,0]  
sitk.WriteImage(sitk.Cast(sitk.RescaleIntensity(mip\_axial),sitk.sitkUInt8),output, f’axialMIP{index}.png’)

How can I modify this code so that the images are written in a particular file called ‘output’

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [October 25, 2021, 9:29pm UTC](https://discourse.itk.org/t/mip/4521/12 "2021-10-25T21:29:46Z")

</div>

Hello @Aleena_Suhail,

As the files are written in a loop, using the same file name will cause the images to overwrite each other, so in the end only the last image will be saved. If you want the file name to have a prefix of `output` instead of `axialIMIP` then just replace the text.

Note that this forum is for ITK/SimpleITK questions and not for general Python questions, which is what you appear to need. Please post Python questions in a more appropriate forum ([stack overflow](https://stackoverflow.com/questions/tagged/python)).
