def calculateOpposingWeights(size, sigmoidHalfWidthRelativeToStackSize=0.05): print 'calculating weights for opposing stacks fusion' size = (size[0], size[1], size[2] + 2) halfY = 0.9 nZPlanes = size[2] sigmoidHalfWidthInPixels = nZPlanes * sigmoidHalfWidthRelativeToStackSize halfY = 0.9 # half width is defined as the x value corresponding to y=halfY sigmoidA = n.log(1. / halfY - 1.) / sigmoidHalfWidthInPixels zs = n.linspace(0, size[2] - 1, size[2]) zsigmoid1 = n.abs(1 / (1 + n.exp(sigmoidA * (zs - size[2] / 2.)))) #zsigmoid2 = n.abs(1/(1+n.exp(sigmoidA*(-zs+size[2]/2.)))) zyplane = n.ones(size[1:]) zyplane1 = zyplane * zsigmoid1 #zyplane2 = zyplane*zsigmoid2 zyplane1 = zyplane1.astype(n.float32) #zyplane2 = zyplane2.astype(n.float32) w1 = (n.ones(size, dtype=n.uint16) * zyplane1).swapaxes(0, 1).swapaxes( 1, 2).swapaxes(0, 1) #w2 = (n.ones(size,dtype=n.uint16)*zyplane2).swapaxes(0,1).swapaxes(1,2).swapaxes(0,1) w1 = sitk.GetImageFromArray(w1) w2 = beads.transformStack([1., 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, size[2] - 2], w1, outShape=(size[0], size[1], size[2] - 2)) w1 = beads.transformStack([1., 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 2], w1, outShape=(size[0], size[1], size[2] - 2)) return [w1, w2]
def fuseStacks(s, weights, p=None): if p != None: for iis in range(len(s)): s[iis] = beads.transformStack(p[iis], s[iis], outShape=s[0].GetSize()) # pdb.set_trace() if weights is None: print 'fusing stacks by averaging' N = len(s) tmp = sitk.Cast(s[0], 6) / N for i in range(N - 1): tmp += sitk.Cast(s[i + 1], 6) / N return sitk.Cast(tmp, 3) print 'fusing stacks by using weight matrices' for iweight in range(len(weights)): weights[iweight].SetSpacing(s[iweight].GetSpacing()) weights[iweight].SetOrigin(s[iweight].GetOrigin()) #for iweight in range(len(weights)): #weights[iweight] = weights[iweight]*sitk.Cast(s[iweight],6) result = weights[0] * sitk.Cast(s[0], 6) for iweight in range(1, len(weights)): result += weights[iweight] * sitk.Cast(s[iweight], 6) result = sitk.Cast(result, 3) return result
def fuseOpposingStacksOld(p1,p2,s1,s2,normalizeIntensities=True,axis=0): print 'fusing (normalize intensities %s)' %(['OFF','ON'][int(normalizeIntensities)]) s1 = beads.transformStack(invertParams(p1),s1) s2 = beads.transformStack(invertParams(p2),s2) s1 = sitk.GetArrayFromImage(s1) s2 = sitk.GetArrayFromImage(s2) if normalizeIntensities: n1,n2 = n.mean(s1),n.mean(s2) else: n1,n2 = 1.,1. length = s1.shape[axis] s1 = s1.swapaxes(0,axis) s2 = s2.swapaxes(0,axis) for z in range(length): w1 = 1/(1+n.exp(-40./length*(z-length/2.))) w2 = 1/(1+n.exp(40./length*(z-length/2.))) s1[z,:,:] = (w1*s1[z,:,:]+w2*s2[z,:,:])/(w1+w2) return sitk.GetImageFromArray(s1.swapaxes(0,axis))
def fuseOpposingStacksOld(p1, p2, s1, s2, normalizeIntensities=True, axis=0): print 'fusing (normalize intensities %s)' % (['OFF', 'ON' ][int(normalizeIntensities)]) s1 = beads.transformStack(invertParams(p1), s1) s2 = beads.transformStack(invertParams(p2), s2) s1 = sitk.GetArrayFromImage(s1) s2 = sitk.GetArrayFromImage(s2) if normalizeIntensities: n1, n2 = n.mean(s1), n.mean(s2) else: n1, n2 = 1., 1. length = s1.shape[axis] s1 = s1.swapaxes(0, axis) s2 = s2.swapaxes(0, axis) for z in range(length): w1 = 1 / (1 + n.exp(-40. / length * (z - length / 2.))) w2 = 1 / (1 + n.exp(40. / length * (z - length / 2.))) s1[z, :, :] = (w1 * s1[z, :, :] + w2 * s2[z, :, :]) / (w1 + w2) return sitk.GetImageFromArray(s1.swapaxes(0, axis))
def fuseOpposingStacks(p1, p2, s1, s2, axis=0): s1 = beads.transformStack(invertParams(p1), s1) s2 = beads.transformStack(invertParams(p2), s2) size = s1.GetSize() s1 = sitk.GetArrayFromImage(s1) s2 = sitk.GetArrayFromImage(s2) # halfY = 0.9 nZPlanes = size[2] sigmoidHalfWidthInPixels = nZPlanes * 0.05 halfY = 0.9 # half width is defined as the x value corresponding to y=halfY sigmoidA = n.log(1. / halfY - 1.) / sigmoidHalfWidthInPixels length = s1.shape[axis] s1 = s1.swapaxes(0, axis) s2 = s2.swapaxes(0, axis) for z in range(length): w1 = n.abs(1 / (1 + n.exp(sigmoidA * (z - size[2] / 2.)))) w2 = n.abs(1 / (1 + n.exp(sigmoidA * (-z + size[2] / 2.)))) s1[z, :, :] = (w1 * s1[z, :, :] + w2 * s2[z, :, :]) / (w1 + w2) return sitk.GetImageFromArray(s1.swapaxes(0, axis))
def fuseOpposingStacks(p1,p2,s1,s2,axis=0): s1 = beads.transformStack(invertParams(p1),s1) s2 = beads.transformStack(invertParams(p2),s2) size = s1.GetSize() s1 = sitk.GetArrayFromImage(s1) s2 = sitk.GetArrayFromImage(s2) # halfY = 0.9 nZPlanes = size[2] sigmoidHalfWidthInPixels = nZPlanes*0.05 halfY = 0.9 # half width is defined as the x value corresponding to y=halfY sigmoidA = n.log(1./halfY-1.)/sigmoidHalfWidthInPixels length = s1.shape[axis] s1 = s1.swapaxes(0,axis) s2 = s2.swapaxes(0,axis) for z in range(length): w1 = n.abs(1/(1+n.exp(sigmoidA*(z-size[2]/2.)))) w2 = n.abs(1/(1+n.exp(sigmoidA*(-z+size[2]/2.)))) s1[z,:,:] = (w1*s1[z,:,:]+w2*s2[z,:,:])/(w1+w2) return sitk.GetImageFromArray(s1.swapaxes(0,axis))
def calculateOpposingWeights(size,sigmoidHalfWidthRelativeToStackSize=0.05): print 'calculating weights for opposing stacks fusion' size = (size[0],size[1],size[2]+2) halfY = 0.9 nZPlanes = size[2] sigmoidHalfWidthInPixels = nZPlanes*sigmoidHalfWidthRelativeToStackSize halfY = 0.9 # half width is defined as the x value corresponding to y=halfY sigmoidA = n.log(1./halfY-1.)/sigmoidHalfWidthInPixels zs = n.linspace(0,size[2]-1,size[2]) zsigmoid1 = n.abs(1/(1+n.exp(sigmoidA*(zs-size[2]/2.)))) #zsigmoid2 = n.abs(1/(1+n.exp(sigmoidA*(-zs+size[2]/2.)))) zyplane = n.ones(size[1:]) zyplane1 = zyplane*zsigmoid1 #zyplane2 = zyplane*zsigmoid2 zyplane1 = zyplane1.astype(n.float32) #zyplane2 = zyplane2.astype(n.float32) w1 = (n.ones(size,dtype=n.uint16)*zyplane1).swapaxes(0,1).swapaxes(1,2).swapaxes(0,1) #w2 = (n.ones(size,dtype=n.uint16)*zyplane2).swapaxes(0,1).swapaxes(1,2).swapaxes(0,1) w1 = sitk.GetImageFromArray(w1) w2 = beads.transformStack([1.,0,0,0,1,0,0,0,-1,0,0,size[2]-2],w1,outShape=(size[0],size[1],size[2]-2)) w1 = beads.transformStack([1.,0,0,0,1,0,0,0,1,0,0,2],w1,outShape=(size[0],size[1],size[2]-2)) return [w1,w2]
def fuseStacks(s,weights,p=None): if p != None: for iis in range(len(s)): s[iis] = beads.transformStack(p[iis],s[iis],outShape=s[0].GetSize()) # pdb.set_trace() if weights is None: print 'fusing stacks by averaging' N = len(s) tmp = sitk.Cast(s[0],6)/N for i in range(N-1): tmp += sitk.Cast(s[i+1],6)/N return sitk.Cast(tmp,3) print 'fusing stacks by using weight matrices' for iweight in range(len(weights)): weights[iweight].SetSpacing(s[iweight].GetSpacing()) weights[iweight].SetOrigin(s[iweight].GetOrigin()) #for iweight in range(len(weights)): #weights[iweight] = weights[iweight]*sitk.Cast(s[iweight],6) result = weights[0]*sitk.Cast(s[0],6) for iweight in range(1,len(weights)): result += weights[iweight]*sitk.Cast(s[iweight],6) result = sitk.Cast(result,3) return result