def xcorr(offset_image, image): image = gaussianSmoothing(image, 1) offset_image = gaussianSmoothing(offset_image, 1) image = filters.sobel(image) offset_image = filters.sobel(offset_image) shift, error, diffphase = register_translation(image, offset_image, 100) return (shift[1], shift[0])
def pmapToHeightMap(pmap): footprint, origin = makeBall(r=3) medianImg = scipy.ndimage.percentile_filter( input=pmap, #size=(20,20,20), footprint=footprint, #origin=origin, mode='reflect', percentile=50.0) if False: blurredSmall = vigra.gaussianSmoothing( pmap.T, 1.0, ).T blurredLarge = vigra.gaussianSmoothing( pmap.T, 6.0, ).T blurredSuperLarge = vigra.gaussianSmoothing( pmap.T, 10.0, ).T else: blurredSmall = fastfilters.gaussianSmoothing( pmap, 1.0, ) blurredLarge = fastfilters.gaussianSmoothing( pmap, 6.0, ) blurredSuperLarge = fastfilters.gaussianSmoothing( pmap, 10.0, ) combined = medianImg + blurredSuperLarge * 0.3 + 0.15 * blurredLarge + 0.1 * blurredSmall footprint, origin = makeBall(r=3) combined = scipy.ndimage.percentile_filter( input=combined, #size=(20,20,20), footprint=footprint, #origin=origin, mode='reflect', percentile=50.0) combined = fastfilters.gaussianSmoothing(combined, 1.3) #if False: # nifty.viewer.view3D(pmap, show=False, title='pm',cmap='gray') # nifty.viewer.view3D(medianImg, show=False, title='medianImg',cmap='gray') # nifty.viewer.view3D(combined, show=False, title='combined',cmap='gray') # pylab.show() return combined
def test_several_gaussian_filter(): '''result:these are almost the same''' a = np.zeros((5, 5),np.float32) a[2, 2] = 1 import vigra from skimage.filter import gaussian_filter print gaussian_filter(a,1) print vigra.gaussianSmoothing(a, 1) print ndimage.gaussian_filter(a, 1)
def createHeatmap2(dataR,dataG): dataRm = np.array(dataR[...,2],dtype=np.float32) dataRm = np.array(vigra.gaussianSmoothing(dataRm, 3),dtype=np.uint8) dataGm = np.array(dataG[...,1],dtype=np.float32) dataGm = np.array(vigra.gaussianSmoothing(dataGm, 3),dtype=np.uint8) dataRR = dataR[:,:,2] * dataR[:,:,2] dataGG = dataG[:,:,1] * dataG[:,:,1] dataRR = np.array(dataRR,dtype = np.float32) dataGG = np.array(dataGG,dtype = np.float32) dataRRm = np.array(vigra.gaussianSmoothing(dataRR, 3),dtype=np.uint8) dataGGm = np.array(vigra.gaussianSmoothing(dataGG, 3),dtype=np.uint8) dataRG = dataR[:,:,2] * dataG[:,:,1] dataRG = np.array(dataRG,dtype=np.float32) dataRGm = np.array(vigra.gaussianSmoothing(dataRG, 3),dtype=np.uint8) suspectedBackgroundR = scipy.stats.scoreatpercentile(dataR[...,2].flatten(), 50) suspectedBackgroundG = scipy.stats.scoreatpercentile(dataG[...,1].flatten(), 50) dataRwichtung = np.where(dataR[:,:,2]>suspectedBackgroundR,1,0) dataGwichtung = np.where(dataG[:,:,1]>suspectedBackgroundG,1,0) dataB = np.zeros((dataR.shape)) dataB[:,:,0]=(dataRGm-dataRm*dataGm)*dataRwichtung*dataGwichtung Var1=np.sqrt(dataRRm-dataRm**2) Var2=np.sqrt(dataGGm-dataGm**2) dataRVarwichtung = np.where(Var1!=0,1,0) dataGVarwichtung = np.where(Var2!=0,1,0) Var1=np.where(Var1==0,10,Var1) #dataRVarwichtung cutts them out anyway Var2=np.where(Var2==0,10,Var2) dataB[:,:,0]=dataB[:,:,0]/(Var1*Var2)*dataRVarwichtung*dataGVarwichtung #mmx = scipy.stats.scoreatpercentile(dataB[dataB>0].flatten(), 90) #if mmx.max() > 0: # dataB[dataB[:,:,0]>mmx] = [mmx,0,0,0] # crop maximum at above percentile dataB = dataB *255./np.max(dataB) dataB = np.array(dataB, dtype=np.uint8) dataR=dataR+ dataB return dataB
def doSmoothing(self): #if len(self.m_npimages): #for i, imag in enumerate(self.m_npimages): if self.ui.selectChannelcomboBox.currentIndex() == 0: for i in range(self.m_numberImages): imag = self.Images[i].npimage imag = vigra.RGBImage(np.array(imag,dtype=np.float32)) self.Images[i].npimage = np.array(vigra.gaussianSmoothing(imag, self.sigma),dtype=np.uint8) #self.m_npimages[i] = np.array(vigra.gaussianSmoothing(imag, self.sigma),dtype=np.uint8) else: i = self.ui.selectChannelcomboBox.currentIndex()-1 imag = self.Images[i].npimagenp.var imag = vigra.RGBImage(np.array(imag,dtype=np.float32)) self.Images[i].npimage = np.array(vigra.gaussianSmoothing(imag, self.sigma),dtype=np.uint8) self.recalculateResult() self.main_window.statusBar().showMessage('Smoothing done')
def run(self): res = Filters.rayFeatures(self.dataIN, self.rayLength) res = Filters.multiHessian(res, self.scales) mem = vigra.gaussianSmoothing(self.dataIN, self.sigmaSmooth) res = Filters.waterSeg(mem, res, self.th1, self.th2) self.seg = res
def undirected_deformation(self, section): shape = section.shape # make meshgrid x, y = np.meshgrid(np.arange(shape[0]), np.arange(shape[1])) # generate random vector field and smooth it flow_x = np.random.uniform(-1, 1, shape) * self.deformation_strength flow_y = np.random.uniform(-1, 1, shape) * self.deformation_strength flow_x = vigra.gaussianSmoothing( flow_x, sigma=3.) # sigma is hard-coded for now flow_y = vigra.gaussianSmoothing( flow_y, sigma=3.) # sigma is hard-coded for now # apply the flow fields flow_x, flow_y = (x + flow_x).reshape(-1, 1), (y + flow_y).reshape(-1, 1) section = map_coordinates(section, (flow_y, flow_x), mode='constant').reshape(shape) return section
def thresholdSegmentation(data, smoothingLevel, thresholdLevel): # Apply gaussian smoothing if smoothingLevel == 0: smo = numpy.copy(data) else: smo = vigra.gaussianSmoothing(data, smoothingLevel) # Apply thresholding res = smo > thresholdLevel return res
def watershed_superpixel_vigra(probs, offset = 0): # in nikos script: substract the mean and take the abs value # threshold: #hmap = np.abs( probs - np.mean(probs) ) hmap = probs.copy() # threshold the data #hmap[hmap < 0.45] = 0. # smooth the hmap hmap_smooth = vigra.gaussianSmoothing(hmap, 1.5) # Hessian of Gaussian hessian = vigra.filters.hessianOfGaussian(hmap, sigma = 1.5) hessian_ev = vigra.filters.tensorEigenvalues( hessian ) # combine the filters h_ev0 = hessian_ev[:,:,0] h_ev1 = hessian_ev[:,:,1] combination = 3*np.absolute( h_ev0 ) + 3*np.absolute( h_ev1 ) + hmap_smooth #combination = hmap # construct a line filter (cf. https://www.spl.harvard.edu/archive/spl-pre2007/pages/papers/yoshi/node3.html) #a_0 = 0.5 #a_1 = 2.0 #line_filter = np.multiply( (h_ev0 <= 0), np.exp( - np.divide( np.square(h_ev0), 2*np.square(a_0*h_ev1) ) ) ) #line_filter += np.multiply( (h_ev0 > 0), np.exp( - np.divide( np.square(h_ev0), 2*np.square(a_1*h_ev1) ) ) ) #volumina_n_layer( [ np.absolute(hessian_ev[:,:,0]), # np.absolute(hessian_ev[:,:,1]), # combination] ) #quit() # find the local minima seeds = vigra.analysis.extendedLocalMinima(combination, neighborhood = 8) # find the connected components in the minima and use them as seeds SEEDS = vigra.analysis.labelImageWithBackground(seeds) # apply the watershed algorithm seg_ws, maxRegionLabel = vigra.analysis.watersheds(combination, neighborhood = 8, seeds = SEEDS.astype(np.uint32) ) # find connected components seg_ws = vigra.analysis.labelImage(seg_ws) # if we have an offset, add it to the array if offset != 0: seg_ws += offset * np.ones( seg_ws.shape ) #volumina_double_layer(probs, seg_ws) #quit() return seg_ws
def pmapToHeightMap(pmap): footprint, origin = makeBall(r=3) medianImg = scipy.ndimage.percentile_filter(input=pmap, #size=(20,20,20), footprint=footprint, #origin=origin, mode='reflect', percentile=50.0) if False: blurredSmall = vigra.gaussianSmoothing(pmap.T, 1.0,).T blurredLarge = vigra.gaussianSmoothing(pmap.T, 6.0,).T blurredSuperLarge = vigra.gaussianSmoothing(pmap.T, 10.0,).T else: blurredSmall = fastfilters.gaussianSmoothing(pmap, 1.0,) blurredLarge = fastfilters.gaussianSmoothing(pmap, 6.0,) blurredSuperLarge = fastfilters.gaussianSmoothing(pmap, 10.0,) combined = medianImg + blurredSuperLarge*0.3 + 0.15*blurredLarge + 0.1*blurredSmall footprint, origin = makeBall(r=3) combined = scipy.ndimage.percentile_filter(input=combined, #size=(20,20,20), footprint=footprint, #origin=origin, mode='reflect', percentile=50.0) if False: nifty.viewer.view3D(pmap, show=False, title='pm',cmap='gray') nifty.viewer.view3D(medianImg, show=False, title='medianImg',cmap='gray') nifty.viewer.view3D(combined, show=False, title='combined',cmap='gray') pylab.show() return combined
def watershed_supervoxel_vigra(probs): # compute seeds # try different filter for computing the best seeds (= minima) # best options so far: # for isotropic data (2x2x2 nm): Gaussian Smoothing with sigma = 4.5 # for anisotropic data: Gaussian Smoothing with sigma = 2 #sm_probs = np.array(np.abs( probs - 0.5*( np.max(probs) - np.min(probs) ) ), dtype = np.float32 ) # Gaussian smoothing sm_probs = vigra.gaussianSmoothing(probs, (2.5,2.5,0.5) ) hessian = vigra.filters.hessianOfGaussian(probs, sigma = (2.5,2.5,0.5) ) hessian_ev = vigra.filters.tensorEigenvalues( hessian ) print hessian_ev.shape volumina_n_layer( [ probs, np.absolute(hessian_ev[:,:,:,0]), np.absolute(hessian_ev[:,:,:,1]), np.absolute(hessian_ev[:,:,:,2]) ] ) quit() # Difference of Gaussians #diff = vigra.gaussianSmoothing(probs,2) - sm_probs #volumina_single_layer(diff) seeds = vigra.analysis.extendedLocalMinima3D(sm_probs, neighborhood = 26) SEEDS = vigra.analysis.labelVolumeWithBackground(seeds) SEEDS = SEEDS.astype(np.uint32) #plot.figure() #plot.gray() #plot.imshow(SEEDS[:,:,25]) #plot.show() seg_ws, maxRegionLabel = vigra.analysis.watersheds(sm_probs, neighborhood = 6, seeds = SEEDS) seg_ws = vigra.analysis.labelVolumeWithBackground(seg_ws) #volumina_double_layer(probs, seg_ws) return seg_ws
def makeFeat(rag, raw, labels, show=True): featureExtractor = graphs.gridRagFeatureExtractor(rag, labels) featureExtractor.labels = labels featureExtractor.graph = rag geoFeat = featureExtractor.geometricFeatures() topoFeat = featureExtractor.topologicalFeatures() features = [geoFeat, topoFeat] # ward facs wardness = numpy.array([0.0, 0.1, 0.15, 0.25, 0.5, 1.0], dtype="float32") accFeat = featureExtractor.accumulatedFeatures(raw) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.filters.gaussianGradientMagnitude(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.filters.laplacianOfGaussian(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.gaussianSmoothing(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) # st ev for s in [2.0, 3.0, 4.0]: res = stEv(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) mean = accFeat[:, 0] ucm = featureExtractor.ucmTransformFeatures(mean[:, None], wardness) features.extend([accFeat, ucm]) if False: for x in range(ucm.shape[1]): rag.showEdgeFeature(raw, ucm[:, x]) vigra.show() # hessian ev for s in [1.0, 3.0, 4.0, 6.0, 8.0]: img = hessianEv2(raw, s, 2.0) # vigra.imshow(img) # vigra.show() accFeat = featureExtractor.accumulatedFeatures(img) mean = accFeat[:, 0] ucm = featureExtractor.ucmTransformFeatures(mean[:, None], wardness) features.extend([accFeat, ucm]) if False: for x in range(ucm.shape[1]): print "x", x rag.showEdgeFeature(raw, ucm[:, x]) vigra.show() # break # features = numpy.concatenate(features, axis=1) return features
def calcObjectMatrix(self, s, M): #Features, Labels, Data O = [] for i in range(len(self.gapL)): x, y, z = self.gapL[i] f = self.SpecialFeatures[i] o = {} F = [] x0, x1 = max(0, x - M), min(s[0], x + M) y0, y1 = max(0, y - M), min(s[1], y + M) z0, z1 = max(0, z - M), min(s[2], z + M) tmp_volume_pre = self.pred_volume[x0:x1, y0:y1, z0:z1, :] tmp_volume_raw = self.raw[x0:x1, y0:y1, z0:z1] tmp_Feature = [] [xp1,yp1,zp1,\ xp2,yp2,zp2,\ xo1,yo1,zo1,\ xo2,yo2,zo2,\ t1,t2]=f # Endpoint Atribute (1) x1 = numpy.array([xp1, yp1, zp1]) o1 = numpy.array([xo1, yo1, zo1]) t1 = numpy.float16(t1) # Endpoint Atribute (2) x2 = numpy.array([xp2, yp2, zp2]) o2 = numpy.array([xo2, yo2, zo2]) t2 = numpy.float16(t2) # Pair Features c = (x1 + x2) / 2 # centre d = numpy.linalg.norm(x1 - x2) # distance k1 = (x1 - c) / numpy.linalg.norm( x1 - c) # vector pointing to center (1) k2 = (x2 - c) / numpy.linalg.norm( x2 - c) # vector pointing to center (2) # Meassure if orientation of endpoints is alligned [0: <- -> , 1: -> <-] A = 1 - (2 + numpy.dot(k1, o1) + numpy.dot(k2, o2)) / 4 #[0,1] #for i in numpy.arange(4,13,2): #tmp_Feature.append(rayFeatures(tmp_volume_pre[:,:,:,0],i)) F.append(d) F.append(t1 / t2) F.append(t1 + t2) F.append(A) for i in numpy.arange(0.5, 6, 0.5): tmp_Feature.append( vigra.gaussianSmoothing(tmp_volume_pre[:, :, :, 0], i)) tmp_Feature.append( vigra.gaussianSmoothing(tmp_volume_pre[:, :, :, 1], i)) tmp_Feature.append( vigra.gaussianSmoothing(tmp_volume_pre[:, :, :, 2], i)) for v in tmp_Feature: #ToDo: change to interpolated line not just horizontal mean!! s1, s2, s3 = v.shape c = v[s1 / 2, s2 / 2, s3 / 2] l = numpy.mean(v[s1 / 2, s2 / 2, :]) d = numpy.mean(v[:, :, s3 / 2]) F.append(c) F.append(l) F.append(d) o['Features'] = F o['Position'] = [x, y, z] o['Label'] = -1 o['Prediction'] = [[0]] o['Data'] = tmp_volume_raw O.append(o) O.reverse() #f=numpy.array(f) #else: #return 0,0 #return f,tmp_volume_raw return O
def coordinateBasedColocalization(dataR, dataG): originaldataR = np.array(dataR) originaldataG = np.array(dataG) maximumR = np.max(dataR) dataR[:,:,2]=np.where(dataR[:,:,2]>=maximumR*0.1,255,0) dataRnp = vigra.RGBImage(np.array(dataR, dtype = np.float32)) labelsR = np.zeros((dataR.shape[0], dataR.shape[1])) labelsR = (vigra.analysis.labelImageWithBackground(dataRnp[...,2], 8,background_value=0)) maximumG = np.max(dataG) dataG[:,:,1]=np.where(dataG[:,:,1]>=maximumG*0.1,255,0) dataGnp = vigra.RGBImage(np.array(dataG, dtype = np.float32)) labelsG = np.zeros((dataR.shape[0], dataR.shape[1])) labelsG = (vigra.analysis.labelImageWithBackground(dataGnp[...,1], 8,background_value=0)) '''pyplot.matshow(dataR[...,2],1) pyplot.matshow(dataG[...,1],2) pyplot.matshow(labelsR,3) pyplot.matshow(labelsG,4) pyplot.show() ''' numberClassesR = np.max(labelsR) numberClassesG = np.max(labelsG) classesR = [] classesR.append([]) for i in range(1,numberClassesR + 1): coordinatesOfCurrentClass = np.where(labelsR == i,1,0) classesR.append(coordinatesOfCurrentClass) classesG = [] classesG.append([]) for i in range(1,numberClassesG + 1): coordinatesOfCurrentClass = np.where(labelsG == i,1,0) classesG.append(coordinatesOfCurrentClass) DAMatrix = np.zeros(dataR.shape) DBMatrix = np.zeros(dataR.shape) radius = 10 for i in range(radius,dataR.shape[0]-radius): for j in range(radius,dataR.shape[1]-radius): print i,j actuallClass = labelsR[i,j] if actuallClass != 0: Naa = 0 for x in range(i-radius,i+radius): for y in range(j-radius, j+radius): if (x-i)**2+(y-j)**2<=radius**2: Naa = Naa + classesR[actuallClass][x,y] if Naa!=0: Rmax=0 coords = np.where(classesR[actuallClass] == 1) for n in range(len(coords[0])): tempMax = np.sqrt((coords[0][n]-i)**2+(coords[1][n]-j)**2) if tempMax>Rmax: Rmax=tempMax Naamax = 0 Rmax = int(Rmax) for x in range(i-Rmax,i+Rmax): for y in range(j-radius, j+radius): if (x-i)**2+(y-j)**2<=radius**2: Naamax = Naamax + classesR[actuallClass][x,y] #######Achtung sollte alle classen durchlaufen!!!! Rminb = int(100) coords = np.where(classesG[actuallClass] == 1) for n in range(len(coords[0])): tempMin = np.sqrt((coords[0][n]-i)**2+(coords[1][n]-j)**2) if tempMin<Rminb: Rminb=tempMin if Rminb<2: print i,j DAMatrix[i,j,2] = Naa / (np.pi*radius**2)* np.pi*Rmax**2/Naamax*np.exp(-Rminb) else: DAMatrix[i,j,2] = 0 Nab = 0 for x in range(i-radius,i+radius): for y in range(j-radius, j+radius): if (x-i)**2+(y-j)**2<=radius**2: for k in range(1,numberClassesG+1): Nab = Nab + classesG[k][x,y] if Nab!=0: #if there is no class B in the radius this block is skipped Rmax=0 coords = np.where(classesG[actuallClass] == 1) for n in Samsung-Galaxy-Noterange(len(coords[0])): tempMax = np.sqrt((coords[0][n]-i)**2+(coords[1][n]-j)**2) if tempMax>Rmax: Rmax=tempMax Rmaxb=Rmax Nabmax = 0 Rmax=int(Rmax) for x in range(i-Rmax,i+Rmax): for y in range(j-radius, j+radius): if (x-i)**2+(y-j)**2<=radius**2: for k in range(1,numberClassesG+1): Nabmax = Nabmax + classesG[k][x,y] DBMatrix[i,j,1] = Nab / (np.pi*radius**2)* np.pi*Rmax**2/Nabmax else: DBMatrix[i,j,1] = 0 #if there is no class B in the radius dataR[:,:,2]=np.where(dataR[:,:,2]>=maximumR*0.1,255,0) else: DAMatrix[i,j,2] = 0 DBMatrix[i,j,1] = 0 dataR=np.where(dataR<0,0,dataR) dataG=np.where(dataG<0,0,dataG) dataR=DAMatrix*255./np.max(DAMatrix) dataG=DBMatrix*255./np.max(DBMatrix) dataRm = np.array(dataR[...,2],dtype=np.float32) dataRm = np.array(vigra.gaussianSmoothing(dataRm, 3),dtype=np.uint8) dataGm = np.array(dataG[...,1],dtype=np.float32) dataGm = np.array(vigra.gaussianSmoothing(dataGm, 3),dtype=np.uint8) print dataRm.max() dataRG = dataR[:,:,2] * dataG[:,:,1] dataRG = np.array(dataRG,dtype=np.float32) dataRGm = np.array(vigra.gaussianSmoothing(dataRG, 3),dtype=np.uint8) dataRwichtung = np.where(dataR[:,:,2]>np.max(dataR)/10.,dataR[:,:,2],0) dataGwichtung = np.where(dataG[:,:,1]>np.max(dataG)/10.,dataG[:,:,1],0) dataB = np.zeros((dataR.shape)) dataB[:,:,0]=(dataRGm-dataRm*dataGm)*dataRwichtung*dataGwichtung dataB = dataB *255./np.max(dataB) dataB = np.array(dataB, dtype=np.uint8) dataR=originaldataR+ dataB dataR = np.array(dataR,dtype=np.uint8) dataG = np.array(originaldataG,dtype=np.uint8) #return dataB, np.zeros(originaldataR.shape) return dataR, dataG
affinities = numpy.require(affinities, dtype='float32', requirements=['C']) # load raw import skimage.io raw_path = "/home/tbeier/src/nifty/src/python/examples/multicut/NaturePaperDataUpl/ISBI2012/raw_test.tif" #raw_path = '/home/tbeier/src/nifty/mysandbox/NaturePaperDataUpl/ISBI2012/raw_test.tif' raw = skimage.io.imread(raw_path) raw = raw[z,0:w,0:w] isMergeEdgeOffset = numpy.zeros(offsets.shape[0], dtype='bool') isMergeEdgeOffset[0:2] = True if True: affinities = vigra.gaussianSmoothing(vigra.taggedView(affinities,'xyc'),0.2) with nifty.Timer("jay"): nodeSeg = nifty.graph.agglo.pixelWiseFixation2D( mergePrios= (1.0 - affinities), #vigra.gaussianSmoothing(vigra.taggedView( (1.0 - affinities),'xyc'),0.01), notMergePrios=affinities, #vigra.gaussianSmoothing(vigra.taggedView( (affinities),'xyc'),0.01), offsets=offsets, isMergeEdgeOffset=isMergeEdgeOffset ) #nodeSeg = nodeSeg.reshape(shape) import pylab pylab.imshow(nodeSeg) pylab.show() pylab.imshow(nifty.segmentation.segmentOverlay(raw, nodeSeg, showBoundaries=False))
import vigra from vigra import graphs from vigra import numpy from vigra import Timer from vigra import blockwise as bw numpy.random.seed(42) # input shape = (500, 500, 500) data = numpy.random.rand(*shape).astype('float32') print "make options object" options = bw.BlockwiseConvolutionOptions3D() print type(options) sigma = 1.0 options.stdDev = (sigma, ) * 3 options.blockShape = (128, ) * 3 print "stddev", options.stdDev print "call blockwise filter" with vigra.Timer("AllThread"): res = bw.gaussianSmooth(data, options) with vigra.Timer("1thread"): resRef = vigra.gaussianSmoothing(data, sigma) print numpy.sum(numpy.abs(res - resRef))
def smoothedGradMag(img, sigma, simgaOuter=3.0): gradMag = vigra.filters.gaussianGradientMagnitude(img, sigma=sigma).squeeze() gradMag = vigra.gaussianSmoothing(gradMag, simgaOuter) return gradMag
def makeFeat(rag, raw, labels, show=True): featureExtractor = graphs.gridRagFeatureExtractor(rag, labels) featureExtractor.labels = labels featureExtractor.graph = rag geoFeat = featureExtractor.geometricFeatures() topoFeat = featureExtractor.topologicalFeatures() features = [geoFeat, topoFeat] # ward facs wardness = numpy.array([0.0, 0.1, 0.15, 0.25, 0.5, 1.0], dtype='float32') accFeat = featureExtractor.accumulatedFeatures(raw) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.filters.gaussianGradientMagnitude(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.filters.laplacianOfGaussian(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) for s in [2.0, 3.0, 4.0]: res = vigra.gaussianSmoothing(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) features.append(accFeat) # st ev for s in [2.0, 3.0, 4.0]: res = stEv(raw, s) accFeat = featureExtractor.accumulatedFeatures(res) mean = accFeat[:, 0] ucm = featureExtractor.ucmTransformFeatures(mean[:, None], wardness) features.extend([accFeat, ucm]) if False: for x in range(ucm.shape[1]): rag.showEdgeFeature(raw, ucm[:, x]) vigra.show() # hessian ev for s in [1.0, 3.0, 4.0, 6.0, 8.0]: img = hessianEv2(raw, s, 2.0) #vigra.imshow(img) #vigra.show() accFeat = featureExtractor.accumulatedFeatures(img) mean = accFeat[:, 0] ucm = featureExtractor.ucmTransformFeatures(mean[:, None], wardness) features.extend([accFeat, ucm]) if False: for x in range(ucm.shape[1]): print "x", x rag.showEdgeFeature(raw, ucm[:, x]) vigra.show() #break # features = numpy.concatenate(features, axis=1) return features
def _prepare_data(idx, batch_idx): # -------------------------------------------------------------------------------------------------------------- # Preparation of the image data if len(im_list_raw) > idx: # Check whether the result exists if a target folder is given here if target_folder is not None: if os.path.isfile(os.path.join(target_folder, os.path.split(im_list_raw[idx])[1])): # Return Nones for each slice that was already computed if verbose: print('{} already exists, nothing to do...'.format(os.path.split(im_list_raw[idx])[1])) return None, None, None, None # Load the raw data slice im = imread(im_list_raw[idx]) assert im.dtype in ['uint8', 'uint16'], 'Only the data types uint8 and uint16 are supported!' else: # This happens if the number of images does not divide by the number of batches (smaller last batch) # Return Nones for each missing slice to fill up the batch if verbose: print('Filling up batch...') return None, None, None, None # Start end end indices for the median filter, idx being the current slice position in the center start_id = idx - median_radius end_id = idx + median_radius + 1 # Load the necessary data for the z-median filtered slice for load_idx, slice_idx in enumerate(range(start_id, end_id)): load_idx += idx - batch_idx # print('load_idx = {}; slice_idx = {}'.format(load_idx, slice_idx)) if 0 <= slice_idx < len(im_list_pre): if template_data[load_idx] is None: template_data[load_idx] = imread(im_list_pre[slice_idx]) assert template_data[load_idx].dtype == 'uint8', 'Only 8bit template data is supported!' # else: # print('Template data is not None') # else: # print('slice_idx < 0') # Do the median smoothing to obtain one composition image at the current z-position ims = [x for x in template_data[idx - batch_idx: idx - batch_idx + median_radius * 2 + 1] if x is not None] median_z = np.median(ims, axis=0).astype('uint8') del ims # The sift doesn't like images of different size. This fixes some cases by cropping away areas from the raw data # that are zero and padding it with zeros to the size of the template image # We are assuming here that the template data slices are larger than the non-zero region of interest in the raw # data. if im.shape != median_z.shape: try: bounds = _crop_zero_padding(im) cropped_im = im[bounds] t_im = np.zeros(median_z.shape, dtype=im.dtype) t_im[0:cropped_im.shape[0], 0:cropped_im.shape[1]] = cropped_im im = t_im except ValueError as e: # This happened when a white slice was present (no zero pixel) warnings.warn('Cropping zero-padding failed with ValueError: {}'.format(str(e))) print('This happens if a completely black slice is present in the data. ') print('Affected slice: {}'.format(im_list_raw[idx])) print('Replacing with empty slice ...') im = np.zeros(median_z.shape, dtype=im.dtype) # Gaussian smooth the image and the median_z for the SIFT step if sift_sigma is not None: median_z_smooth = gaussianSmoothing(median_z, sift_sigma) if im.dtype == 'uint8': im_smooth = gaussianSmoothing(im, sift_sigma) else: # vigra.gaussianSmoothing cannot handle 16 bit data im_smooth = gaussianSmoothing(im.astype('float32'), sift_sigma) # The data for the sift step does not require to be 16 bit im_smooth = (im_smooth / (2 ** 16) * (2 ** 8)).astype('uint8') else: median_z_smooth = median_z.copy() im_smooth = im.copy() if im_smooth.dtype == 'uint16': # The data for the sift step does not require to be 16 bit im_smooth = (im_smooth.astype('float32') / (2 ** 16) * (2 ** 8)).astype('uint8') # Downsample for SIFT step for speed-up of computation if sift_downsample is not None: median_z_smooth = downscale_local_mean(median_z_smooth, sift_downsample).astype('uint8') im_smooth = downscale_local_mean(im_smooth, sift_downsample).astype('uint8') # -------------------------------------------------------------------------------------------------------------- # Return the results assert im.dtype in ['uint8', 'uint16'] assert im_smooth.dtype == 'uint8' assert median_z.dtype == 'uint8' assert median_z_smooth.dtype == 'uint8' return im, im_smooth, median_z, median_z_smooth
if imgBig is None: imgBig=vigra.sampling.resize(img,cgp.shape) #cgp2d.visualize(imgBig,cgp=cgp) print "accumulate cell " hist = cgp.accumulateCellHistogram(cellType=2,image=img,binCount=binCount,sigma=sigma) hist = hist.reshape([cgp.numCells(2),-1]) for c in range(histImg.shape[2]): histImg[:,:,c] += (size)*cgp.featureToImage(cellType=2,features=hist[:,c],ignoreInactive=False,useTopologicalShape=False) histImg=numpy.require(histImg,dtype=numpy.float32) histImg=vigra.taggedView(histImg, 'xyc') histImg = vigra.gaussianSmoothing(histImg,sigma=1.0) #for c in range(histImg.shape[2]): # #print c # pylab.imshow( numpy.swapaxes( norm01(histImg[:,:,c]) ,0,1) ) # pylab.show() # # print "hist",hist.shape imgdt = rdp.deepDetexturize(srcImg=img,img=histImg,nIteration=10, nCluster=10,reductionAlg='pca',nldEdgeThreshold=10.0,nldScale=10.0,distance=None)#'cityblock')
print "lab type in gm shape" ,img.shape basename=os.path.basename(filename) img=imgRGB seedMapTemp=vigra.filters.gaussianGradientMagnitude(img,sigma=sigmaSeed) seedMapTemp2=seedMapTemp.copy() integralSmooth=5 threshold=2 seedMapIntegral = vigra.gaussianSmoothing(seedMapTemp,integralSmooth) vigra.impex.writeImage(seedMapTemp,outprefix+basename+".gausmag.jpg") seedMapTemp=seedMapTemp**2 seedMapTemp[numpy.where(seedMapIntegral<threshold)]=0 seedMap = vigra.gaussianSmoothing(seedMapTemp, smoothSeedScale) vigra.impex.writeImage((seedMap-seedMap.min())/(seedMap.max()-seedMap.min())*255,outprefix+basename+".smoothgausmag.jpg") seg,numberOfRegions=vigra.analysis.watersheds(seedMap) seg=seg-1 seg=numpy.squeeze(seg.astype(numpy.uint64)) rag=opengm.RegionGraph(seg,numberOfRegions,False) boundaryPixels=[]
def calcObjectMatrix(self, s, M): # Features, Labels, Data O = [] for i in range(len(self.gapL)): x, y, z = self.gapL[i] f = self.SpecialFeatures[i] o = {} F = [] x0, x1 = max(0, x - M), min(s[0], x + M) y0, y1 = max(0, y - M), min(s[1], y + M) z0, z1 = max(0, z - M), min(s[2], z + M) tmp_volume_pre = self.pred_volume[x0:x1, y0:y1, z0:z1, :] tmp_volume_raw = self.raw[x0:x1, y0:y1, z0:z1] tmp_Feature = [] [xp1, yp1, zp1, xp2, yp2, zp2, xo1, yo1, zo1, xo2, yo2, zo2, t1, t2] = f # Endpoint Atribute (1) x1 = numpy.array([xp1, yp1, zp1]) o1 = numpy.array([xo1, yo1, zo1]) t1 = numpy.float16(t1) # Endpoint Atribute (2) x2 = numpy.array([xp2, yp2, zp2]) o2 = numpy.array([xo2, yo2, zo2]) t2 = numpy.float16(t2) # Pair Features c = (x1 + x2) / 2 # centre d = numpy.linalg.norm(x1 - x2) # distance k1 = (x1 - c) / numpy.linalg.norm(x1 - c) # vector pointing to center (1) k2 = (x2 - c) / numpy.linalg.norm(x2 - c) # vector pointing to center (2) # Meassure if orientation of endpoints is alligned [0: <- -> , 1: -> <-] A = 1 - (2 + numpy.dot(k1, o1) + numpy.dot(k2, o2)) / 4 # [0,1] # for i in numpy.arange(4,13,2): # tmp_Feature.append(rayFeatures(tmp_volume_pre[:,:,:,0],i)) F.append(d) F.append(t1 / t2) F.append(t1 + t2) F.append(A) for i in numpy.arange(0.5, 6, 0.5): tmp_Feature.append(vigra.gaussianSmoothing(tmp_volume_pre[:, :, :, 0], i)) tmp_Feature.append(vigra.gaussianSmoothing(tmp_volume_pre[:, :, :, 1], i)) tmp_Feature.append(vigra.gaussianSmoothing(tmp_volume_pre[:, :, :, 2], i)) for v in tmp_Feature: # ToDo: change to interpolated line not just horizontal mean!! s1, s2, s3 = v.shape c = v[s1 / 2, s2 / 2, s3 / 2] l = numpy.mean(v[s1 / 2, s2 / 2, :]) d = numpy.mean(v[:, :, s3 / 2]) F.append(c) F.append(l) F.append(d) o["Features"] = F o["Position"] = [x, y, z] o["Label"] = -1 o["Prediction"] = [[0]] o["Data"] = tmp_volume_raw O.append(o) O.reverse() # f=numpy.array(f) # else: # return 0,0 # return f,tmp_volume_raw return O
from vigra import blockwise as bw numpy.random.seed(42) # input shape = (500, 500, 500) data = numpy.random.rand(*shape).astype('float32') print "make options object" options = bw.BlockwiseConvolutionOptions3D() print type(options) sigma = 1.0 options.stdDev = (sigma, )*3 options.blockShape = (128, )*3 print "stddev",options.stdDev print "call blockwise filter" with vigra.Timer("AllThread"): res = bw.gaussianSmooth(data, options) with vigra.Timer("1thread"): resRef = vigra.gaussianSmoothing(data, sigma) print numpy.sum(numpy.abs(res-resRef))
def preprocess_for_bgsmoothing_pedunculus( labeling ): labeling_copy = np.zeros( labeling.shape ) labeling_copy[np.where(labeling == 0)] = 254. labeling_copy[np.where(labeling == 255)] = 0. labeling_smooth = vigra.gaussianSmoothing(labeling_copy.astype(np.float32) , sigma = (2.2,2.2,0.1) ) labeling_smooth[labeling_smooth > 50] = 255 labeling_smooth[labeling_smooth <= 50] = 0 # smooth the edges, where superpixel are spilling out... labeling[405:,445:,3][np.where(labeling_smooth[405:,445:,3] == 0)] = 255 labeling[405:,445:,3][np.where(labeling_smooth[405:,445:,3] == 255)] = 0 labeling[450:,315:,6][np.where(labeling_smooth[450:,315:,6] == 0)] = 255 labeling[450:,315:,6][np.where(labeling_smooth[450:,315:,6] == 255)] = 0 labeling[390:410,470:,9][np.where(labeling_smooth[390:410,470:,9] == 0)] = 255 labeling[390:410,470:,9][np.where(labeling_smooth[390:410,470:,9] == 255)] = 0 labeling[415:,415:,10][np.where(labeling_smooth[415:,415:,10] == 0)] = 255 labeling[415:,415:,10][np.where(labeling_smooth[415:,415:,10] == 255)] = 0 labeling[370:,415:,11][np.where(labeling_smooth[370:,415:,11] == 0)] = 255 labeling[370:,415:,11][np.where(labeling_smooth[370:,415:,11] == 255)] = 0 labeling[:50,460:,11][np.where(labeling_smooth[:50,460:,11] == 0)] = 255 labeling[:50,460:,11][np.where(labeling_smooth[:50,460:,11] == 255)] = 0 labeling[180:400,470:,13][np.where(labeling_smooth[180:400,470:,13] == 0)] = 255 labeling[180:400,470:,13][np.where(labeling_smooth[180:400,470:,13] == 255)] = 0 labeling[475:,150:165,18][np.where(labeling_smooth[475:,150:165,18] == 0)] = 255 labeling[475:,150:165,18][np.where(labeling_smooth[475:,150:165,18] == 255)] = 0 labeling[395:430,300:360,21][np.where(labeling_smooth[395:430,300:360,21] == 0)] = 255 labeling[395:430,300:360,21][np.where(labeling_smooth[395:430,300:360,21] == 255)] = 0 labeling[480:,365:395,24][np.where(labeling_smooth[480:,365:395,24] == 0)] = 255 labeling[480:,365:395,24][np.where(labeling_smooth[480:,365:395,24] == 255)] = 0 labeling[140:160,485:,25][np.where(labeling_smooth[140:160,485:,25] == 0)] = 255 labeling[140:160,485:,25][np.where(labeling_smooth[140:160,485:,25] == 255)] = 0 labeling[470:,360:,27][np.where(labeling_smooth[470:,360:,27] == 0)] = 255 labeling[470:,360:,27][np.where(labeling_smooth[470:,360:,27] == 255)] = 0 labeling[460:,350:,28][np.where(labeling_smooth[460:,350:,28] == 0)] = 255 labeling[460:,350:,28][np.where(labeling_smooth[460:,350:,28] == 255)] = 0 # add missing membrane labels at the boundaries labeling[0,174,0] = 0 labeling[0,175,0] = 0 labeling[0,212,9] = 0 labeling[0,213,9] = 0 labeling[472,0,9] = 0 labeling[473,0,9] = 0 labeling[0,192,18] = 0 labeling[0,193,18] = 0 labeling[511,330,18] = 0 labeling[511,331,18] = 0 labeling[0,384,18] = 0 labeling[0,385,18] = 0 labeling[1,384,18] = 0 labeling[1,385,18] = 0 labeling[0,215,20] = 0 labeling[0,216,20] = 0 labeling[0,67,20] = 0 labeling[0,68,20] = 0 labeling[482,0,20] = 0 labeling[483,0,20] = 0 labeling[0,248,21] = 0 labeling[0,249,21] = 0 labeling[0,212,21] = 0 labeling[0,213,21] = 0 labeling[1,212,21] = 0 labeling[1,213,21] = 0 return labeling
affinities = affinities[:, z, 0:w, 0:w] affinities = numpy.rollaxis(affinities, 0, 3) affinities = numpy.require(affinities, dtype='float32', requirements=['C']) # load raw import skimage.io raw_path = "/home/tbeier/src/nifty/src/python/examples/multicut/NaturePaperDataUpl/ISBI2012/raw_test.tif" #raw_path = '/home/tbeier/src/nifty/mysandbox/NaturePaperDataUpl/ISBI2012/raw_test.tif' raw = skimage.io.imread(raw_path) raw = raw[z, 0:w, 0:w] isMergeEdgeOffset = numpy.zeros(offsets.shape[0], dtype='bool') isMergeEdgeOffset[0:2] = True if True: affinities = vigra.gaussianSmoothing(vigra.taggedView(affinities, 'xyc'), 0.2) with nifty.Timer("jay"): nodeSeg = nifty.graph.agglo.pixelWiseFixation2D( mergePrios=( 1.0 - affinities ), #vigra.gaussianSmoothing(vigra.taggedView( (1.0 - affinities),'xyc'),0.01), notMergePrios= affinities, #vigra.gaussianSmoothing(vigra.taggedView( (affinities),'xyc'),0.01), offsets=offsets, isMergeEdgeOffset=isMergeEdgeOffset) #nodeSeg = nodeSeg.reshape(shape) import pylab pylab.imshow(nodeSeg)
def gaussianSmoothing(image,sigma): inImg = numpy.require(image, dtype=numpy.float32) inImg = vigra.taggedView(inImg, 'xyz') #print inImg.shape, inImg.dtype return vigra.gaussianSmoothing(inImg, sigma=sigma)