def itkFilter(array, filterType, kernelSize=20, radius=1, sigma=1): """Executes ITK filter on numpy array and returns result as numpy array""" InternalPixelType = itk.F Dimension = len(array.shape) ImageType = itk.Image[InternalPixelType, Dimension] #image2d = converter.GetImageFromArray(array2d) image = numpyToItk(array) kernel = itk.strel(Dimension, kernelSize) if filterType == 'GrayscaleDilate': filter = itk.GrayscaleDilateImageFilter[ImageType, ImageType, kernel].New( image, Kernel=kernel) elif filterType == 'GrayscaleErode': filter = itk.GrayscaleErodeImageFilter[ImageType, ImageType, kernel].New( image, Kernel=kernel) elif filterType == 'Median': filter = itk.MedianImageFilter[ImageType, ImageType].New( image, Radius=radius) elif filterType == 'SmoothingRecursiveGaussian': filter = itk.SmoothingRecursiveGaussianImageFilter[ImageType, ImageType].New( image, Sigma=sigma) else: raise Exception, "Invalid filter type: %s" % filterType filter.Update() #outputVolume[:,:,z] = converter.GetArrayFromImage(dilateFilter.GetOutput()) return itkToNumpy(filter.GetOutput())
import ITKCommon import ITKBinaryMathematicalMorphology import ITKImageStatistics import ITKSmoothing import ITKDistanceMap import ITKImageIntensity import ITKIOImageBase import ITKThresholding import ITKImageGrid PType = itk.UC dim = 2 IType = itk.Image[PType, dim] kernel = itk.strel(2, 1) # create the reader reader = itk.ImageFileReader[IType].New(FileName=sys.argv[1]) sources = [] image = ITKCommon.Image[PType, dim].New() r = itk.ImageRegion._2() r.SetSize((10, 10)) image.SetRegions(r) image.Allocate() sources.append(("ITKCommon", image)) sources.append(("ITKIOImageBase", reader.GetOutput()))
except KeyError: pass # test image assert repr(itk.image(reader)) == repr(reader.GetOutput().GetPointer()) assert repr(itk.image(reader.GetOutput())) == repr(reader.GetOutput().GetPointer()) assert repr(itk.image(reader.GetOutput().GetPointer())) == repr(reader.GetOutput().GetPointer()) assert itk.image(1) == 1 # test strel # should work with the image type, an image instance or a filter # and should work with a list, a tuple, an int or an itk.Size for s in [2, (2, 2), [2, 2], itk.Size[2](2)] : st = itk.strel(dim, s) (tpl, param) = itk.template(st) assert tpl == itk.FlatStructuringElement assert param[0] == dim assert st.GetRadius().GetElement(0) == st.GetRadius().GetElement(1) == 2 # test size s = itk.size(reader) assert s.GetElement(0) == s.GetElement(1) == 256 s = itk.size(reader.GetOutput()) assert s.GetElement(0) == s.GetElement(1) == 256 s = itk.size(reader.GetOutput().GetPointer()) assert s.GetElement(0) == s.GetElement(1) == 256
# # Example on the use of the GrayscaleErodeImageFilter # import itk from sys import argv itk.auto_progress(2) dim = 2 IType = itk.Image[itk.US, dim] OIType = itk.Image[itk.UC, dim] reader = itk.ImageFileReader[IType].New( FileName=argv[1] ) kernel = itk.strel(dim, 5) filter = itk.GrayscaleErodeImageFilter[IType, IType, kernel].New( reader, Kernel=kernel ) cast = itk.CastImageFilter[IType, OIType].New(filter) writer = itk.ImageFileWriter[OIType].New( cast, FileName=argv[2] ) writer.Update()
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # #==========================================================================*/ # # Example on the use of the BinaryDilateImageFilter # import itk from sys import argv itk.auto_progress(2) dim = 2 IType = itk.Image[itk.US, dim] OIType = itk.Image[itk.UC, dim] reader = itk.ImageFileReader[IType].New(FileName=argv[1]) kernel = itk.strel(dim, 5) filter = itk.BinaryDilateImageFilter[IType, IType, kernel].New(reader, DilateValue=200, Kernel=kernel) cast = itk.CastImageFilter[IType, OIType].New(filter) writer = itk.ImageFileWriter[OIType].New(cast, FileName=argv[2]) writer.Update()
def GetKernel(dimension, radius): element = itk.strel( int(dimension), radius) return element
# import Patented import PixelMath import Registration import Resize import SegmentationAndThreshold import SegmentationValidation import SimpleFilters import UnaryPixelMath import VXLNumerics PType = itk.US dim = 2 IType = itk.Image[PType, dim] kernel = itk.strel(2, 1) # create the reader reader = itk.ImageFileReader[IType].New(FileName=sys.argv[1]) sources = [] image = Base.Image[PType, dim].New() r = itk.ImageRegion._2() r.SetSize((10, 10)) image.SetRegions(r) image.Allocate() # BaseNumerics # BaseSpatialObject
spacing = itk.Vector[itk.D, dim]() spacing.Fill(1) spacing[2] = 3 changeInfo = itk.ChangeInformationImageFilter[IT].New(reader, OutputSpacing=spacing, ChangeSpacing=True) # resample image to be isotropic resample = itk.IsotropicResolutionImageFilter[IT, IT].New(changeInfo) #, MaximumIncrease=2) # un peu de nettoyage median = itk.MedianImageFilter[IT, IT].New(resample, Radius=2) # r = median.GetRadius() # r.SetElement(2, 1) # selectionne le noyau nThreshold = itk.BinaryThresholdImageFilter[IT, IT].New(median, LowerThreshold=30) # supprime tous les petits trucs qui trainent dans l'image kernel = itk.strel(IT, 5) # use an binary optimized sequence of filters instead of this one # nOpen = itk.OpeningByReconstructionImageFilter[IT, IT, kernel].New(nThreshold, Kernel=kernel) nErode = itk.BinaryErodeImageFilter[IT, IT, kernel].New(nThreshold, Kernel=kernel) nOpen = itk.ReconstructionByDilationImageFilter[IT, IT].New(nThreshold, nErode) # il faut faire une fermeture pour lisser les noyaux nk2 = itk.strel(IT, (30, 30, 10)) nClose = itk.BinaryMorphologicalClosingImageFilter[IT, IT, nk2].New(nOpen, SafeBorder=True, Kernel=nk2) nConnected = itk.ConnectedComponentImageFilter[IT, LIT].New(nClose) noyaux = itk.RelabelComponentImageFilter[LIT, IT].New(nConnected) # les granules # gHconvex = itk.HConvexImageFilter.US3US3(median, Height=25) # gThreshold = itk.BinaryThresholdImageFilter.US3US3.New(gHconvex, LowerThreshold=1)
def compute(self): dim = self.getInputFromPort("Dimension") radius = self.getInputFromPort("Radius") self.kernel = itk.strel(dim,radius) self.setResult("Kernel", self.kernel)
i = f.GetOutput() imgDuplicator = itk.ImageDuplicator[i].New(i) imgDuplicator.Update() return imgDuplicator.GetOutput() readerNuclei = itk.lsm(channel=0, fileName=inputImageName) medianNuclei = itk.MedianImageFilter.IUC3IUC3.New(readerNuclei) gaussianNuclei = itk.SmoothingRecursiveGaussianImageFilter.IUC3IUC3.New(medianNuclei, Sigma=0.2) fillNuclei = itk.SliceBySliceImageFilter.IUC3IUC3.New(gaussianNuclei, Filter=fill.GetPointer()) sizeNuclei = itk.PhysicalSizeOpeningImageFilter.IUC3IUC3.New(fillNuclei, Lambda=200) gradientNuclei = itk.GradientMagnitudeRecursiveGaussianImageFilter.IUC3IUC3.New(sizeNuclei, Sigma=0.36) ratsNuclei = itk.RobustAutomaticThresholdImageFilter.IUC3IUC3IUC3.New(sizeNuclei, gradientNuclei) # enlarge the nuclei to be sure to found the spots on the border dilateNuclei = itk.BinaryDilateImageFilter.IUC3IUC3SE3.New(ratsNuclei, Kernel=itk.strel(3, [5,5,3])) # split and labelize the nuclei maurerNuclei = itk.SignedMaurerDistanceMapImageFilter.IUC3IF3.New(dilateNuclei, UseImageSpacing=True) watershedNuclei = itk.MorphologicalWatershedImageFilter.IF3IUC3.New(maurerNuclei, Level=5.0, MarkWatershedLine=False) #, FullyConnected=True) maskWatershedNuclei = itk.MaskImageFilter.IUC3IUC3IUC3.New(watershedNuclei, dilateNuclei) lmNuclei = itk.LabelImageToLabelMapFilter.IUC3LM3.New(maskWatershedNuclei) # the same with the real mask (not the enlarged one), and also compute shape attributes for that one realMaskWatershedNuclei = itk.MaskImageFilter.IUC3IUC3IUC3.New(watershedNuclei, ratsNuclei) shapeNuclei = itk.LabelImageToShapeLabelMapFilter.IUC3LM3.New(realMaskWatershedNuclei) # remove (again) the objects too small to be a nucleus sizeOpeningNuclei = itk.ShapeOpeningLabelMapFilter.LM3.New(shapeNuclei, Attribute="PhysicalSize", Lambda=200) # remove the nucleus on the border - note that they can touch "a little" the border borderNuclei = itk.ShapeOpeningLabelMapFilter.LM3.New(sizeOpeningNuclei, Attribute="PhysicalSizeOnBorder", Lambda=50, ReverseOrdering=True) lm2iNuclei = itk.LabelMapToLabelImageFilter.LM3IUC3.New(borderNuclei) if opts.visualValidation:
#inputFileName = "gml16_cenp-fitc_dapi_1.zvi" nucleiFileName = sys.argv[1] inputFileName = "gml16_cenp-fitc_dapi_%s.zvi" % nucleiFileName[11:-12] # read the images cenp = itk.bioformats(inputFileName, ImageType=itk.Image.US3) nuclei = itk.ImageFileReader.IUC3.New(FileName=nucleiFileName) # remove the background before masking the image, in case large background outside the nuclei leak inside the nuclei cenpmedian = itk.MedianImageFilter.IUS3IUS3.New(cenp, Radius=[1, 1, 0]) # add a gaussian filter after the median? sopening = itk.PhysicalSizeOpeningImageFilter.IUS3IUS3.New(cenpmedian, Lambda=0.2) sub = itk.SubtractImageFilter.IUS3IUS3IUS3.New(cenpmedian, sopening) # and dilate the nuclei to be sure to not truncate the spots dilate = ClosestLabelDilateImageFilter.IUC3IF3.New(nuclei, Kernel=itk.strel(3,[3,3,0])) # translate the masks to label objects li2lm = itk.LabelImageToStatisticsLabelMapFilter.IUC3IUS3LM3.New(dilate, sub) lmNuclei = li2lm lmNuclei() # keep only one nucleus in the pre-filtered cenp image mask = itk.LabelMapMaskImageFilter.LM3IUS3.New(lmNuclei, sub, Crop=True, CropBorder=1, Label=3) # search a nice threshold - it will be based on the values of the brightests regions (spots) rmax = itk.RegionalMaximaImageFilter.IUS3IUS3.New(mask, FullyConnected=True) rmaxbi2lm = itk.BinaryImageToStatisticsLabelMapFilter.IUS3IUS3LM3.New(rmax, mask, FullyConnected=True) rmaxRelabel = itk.StatisticsRelabelLabelMapFilter.LM3.New(rmaxbi2lm, Attribute="Maximum") # and locate the spots th = itk.BinaryThresholdImageFilter.IUS3IUC3.New(mask) # convert them to a label map representation bi2lm = itk.BinaryImageToStatisticsLabelMapFilter.IUC3IUS3LM3.New(th, sub, FullyConnected=True)
def compute(self): dim = self.get_input("Dimension") radius = self.get_input("Radius") self.kernel = itk.strel(dim, radius) self.set_output("Kernel", self.kernel)
readerNuclei = itk.lsm(channel=0, fileName=inputImageName) medianNuclei = itk.MedianImageFilter.IUC3IUC3.New(readerNuclei) gaussianNuclei = itk.SmoothingRecursiveGaussianImageFilter.IUC3IUC3.New( medianNuclei, Sigma=0.2) fillNuclei = itk.SliceBySliceImageFilter.IUC3IUC3.New( gaussianNuclei, Filter=fill.GetPointer()) sizeNuclei = itk.PhysicalSizeOpeningImageFilter.IUC3IUC3.New( fillNuclei, Lambda=200) gradientNuclei = itk.GradientMagnitudeRecursiveGaussianImageFilter.IUC3IUC3.New( sizeNuclei, Sigma=0.36) ratsNuclei = itk.RobustAutomaticThresholdImageFilter.IUC3IUC3IUC3.New( sizeNuclei, gradientNuclei) # enlarge the nuclei to be sure to found the spots on the border dilateNuclei = itk.BinaryDilateImageFilter.IUC3IUC3SE3.New( ratsNuclei, Kernel=itk.strel(3, [5, 5, 3])) # split and labelize the nuclei maurerNuclei = itk.SignedMaurerDistanceMapImageFilter.IUC3IF3.New( dilateNuclei, UseImageSpacing=True) watershedNuclei = itk.MorphologicalWatershedImageFilter.IF3IUC3.New( maurerNuclei, Level=5.0, MarkWatershedLine=False) #, FullyConnected=True) maskWatershedNuclei = itk.MaskImageFilter.IUC3IUC3IUC3.New( watershedNuclei, dilateNuclei) lmNuclei = itk.LabelImageToLabelMapFilter.IUC3LM3.New(maskWatershedNuclei) # the same with the real mask (not the enlarged one), and also compute shape attributes for that one realMaskWatershedNuclei = itk.MaskImageFilter.IUC3IUC3IUC3.New( watershedNuclei, ratsNuclei) shapeNuclei = itk.LabelImageToShapeLabelMapFilter.IUC3LM3.New( realMaskWatershedNuclei) # remove (again) the objects too small to be a nucleus sizeOpeningNuclei = itk.ShapeOpeningLabelMapFilter.LM3.New(
def compute(self): dim = self.get_input("Dimension") radius = self.get_input("Radius") self.kernel = itk.strel(dim,radius) self.set_output("Kernel", self.kernel)
def GetKernel(dimension, radius): element = itk.strel(int(dimension), radius) return element