Hi Diana,
I agree with Cam, it's the same workflow we use in our team. Still, if that is not possible, you can use your square ROIs. However, I'd suggest a slightly different approach (which you can can do in Python).
The are some slight problems with your current approach:
You have a mask of numpy.ones, but it's dimensions are different from the image ((20, 12, 5) vs (256, 256, 25)), and no geometric information (origin, spacing and direction). Therefore, your computer cannot know where to put your square ROI in the image.
Even if you were to copy the image's geometric information, the ROI would end up in the corner of your original image, hardly near the point of interest. PyRadiomics would be able to extract features, but these would be meaningless.
So if you have an image and want to draw a square box around a point for which you have index x, y, z (index, so starting at 0), you can use the following code to build a mask NRRD file:
import SimpleITK as sitk
import numpy as np
im = sitk.ReadImage(r'path/to/image')
point = (x, y, z) # fill in the index of your point here
roi_size = (19, 11, 5) # x, y, z; uneven to ensure the point is really the center of your ROI
im_size = im.GetSize()[::-1] # size in z, y, x, needed because the arrays obtained from the image are oriented in z, y, x
ma_arr = np.zeros(im_size, dtype='uint8')
# Compute lower and upper bound of the ROI
L_x = point[0] - int((roi_size[0] - 1) / 2)
L_y = point[1] - int((roi_size[1] - 1) / 2)
L_z = point[2] - int((roi_size[2] - 1) / 2)
U_x = point[0] + int((roi_size[0] - 1) / 2)
U_y = point[1] + int((roi_size[1] - 1) / 2)
U_z = point[2] + int((roi_size[2] - 1) / 2)
# ensure the ROI stays within the image bounds
L_x = max(0, L_x)
U_x = min(im_size[2] - 1, U_x)
U_y = min(im_size[1] - 1, U_y)
U_z = min(im_size[0] - 1, U_z)
# 'segment' the mask
ma_arr[L_z:U_z + 1, L_y:U_y + 1, L_x:U_x+1] = 1 # Add + 1 to each slice, as slicing is done from lower bound to, but not including, upper bound. Because we do want to include our upper bound, add + 1
ma = sitk.GetImageFromArray(ma_arr)
ma.CopyInformation(im) # This copies the geometric information, ensuring image and mask are aligned. This works, because image and mask have the same size of the pixel array
sitk.WriteImage(ma, r'path/to/mask.nrrd', True) # don't forget the extension!, True specifies it can be compressed for storage (only used for masks)
Regards,
Joost
Op vrijdag 21 september 2018 05:17:20 UTC+2 schreef cam cushingc: