def median_filter(Y, r=2): """ Simple median filter. Parameters: Y := the input image r := median filter "radius". Filter will be 2*r+1 in width/height. """ # For now, assume a single 2d image (vs a stack) assert (len(Y.shape) == 2) assert (r >= 1) w = 2 * r + 1 # width of kernel # so we can run the filter on the interior and still be centered # on all pixels of Y. Ym = mirror_edges(Y, r) if True: # This is the built-in version return ST.medfilt2d(Ym, w)[r:-r, r:-r] else: # a hand-rolled implementation Z = numpy.zeros(Y.shape) for row in range(Z.shape[0]): for col in range(Z.shape[1]): tile = Ym[row:row + w, col:col + w] assert (tile.shape[0] == w) assert (tile.shape[1] == w) Z[row, col] = numpy.median(tile) assert (not numpy.any(numpy.isnan(Z))) return Z
def median_filter(Y, r=2): """ Simple median filter. Parameters: Y := the input image r := median filter "radius". Filter will be 2*r+1 in width/height. """ # For now, assume a single 2d image (vs a stack) assert(len(Y.shape)==2) assert(r >= 1) w = 2*r+1 # width of kernel # so we can run the filter on the interior and still be centered # on all pixels of Y. Ym = mirror_edges(Y,r) if True: # This is the built-in version return ST.medfilt2d(Ym,w)[r:-r, r:-r] else: # a hand-rolled implementation Z = numpy.zeros(Y.shape) for row in range(Z.shape[0]): for col in range(Z.shape[1]): tile = Ym[row:row+w, col:col+w] assert(tile.shape[0] == w); assert(tile.shape[1] == w) Z[row,col] = numpy.median(tile) assert(not numpy.any(numpy.isnan(Z))) return Z
def median_2d(data, kernel_size=3): if data.ndim != 2: sys.exit("Only for 2D data!") sx, sy = shape(data) k2 = int(kernel_size) / 2 if 2 * k2 == kernel_size: sys.exit("Kernel size must be odd!") result = copy(data) for k in range(1, k2 + 1): result[k:sx - k, k:sy - k] = medfilt2d(data, kernel_size=2 * k + 1)[k:sx - k, k:sy - k] return result
def run(self): """ Runs the acquisition loop. """ self.display('Processing started') fNames,numOfFiles =self.acquire() self.display("%d files are found " % numOfFiles) self.display("ittx is %d" % self.experiment.ittx) itt = self.experiment.ittx spc = self.experiment.spacing_X s2nm = self.experiment.S2N_Type s2n = self.experiment.S2N_Value sclt = self.experiment.scale outl = self.experiment.Outlier_Filter jump = self.experiment.jump # for fileind in range(0,numOfFiles,2): fileind = 0 while not self.wants_abort and fileind < numOfFiles: a,b = read_pair_of_images(fNames[fileind],fNames[fileind+1],([0,0,0,0]),itt,spc) sx,sy = a.shape Nfft = 2*itt resRows = (sx-itt)/spc+1 resCols = (sy-itt)/spc+1 res = zeros((resRows*resCols,5)) counter = 0 k = 0 # for k in r_[0:sx-itt+1:spc]: while not self.wants_abort and k <= (sx-itt+1): self.display("\n Working on %d pixel row " % k) for m in r_[0:sy-itt+1:spc]: # self.display(".") a2 = a[k:k+itt,m:m+itt] b2 = b[k:k+itt,m:m+itt] c = cross_correlate(a2,b2,Nfft) [peak1,peak2,peakx,peaky] = find_displacement(c,s2nm) # find integer displacement s2n = 1 [peakx,peaky,s2n] = sub_pixel_velocity(c,peakx,peaky,peak1,peak2,s2n,sclt,itt) ## Scale the pixel displacement to the velocity u = (itt-peaky)*sclt v = (itt-peakx)*sclt x = m+itt/2 y = k+itt/2 res[counter][:] = [x,y,u, v, s2n] counter += 1 k += spc # Write unfiltered, raw data row,col = shape(res) # writer = csv.writer(file(fNames[fileind]+'_noflt.txt','w'),dialect='excel',delimiter='\t') savetxt(fNames[fileind][:-4]+'_noflt.txt',res,fmt='%4.3f',delimiter=' ') # for i in xrange(row): # writer.writerow(res[i][:]) # Find outliers uMag = (res[:,2]**2 + res[:,3]**2)**0.5 limit = mean(compress(uMag!=0,uMag))*outl filtres = res.copy() u = filtres[:,2].copy() v = filtres[:,3].copy() u = choose(uMag > limit,(u,0)) v = choose(uMag > limit, (v,0)) # Median filter tmpu = fabs(ravel(medfilt2d(reshape(u,(resRows,resCols)),3))) tmpv = fabs(ravel(medfilt2d(reshape(v,(resRows,resCols)),3))) uLimit = mean(take(tmpu,nonzero(tmpu!=0))) + 3*std(take(tmpu,nonzero(tmpu!=0))) vLimit = mean(take(tmpv,nonzero(tmpv!=0))) + 3*std(take(tmpv,nonzero(tmpv!=0))) u = where(greater(tmpu,uLimit),0,u) v = where(greater(tmpv,vLimit),0,v) filtres[:,2] = u.copy() filtres[:,3] = v.copy() savetxt(fNames[fileind][:-4]+'_flt.txt',filtres,fmt='%4.3f',delimiter=' ') ind = nonzero(logical_and(u == 0,v == 0)) indLoop = 0 while size(ind[0]) > 0: # Loop until no more "holes" are found indLoop += 1 self.display("Interpolation loop %d " % indLoop) for i in ind[0]: # find a "hole" (zero), fill with an average of 5 x 5 neighbours x2 = i/resCols y2 = i - x2 * resCols k = r_[maximum(2,x2)-2:minimum(resCols-3,x2)+2] m = r_[maximum(2,y2)-2:minimum(resRows-3,y2)+2] region = sub2ind(resRows,resCols,k,m) tmpu = take(u,region) tmpu = take(v,region) u[i] = mean(tmpu) v[i] = mean(tmpv) ind = nonzero(logical_and(u == 0,v == 0)) filtres[:,2] = u filtres[:,3] = v savetxt(fNames[fileind][:-4]+'.txt',filtres,fmt='%4.3f',delimiter=' ') self.quiverm(filtres,a,resRows,resCols,scalearrows=50) fileind += 2
j += 4 i += 4 error_map = np.zeros((row, col)) no_ep = 0 for i in range(row): for j in range(col): if ew[i][j] == sw[i][j]: error_map[i][j] = 0 else: error_map[i][j] = 1 no_ep += 1 m1 = float(float(no_ep) / float((n / 4) * (m / 4))) #applying median filter error_map = np.array(error_map, dtype='f') if m1 <= 0.15: error_map = signaltools.medfilt2d(error_map, 3) no_step = 0 sterror_map = error_map mterror_map = error_map for i in range(1, int(n / 4) - 1): count1 = 0 count2 = 0 if error_map[i - 1][0] == 1: count1 += 1 if error_map[i - 1][1] == 1: count1 += 1 if error_map[i][1] == 1: count1 += 1 if error_map[i + 1][1] == 1: count1 += 1 if error_map[i + 1][0] == 1: