ROI Intensity using Python


I want to calculate average pet intensity within given ROI. I have two images: (1) PET registered on CT and (2) ROI registered on CT. I can do this in MATLAB but I want to try with python. Does anyone have python solution similar to this? I am new to python.

Here is the MATLAB code:

pet_image = niftiread(‘pet_on_ct.nii.gz’);
roi_image = niftiread(‘ROI_MNI_on_ct.nii.gz’);
index_all = unique(roi_image(:));
for i = 1:length(index_all)
index = index_all(i)
result=[result; mean(pet_intensity_within_given_roi)];


have you solved it? i also want to know and use it

I use nipype with fslstats Neuroimaging in Python - Pipelines and Interfaces — nipy pipeline and interfaces package ImageStats with -K -M. Not pure python, but easy to use.

could you provide the python code for roi extraction of pet

from nipype.interfaces.fsl import ImageStats
stats = ImageStats()
stats.inputs.in_file = 'pet.nii.gz'
stats.inputs.op_string = '-M'
stats.intputs.index_mask_file = 'mask.nii.gz'
result =

Thx very much,, i will try later… if any questions i will leave a message… thx