use .png images

1,278 views
Skip to first unread message

Pranita Pradhan

unread,
Jun 2, 2017, 4:11:56 AM6/2/17
to pyradiomics
Hello people,

I am very new to pyradiomics and also to simple itk so I have some basic question. I intend to calculate first order features for every pixel in a RGB image. For every pixel a window of 10*10 neighbors is chosen and features should be calculated.  
Can I use .png files? I mean the example videos show usage of .nrrd files. The size of image will be 10*10*3 and of mask will be 10*10. I tried doing what was shown in documentation on .png files something like this:

image = sitk.ReadImage('ID_000101_ContrastAdjust.png')
mask = sitk.ReadImage('ID_000101_MaskBackground.png')

firstOrderFeatures = firstorder.RadiomicsFirstOrder(image, mask)

energy = firstOrderFeatures.getEnergyFeatureValue()
m = firstOrderFeatures.getMeanFeatureValue()
totalenergy = firstOrderFeatures.getTotalEnergyFeatureValue()
entropy = firstOrderFeatures.getEntropyFeatureValue()
median = firstOrderFeatures.getMedianFeatureValue()
std = firstOrderFeatures.getStandardDeviationFeatureValue()
skew = firstOrderFeatures.getSkewnessFeatureValue(axis=0)
uniformity = firstOrderFeatures.getUniformityFeatureValue()

This does not work.I always get some error or nan values. What is wrong going in here?

Waiting for reply.



Andrey Fedorov

unread,
Jun 2, 2017, 8:55:25 AM6/2/17
to pyradiomics
Pyradiomics requires the input image to be scalar valued. RGB has a vector of 3 values a every pixel, and is not an acceptable input image type.

Andrey Fedorov

unread,
Jun 6, 2017, 4:41:03 PM6/6/17
to Pranita Pradhan, pyradiomics
Pranita,

please post responses to the list instead of private messages, this
way other developers can help, and also other users can find this
answer.

If you are trying to process a 10x10 image, it may be too small. Have
you tried larger image size?

Andrey

On Tue, Jun 6, 2017 at 6:58 AM, Pranita Pradhan
<pranitap...@gmail.com> wrote:
> Hello Andrey,
> Thank you for your reply.
> What do you mean by scalar valued? I don't use vector of R,G,B for every
> pixel rather 10*10 window of just one channel (R/G/B) at a time. So
> basically the input is a gray scale subimage of original image.
>
> Regards
> Pranita

Joost van Griethuysen

unread,
Jun 13, 2017, 3:13:50 AM6/13/17
to pyradiomics, pranitap...@gmail.com
Pranita, Andrey,

In addition, I believe that if you load images in .png format using SimpleITK, it will load it as a 2D image, which is not accepted by PyRadiomics (2D images should be loaded as 3D images, with size 1 in the 3rd dimension).
You can use SimpleITK.JoinSeries to create these "3D" images from 2D. See also this issue on the github.

Joost

Op dinsdag 6 juni 2017 22:41:03 UTC+2 schreef Andrey Fedorov:

Andrey Fedorov

unread,
Jun 13, 2017, 6:34:38 PM6/13/17
to pyradiomics, pranitap...@gmail.com
> In addition, I believe that if you load images in .png format using SimpleITK, it will load it as a 2D image

Joost, wouldn't it make sense to detect this situation and convert single slice 2D image into a 3D image automatically?

Joost van Griethuysen

unread,
Jun 14, 2017, 2:43:56 AM6/14/17
to pyradiomics, pranitap...@gmail.com
I'm not sure, as it will be imperfect anyway

in case of .png you are also missing additional information, such as origin, spacing and direction. Although this does not produce errors, it does make the values of calculated shape features and firstorder:TotalEnergy to be likely erroneous.

We could add a check and convert if necessary, but log a warning if this happens.

Op woensdag 14 juni 2017 00:34:38 UTC+2 schreef Andrey Fedorov:

Pranita Pradhan

unread,
Jun 14, 2017, 2:58:59 AM6/14/17
to Joost van Griethuysen, pyradiomics
Yes, I tried to convert .png images to 3D as suggested. And then when I calculated mean I got nan value. 

Regards 
Pranita

Joost van Griethuysen

unread,
Jun 14, 2017, 3:00:35 AM6/14/17
to pyradiomics, j.v.gri...@nki.nl
Can you run with debug logging enabled and share the log?


Joost

Op woensdag 14 juni 2017 08:58:59 UTC+2 schreef Pranita Pradhan:

Pranita Pradhan

unread,
Jun 14, 2017, 5:49:49 AM6/14/17
to Joost van Griethuysen, pyradiomics, andrey....@gmail.com
I really appreciate this help. I am just doing this. Can you see and where I am going wrong? I ma also attaching the images, probably you could take a look at it.​​​

imageName = 'C:/Users/si62qit/Documents/PhDJenaPranita/Analysis/4_Contrast adjustment/ID_000101_ContrastAdjust.png'
maskName = 'C:/Users/si62qit/Documents/PhDJenaPranita/Analysis/3_Background/ID_000101_MaskBackground.png'

image = sitk.ReadImage(imageName)
image3d = sitk.JoinSeries(image)
mask = sitk.ReadImage(maskName)

firstOrderFeatures = firstorder.RadiomicsFirstOrder(image3d,mask)
m = firstOrderFeatures.getMeanFeatureValue()


Waiting for reply.

Regards
Pranita

Have a nice day.
Regards
P. Pranita

Joost van Griethuysen

unread,
Jun 14, 2017, 10:46:53 AM6/14/17
to pyradiomics, j.v.gri...@nki.nl, andrey....@gmail.com
You're first file (the image) is a vector image, with vector size 3 (this would be the RGB channels) PyRadiomics currently does not accept this, and I suspect this is causing your error. I will investigate further.

Joost

Op woensdag 14 juni 2017 11:49:49 UTC+2 schreef Pranita Pradhan:

Joost van Griethuysen

unread,
Jun 16, 2017, 3:29:22 AM6/16/17
to pyradiomics, j.v.gri...@nki.nl, andrey....@gmail.com
Dear Pranita,

I tested your images, and you can extract a mean value. The reason it did not work in your case was because you did not update the default label value. The value of segmented voxels in your image is 255, not 1. Furthermore, you must also update your mask volume, this has the same requirements as the image

This code works (changes compared to your code are bold):

imageName = 'C:/Users/si62qit/Documents/PhDJenaPranita/Analysis/4_Contrast adjustment/ID_000101_ContrastAdjust.png'
maskName = 'C:/Users/si62qit/Documents/PhDJenaPranita/Analysis/3_Background/ID_000101_MaskBackground.png'

image = sitk.ReadImage(imageName)
image3d = sitk.JoinSeries(image)
mask = sitk.ReadImage(maskName)
mask3d = sitk.JoinSeries(mask)

firstOrderFeatures = firstorder.RadiomicsFirstOrder(image3d,mask, label=255)
m = firstOrderFeatures.getMeanFeatureValue()


Be careful though, PyRadiomics is currently not developed for handling vector images and unexpected behaviour may occur! For example, in case of Mean, it wil return the average intensity for all 3 channels (i.e. returns just one value), but for Energy, it will return the Energy for each separate channel (i.e. returns a vector of 3 values). Moreover, I don't know, but I suspect textureclasses will not compute correcty.

Also, you can't use the featureextractor module, as it contains the mask checks, which currently fail for vectorized images.

Regards,

Joost

Op woensdag 14 juni 2017 11:49:49 UTC+2 schreef Pranita Pradhan:
I really appreciate this help. I am just doing this. Can you see and where I am going wrong? I ma also attaching the images, probably you could take a look at it.​​​

Joost van Griethuysen

unread,
Jun 16, 2017, 3:30:11 AM6/16/17
to pyradiomics, j.v.gri...@nki.nl, andrey....@gmail.com
Sorry, small error in the code, here is the correct one:

imageName = 'C:/Users/si62qit/Documents/PhDJenaPranita/Analysis/4_Contrast adjustment/ID_000101_ContrastAdjust.png'
maskName = 'C:/Users/si62qit/Documents/PhDJenaPranita/Analysis/3_Background/ID_000101_MaskBackground.png'

image = sitk.ReadImage(imageName)
image3d = sitk.JoinSeries(image)
mask = sitk.ReadImage(maskName)
mask3d = sitk.JoinSeries(mask)

firstOrderFeatures = firstorder.RadiomicsFirstOrder(image3d,mask3d, label=255)
m = firstOrderFeatures.getMeanFeatureValue()

Op vrijdag 16 juni 2017 09:29:22 UTC+2 schreef Joost van Griethuysen:

r.z.h...@gmail.com

unread,
Apr 25, 2018, 4:27:52 PM4/25/18
to pyradiomics
Dear Joost, Pranita,

I am trying the same thing, but then for the grey-level co-occurence (glcm) matrix features.
The code Joost suggested works for first order features with my RGB image (480x480x3) and mask (480x480).
Although trying to extract the glcm features, I keep getting: 
-----------------------------------
    self.kwargs.get('force2Ddimension', 0))

RuntimeError: Expected a 3D array for image and mask.
-----------------------------------

Where I tried using .png and .nrrd  files for the images:

-----------------------------------
imageName= r'C:\Users\ruben\Documents\Master\SCRIPTIE\Images\Images_batch_2\Image_Script\2637932_image_0.nrrd'
maskName=  r'C:\Users\ruben\Documents\Master\SCRIPTIE\Images\Images_batch_2\Image_Script\2637932_mask_0.nrrd'
#imageName= r'C:\Users\ruben\Documents\Master\SCRIPTIE\Images\Images_batch_2\Image_Script\2637932_image_0.png'
#maskName=  r'C:\Users\ruben\Documents\Master\SCRIPTIE\Images\Images_batch_2\Image_Script\2637932_mask_0.png'


image = sitk.ReadImage(imageName)
#image3d = sitk.JoinSeries(image)
mask = sitk.ReadImage(maskName)
#mask3d = sitk.JoinSeries(mask)

#glcmFeatures = glcm.RadiomicsGLCM(image3d,mask3d, label=255,binWidth=3)
glcmFeatures = glcm.RadiomicsGLCM(image,mask, label=255)
m = glcmFeatures.getAutocorrelationFeatureValue()

--------------------------------------------

I don't seem to get a solution...
Any help would be really appreciated!


Kind regards,

Ruben Hekster

Joost van Griethuysen

unread,
Apr 26, 2018, 4:29:08 AM4/26/18
to pyradiomics
What is the result of 
print(image)
print(mask)

I suspect it to be similar to what is mentioned in this thread, i.e. that your mask and/or image loads as a 2D Image, which is not accepted by PyRadiomics. In the code you showed, JoinSeries was commented out (#), which will not convert it to 3D. Furthermore, your RGB image has 3 color channels, where PyRadiomics is designed for scalar values. How do you want to handle that? In firstorder it's easier to ignore, you can just take the mean over all channels for example. However, for GLCM that is not so easy, how do you define the relationship between color channels?

KR,

Joost

Op woensdag 25 april 2018 22:27:52 UTC+2 schreef r.z.h...@gmail.com:

as...@walla.co.il

unread,
Jan 1, 2020, 1:24:28 AM1/1/20
to pyradiomics
Can you please load the images.zip again.


On Wednesday, June 14, 2017 at 12:49:49 PM UTC+3, Pranita Pradhan wrote:
I really appreciate this help. I am just doing this. Can you see and where I am going wrong? I ma also attaching the images, probably you could take a look at it.​​​

imageName = 'C:/Users/si62qit/Documents/PhDJenaPranita/Analysis/4_Contrast adjustment/ID_000101_ContrastAdjust.png'
maskName = 'C:/Users/si62qit/Documents/PhDJenaPranita/Analysis/3_Background/ID_000101_MaskBackground.png'

image = sitk.ReadImage(imageName)
image3d = sitk.JoinSeries(image)
mask = sitk.ReadImage(maskName)

firstOrderFeatures = firstorder.RadiomicsFirstOrder(image3d,mask)
m = firstOrderFeatures.getMeanFeatureValue()


Waiting for reply.

Regards
Pranita

Have a nice day.
Regards
P. Pranita

On 14 June 2017 at 08:58, Pranita Pradhan <pranitap...@gmail.com> wrote:
Yes, I tried to convert .png images to 3D as suggested. And then when I calculated mean I got nan value. 

Regards 
Pranita
Reply all
Reply to author
Forward
0 new messages