I’m trying to write this code in Python, but i don’t know what i have to do when i have to create an iterator to build the mask, because i don’t know what are the values of the array created from mha file.
I’m using NumPy to access the file, but i don’t understand what represents elements in the array that i get with array = itk.GetArrayViewFromImage(outputImage)
Array elements are intensities of pixels in the image. Try reading a super small image (lets say 3x4x5) with distinctive values, and then access that via itk.GetArrayViewFromImage.
Thank you so much, but when i have to use TransformIndexToPhysicalPoint what i have to put to replace maskIt.GetIndex() ?
EDIT: the first value of the matrix is the slice number, the second is the y axe, the third is the x axe and all the elements cointaned in the matrix are the pixel values
pixelIndex=itk.Index[2] ()
for z in range (array.shape[0]):#slice
for y in range (array.shape[1]): #Y axes
for x in range (array.shape[2]): #X axes
pixelIndex[0]=x
pixelIndex[1]=y
point=ImageType.New()
outputImage.TransformIndexToPhysicalPoint(pixelIndex,point)
if (maskInterpolator.IsInsideBuffer(point)):
if (maskInterpolator.Evaluate(point) == 0):
maskIt.Set(backgroundValue)
else:
maskIt.Set(foregroundValue)
else:
maskIt.Set(backgroundValue)
@blowekamp and @dzenanz have good suggestions – the result will be simpler and faster if itk.resample_image_filter with use_reference_image=True and reference_image=input_image followed by a call to itk.binary_threshold_image_filter.
It’s a little bit different from your interpretation because i have to check if a pixel is inside or outside a ROI contained in a TIF as an image sequence. I have already created an interpolation with itk.NearestNeighborInterpolateImageFunction so now i have to check if a point is inside or outside to create the mask
I followed your suggestions but i have a problem because the mask and the input image don’t occupy the same physical space, despite having assigned them identical size,origin etc. It seems that the value of the mask resets in the function MaskImageFilter
maskImage=flipFilter.GetOutput()
maskImage.SetOrigin(reader4mm.GetOutput().GetOrigin()) >>>Assignation values
maskImage.SetSpacing(reader4mm.GetOutput().GetSpacing())
maskImage.SetDirection(reader4mm.GetOutput().GetDirection()) #create the final mask
MaskFilterType=itk.MaskImageFilter[LabeledImageType,MaskImageType,LabeledImageType]
maskFilter=MaskFilterType.New()
maskFilter.SetInput(outputImage)
maskFilter.SetMaskImage(maskImage) >>>Mask seems to reset values
WriterType=itk.ImageFileWriter[LabeledImageType]
writer=WriterType.New()
writer.SetFileName(path+“/mask.mha”)
writer.SetInput(maskFilter.GetOutput())
try:
writer.Update();
except Exception as error:
print(“EXIT FAILURE”)
print(error)
sys.exit(1)
print(“EXIT SUCCESS”)
but it says that AttributeError: 'itkImageUC3' object has no attribute 'ChangeSpacingOn'.
It may be helpful to say that i used flipFilter (itk.FlipImageFilter) to change the axis of the mask and that the mask should have the same size of the output image [256,256,50] (so i have to set: origin, direction and spacing)
EDIT: i tried to not flip the axes with flipFilter and to mantain