def getSitkGradient(im): gradient = sitk.Gradient(im) gradientn = sitk.gafi(gradient) gradients = [sitk.Cast(sitk.gifa(gradientn[:,:,:,i]),6) for i in range(gradient.GetNumberOfComponentsPerPixel())] #gradients = imaging.sortAxes(gradient,[3,0,1,2]) return gradients
def getSmoothGradient(im,sigma=2): shape = im.shape im = sitk.gifa(im) gradient = sitk.GradientRecursiveGaussian(im,sigma) gradient = sitk.gafi(gradient) gradients = imaging.sortAxes(gradient,[len(shape)]+range(len(shape))) return gradients
def step(self): """Perform a single step of the morphological snake evolution.""" # Assign attributes to local variables for convenience. u = self._u gI = self._data dgI = self._ddata theta = self._theta v = self._v if u is None: raise ValueError, "the levelset is not set (use set_levelset)" # res = np.copy(u) res = u*1 # Balloon. if v > 0: aux = sitk.Cast(sitk.BinaryDilate(sitk.Cast(u,3)),self.dtype) # aux = binary_dilation(u, self.structure) elif v < 0: aux = sitk.Cast(sitk.BinaryErode(sitk.Cast(u,3)),self.dtype) # aux = binary_erosion(u, self.structure) if v!= 0: res = sitk.Cast(self._threshold_mask_v==0,self.dtype)*res+\ sitk.Cast(self._threshold_mask_v>00,self.dtype)*aux # res[self._threshold_mask_v] = aux[self._threshold_mask_v] # Image attachment. # aux = np.zeros_like(res) aux = sitk.gifa(np.zeros(res.GetSize()[::-1],dtype=self.dtype)) dres = getSitkGradient(res) for el1, el2 in zip(dgI, dres): aux += el1*el2 res[aux > 0] = 1 res[aux < 0] = 0 res = sitk.Cast(aux>0,self.dtype) # Smoothing. # Smoothing. res = sitk.gafi(res) for i in xrange(self.smoothing): res = curvop(res) res = sitk.gifa(res) self._u = res
def step(self): """Perform a single step of the morphological Chan-Vese evolution.""" # Assign attributes to local variables for convenience. u = self._u if u is None: raise ValueError, "the levelset function is not set (use set_levelset)" data = self.data # Determine c0 and c1. inside = sitk.Cast(u>0,self.dtype) outside = sitk.Cast(u<=0,self.dtype) c0 = sitkSum(data*outside)/float(sitkSum(outside)) c1 = sitkSum(data*inside)/float(sitkSum(inside)) # c0 = data[outside].sum() / float(outside.sum()) # c1 = data[inside].sum() / float(inside.sum()) # Image attachment. dres = getSitkGradient(u) # abs_dres = sitk.GradientMagnitude(u) # dres = np.array(np.gradient(u)) # abs_dres = np.abs(dres).sum(0) abs_dres = sitk.Cast(sitk.Add(sitk.Add(dres[0],dres[1]),dres[2]),self.dtype) #aux = abs_dres * (c0 - c1) * (c0 + c1 - 2*data) aux = abs_dres * (self.lambda1*(data - c1)**2 - self.lambda2*(data - c0)**2) # res = np.copy(u) res = u*1. res = sitk.Cast(aux<0,self.dtype)+res*sitk.Cast(aux==0,self.dtype) # res[aux < 0] = 1 # res[aux > 0] = 0 # Smoothing. res = sitk.gafi(res) for i in xrange(self.smoothing): res = curvop(res) res = sitk.Cast(sitk.gifa(res),self.dtype) self._u = res
def getSmoothGradientMagnitude(im,sigma=2): im = sitk.gifa(im) gm = sitk.GradientMagnitudeRecursiveGaussian(im,sigma) gm = sitk.gafi(gm) return gm