def cof(f,s,l): first_frame_dap = netCDF4.Dataset(fetch_gini(hind_time_step = f)) first_frame_grid = gini_grid(first_frame_dap) second_frame_dap = netCDF4.Dataset(fetch_gini(hind_time_step = s)) second_frame_grid = gini_grid(second_frame_dap) first_frame_dap.close() second_frame_dap.close() new_ir = copy.deepcopy(first_frame_grid.fields['IR']) img = new_ir['data'][0,:] level = 200 img[np.where(img > img.max()-level)] = ndimage.median_filter(img, 2)[np.where(img > img.max()-level)] new_ir['data'][0, :] = img first_frame_grid.fields.update({'IR_filt' : new_ir}) new_ir = copy.deepcopy(second_frame_grid.fields['IR']) img = new_ir['data'][0,:] level =200 img[np.where(img > img.max()-level)] = ndimage.median_filter(img, 2)[np.where(img > img.max()-level)] new_ir['data'][0, :] = img second_frame_grid.fields.update({'IR_filt' : new_ir}) umot, vmot = doflow_lk(first_frame_grid, second_frame_grid, winSize = (5,5), filter_len = 5) umot_fb, vmot_fb = doflow_fb(first_frame_grid, second_frame_grid, winSize = (20,20), filter_len = 10, levels = l) second_frame_grid.fields.update({'u' : umot, 'v' : vmot}) second_frame_grid.fields.update({'u_fb' : umot_fb, 'v_fb' : vmot_fb}) return second_frame_grid
def doflow_fb(first_frame, second_frame, winSize = (5,5), filter_len = 10, sig_min = 150, n_iter = 40, levels = 1): im0 = copy.deepcopy(first_frame.fields['IR_filt']['data']) im0[np.where(im0 < sig_min)] = sig_min im1 = copy.deepcopy(second_frame.fields['IR_filt']['data']) im1[np.where(im1 < sig_min)] = sig_min sim0 = (im0 - im0.min())*(im0.max()/(im0.max()-im0.min())) sim1 = (im1 - im1.min())*(im1.max()/(im1.max()-im1.min())) u, v = get_optic_flow_fb(sim0[0], sim1[0], winSize = winSize[0], n_iter=n_iter, levels=levels) t1 = netCDF4.num2date(second_frame.axes['time']['data'][0], units = second_frame.axes['time']['units']) t0 = netCDF4.num2date(first_frame.axes['time']['data'][0], units = first_frame.axes['time']['units']) dt = (t1-t0).seconds dx = np.expand_dims(np.gradient(second_frame.fields['x']['data'])[1], 0) dy = np.expand_dims(np.gradient(second_frame.fields['y']['data'])[0], 0) u_fld = {'data' : dt * ndimage.median_filter(u.reshape([1,u.shape[0], u.shape[1]]),filter_len)/dx, 'units' :'pixels', 'standard_name' : 'disp', 'long name' : 'todo'} v_fld = {'data' : dt * ndimage.median_filter( v.reshape([1,v.shape[0], v.shape[1]]),filter_len)/dy, 'units' :'pixels', 'standard_name' : 'disp', 'long name' : 'todo'} return u_fld, v_fld
def getpic(self,img_name,channel_num): self.img_name = img_name os.system('streamer -c /dev/video'+str(channel_num)+' -f jpeg -o '+ self.img_name + '.jpeg') # print 'streamer -c /dev/video0 -f jpeg -o '+ self.img_name + '.jpeg' img = mpimg.imread(self.img_name+'.jpeg', 'jpeg') ndi.median_filter(img,2) return img
def get_thrustermask(dtheta): """ Identify thruster fire events. Delta theta is the change in telescope roll angle compared to the previous cadence. We establish the typical scatter in delta theta by computing the median absolute deviation of points in regions of 100 measurements. We identfy thruster fires as cadences where the change in telescope roll angle exceeds the median absolute deviation by > 15. Parameters ---------- dtheta : roll angle for contiguous observations Returns ------- thrustermask : boolean array where True means thruster fire """ medfiltwid = 10 # Filter width to identify discontinuities in theta sigfiltwid = 100 # Filter width to establish local scatter in dtheta thresh = 10 # Threshold to call something a thruster fire (units of sigma) medtheta = nd.median_filter(dtheta,medfiltwid) diffdtheta = np.abs(dtheta - medtheta) sigma = nd.median_filter(diffdtheta,sigfiltwid) * 1.5 thrustermask = np.array(diffdtheta > thresh*sigma) idx = np.where(thrustermask)[0] mask_after = idx + 1 # mask cadence after mask_after = mask_after[mask_after < len(thrustermask)] print "{} masked after thruster fire ".format(len(mask_after)) thrustermask[mask_after] = True return thrustermask
def detect_vortices(cloud, radius=70, showplots=False): """ Detects whether there are vortex-like features within a given radius of the peak density in the TOF image of an expanded BEC """ OD = cloud.get_OD() peak_coord = cloud.results['peak coordinates'] center_region = ROI(center=peak_coord, size=(1.5 * radius, 1.5 * radius)).slices smooth_cloud = ndi.median_filter(OD[center_region], size=4) minOD = smooth_cloud.min() maxOD = smooth_cloud.max() cloud_median = ndi.median_filter(smooth_cloud, size=10) belowthresh = where(smooth_cloud < cloud_median * 0.75, 1, 0) opened = ndi.binary_opening(belowthresh, iterations=1) closed = ndi.binary_closing(opened, iterations=1) vort_found = ndi.label(closed)[1] cloud.results['vort_found'] = vort_found if showplots == True: fig = plt.figure(1999) fig.add_subplot(221, xticks=[], yticks=[]) plt.imshow(smooth_cloud, interpolation='nearest', vmin=minOD, vmax=maxOD) fig.add_subplot(222, xticks=[], yticks=[]) plt.imshow(cloud_median, interpolation='nearest', vmin=minOD, vmax=maxOD) fig.add_subplot(223, xticks=[], yticks=[]) plt.imshow(closed, interpolation='nearest', cmap=plt.cm.get_cmap('binary')) fig.add_subplot(224, xticks=[], yticks=[]) plt.imshow(belowthresh, interpolation='nearest', cmap=plt.cm.get_cmap('binary')) return vort_found
def detect_current(cloud, showplots=False): """ Detects whether there is a vortex-like signature of persistent current in the center of a TOF image of an expanded ring BEC """ OD = cloud.get_OD() peak_coord = cloud.results['peak coordinates'] center_region = ROI(center=peak_coord, size=(40, 40)).slices cloud_center = ndi.median_filter(OD[center_region], size=2) minOD = cloud_center.min() maxOD = cloud_center.max() cloud_median = ndi.median_filter(cloud_center, size=10) belowthresh = where(cloud_center < cloud_median * 0.75, 1, 0) opened = ndi.binary_opening(belowthresh, iterations=1) closed = ndi.binary_closing(opened, iterations=3) current_found = ndi.label(closed)[1] cloud.results['current_found'] = current_found if showplots == True: fig = plt.figure(1999) fig.add_subplot(221, xticks=[], yticks=[]) plt.imshow(cloud_center, interpolation='nearest', vmin=minOD, vmax=maxOD) fig.add_subplot(222, xticks=[], yticks=[]) plt.imshow(cloud_median, interpolation='nearest', vmin=minOD, vmax=maxOD) fig.add_subplot(223, xticks=[], yticks=[]) plt.imshow(closed, interpolation='nearest', cmap=plt.cm.get_cmap('binary')) fig.add_subplot(224, xticks=[], yticks=[]) plt.imshow(belowthresh, interpolation='nearest', cmap=plt.cm.get_cmap('binary')) return current_found, asum(closed)
def pepareImage(image): """Remove background and fit data to 8-bit range""" image = image.astype(float) #convert to floating point (integers lose 99% of data!) colonies_blurred = spim.median_filter(colonies,2)/spim.median_filter(colonies,20)*numpy.mean(colonies) # remove stongly blurred 'background' from slightly blurred foregound minval = numpy.min(colonies_blurred) colonies_blurred = colonies_blurred - minval maxval = numpy.max(colonies_blurred) colonies_blurred = colonies_blurred * (256/maxval)
def bandpass_m(array, small=2, big=30): ''' Applies a small-radius median filter to the array, then subtracts the result of applying a large-radius median filter. ''' mS = ndi.median_filter(array, size=small) mB = ndi.median_filter(array, size=big) return(mS - mB)
def estimateHSflow(frame1, frame2, lam = 80): H, W = frame1.shape # build the image pyramid pyramid_spacing = 1.0 / 0.8 # pyramid_levels = 1 + np.floor(np.log(min(W, H) / 16.0) / np.log(pyramid_spacing * 1.0)) # pyramid_levels = int(pyramid_levels) pyramid_levels = 4 smooth_sigma = np.sqrt(2.0) pyramid1 = [] pyramid2 = [] pyramid1.append(frame1) pyramid2.append(frame2) t_H = H t_W = W gau_frame1 = frame1 gau_frame2 = frame2 for m in range(1, pyramid_levels): # TODO #1: build Gaussian pyramid for coarse-to-fine optical flow # estimation # use cv2.GaussianBlur t_H = int(t_H*0.8) t_W = int(t_W*0.8) gau_frame1 = cv2.GaussianBlur(gau_frame1,(3,3),smooth_sigma) gau_frame2 = cv2.GaussianBlur(gau_frame2,(3,3),smooth_sigma) gau_frame1 = cv2.resize(gau_frame1,(t_W,t_H)) gau_frame2 = cv2.resize(gau_frame2,(t_W,t_H)) pyramid1.append(gau_frame1) pyramid2.append(gau_frame2) # coarst-to-fine compute the flow uv = np.zeros(((H, W, 2))) for levels in range(pyramid_levels - 1, -1, -1): print "level %d" % (levels) H1, W1 = pyramid1[levels].shape uv = cv2.resize(uv, (W1, H1)) uv = estimateHSflowlayer(pyramid1[levels], pyramid2[levels], uv, lam) # TODO #6: use median filter to smooth the flow result in each level in each iteration # uv[:,:,0] = af.median_filter(uv[:,:,0], 7) uv[:,:,1] = af.median_filter(uv[:,:,1], 7) # uv = uv.astype('uint8') # c1,c2 = cv2.split(uv) # c1 = cv2.medianBlur(c1, 3) # c2 = cv2.medianBlur(c2, 3) # uv = cv2.merge([c1,c2]) # uv = uv.astype('float') return uv
def center_on_peak(self): """ Re-centers the ROI on the peak OD in the present ROI """ OD = self.get_OD() ndi.median_filter(OD, 3) rel_y, rel_x = find_peak_coords(OD) new_center = (self.roi.tblr[0] + rel_y, self.roi.tblr[2] + rel_x) self.recenter(center=new_center) self.results = {} self.analyze()
def _apply_prefilters(self): """ Allows prefiltering of the raw image data before processing. """ if self.proc_info['prefilt'] == 'median': adjacent = ((0.5, 1, 0.5), (1, 1, 1), (0.5, 1, 0.5)) img = ndi.median_filter(self.img, footprint=adjacent) pro = ndi.median_filter(self.pro, footprint=adjacent) bac = ndi.median_filter(self.bac, footprint=adjacent) return (img, pro, bac) else: return(self.img, self.pro, self.bac)
def medianClicked(self): global _img, red, green, blue red = ndimage.median_filter(red, size = 15) green = ndimage.median_filter(green, size = 15) blue = ndimage.median_filter(blue, size =15) for a in range(0, _img.shape[0]): for b in range(0, _img.shape[1]): _img[a, b, 0] = red[a, b] _img[a, b, 1] = green[a, b] _img[a, b, 2] = blue[a, b] updateImage()
def baseline_and_deglitch(orig_spec, ww=300, sigma_cut=1.5, poly_n=2., filt_width=7.): """ (1) Calculate a rolling standard deviation (s) in a window of 2*ww pixels (2) Mask out portions of the spectrum where s is more than sigma_cut times the median value for s. This seems to be mis-calibrated (perhaps not independent?). A value of 1.5 works well to remove all signal. (3) Downsample the masked spectrum (to avoid memory bug) and find the minimum order polynomial baseline that does a good job of fitting the data. (3) Median filter (with a filter width of filt_width) to remove the single-channel spikes seen. """ ya = rolling_window(orig_spec,ww*2) #Calculate standard dev and pad the output stds = my_pad.pad(np.std(ya,-1),(ww-1,ww),mode='edge') #Figure out which bits of the spectrum have signal/glitches med_std = np.median(stds) std_std = np.std(stds) sigma_x_bar = med_std/np.sqrt(ww) sigma_s = (1./np.sqrt(2.))*sigma_x_bar #Mask out signal for baseline masked = ma.masked_where(stds > med_std+sigma_cut*sigma_s,orig_spec) #Down-sample for the polyfit. #For speed, but mostly for memory masked = im.median_filter(masked,filt_width)[::filt_width] xx = np.arange(masked.size) npoly = find_best_baseline(masked,xx) basepoly = fit_baseline(masked,xx,ndeg=npoly) #Some kludgy code to refactor the baseline polynomial to #the full size spectra xxx = np.arange(orig_spec.size) params = np.asarray(basepoly) rr = filt_width newparams = [] for i,p in enumerate(params[::-1]): newparams.append(p/rr**i) newparams = newparams[::-1] newpoly = np.poly1d(newparams) newbaseline = newpoly(xxx) #Subtract off baseline sub = orig_spec-newbaseline #Filter out glitches in baseline-subtracted version final = im.median_filter(sub,filt_width)[::filt_width] return(final)
def process(img,downsample_fact=0.05): # Downsample the image r = [] for i in range(3): r.append(ndimage.zoom(img[:,:,i], downsample_fact)) r = np.dstack(r) rs = r.mean(2) # Extract light/dark boundaries h = ndimage.median_filter(np.max(rs,axis=1), (rs.shape[1]/10.0,)) w = ndimage.median_filter(np.max(rs,axis=0), (rs.shape[0]/50.0,)) # Find the index of the boundaries height_ticks = find_highs(h,min_length=100) width_ticks = find_highs(w,min_length=10) # Extract the sub-images test_imgs_bw = [] test_imgs = [] position = [] for iheight,height_tick in enumerate(height_ticks): h1,h2 = height_tick for iwidth,width_tick in enumerate(width_ticks): w1,w2 = width_tick Ibw = rs[h1:h2,w1:w2] test_imgs_bw.append(Ibw) I = r[h1:h2,w1:w2,:] test_imgs.append(I) position.append((iheight,iwidth)) # Save a copy of the extracted images out_imgs = test_imgs[:] # Shift the sub-images to remove the plug. shifts = [] for test_img, pos in zip(test_imgs, position): plug_data = np.mean(test_img[:,:,0] - test_img.mean(2), axis=1) plug_data = plug_data[2:] - plug_data[:-2] # lagged derivative, smooths things out a little locs = np.argwhere(local_maxima(plug_data[:20])).ravel() # only look at the beginning of the tube for the plug highs = [h+1 for h in locs[np.argsort(plug_data[locs])[::-1]]] if len(highs) == 0: print "NO PLUG FOUND FOR POSITION %s" % str(pos) shifts.append(highs[0]) test_imgs = [I[shift:,:,:] for I,shift in zip(test_imgs, shifts)] # Extract fittable data data = [-np.median(test_img,axis=1).mean(1) for test_img in test_imgs] return data, position, out_imgs, shifts
def local_median_val( u, v, u_threshold, v_threshold, size=1 ): """Eliminate spurious vectors with a local median threshold. This validation method tests for the spatial consistency of the data. Vectors are classified as outliers and replaced with Nan (Not a Number) if the absolute difference with the local median is greater than a user specified threshold. The median is computed for both velocity components. Parameters ---------- u : 2d np.ndarray a two dimensional array containing the u velocity component. v : 2d np.ndarray a two dimensional array containing the v velocity component. u_threshold : float the threshold value for component u v_threshold : float the threshold value for component v Returns ------- u : 2d np.ndarray a two dimensional array containing the u velocity component, where spurious vectors have been replaced by NaN. v : 2d np.ndarray a two dimensional array containing the v velocity component, where spurious vectors have been replaced by NaN. mask : boolean 2d np.ndarray a boolean array. True elements corresponds to outliers. """ um = median_filter( u, size=2*size+1 ) vm = median_filter( v, size=2*size+1 ) ind = (np.abs( (u-um) ) > u_threshold) | (np.abs( (v-vm) ) > v_threshold) u[ind] = np.nan v[ind] = np.nan mask = np.zeros(u.shape, dtype=bool) mask[ind] = True return u, v, mask
def make_gif2(): t = sp.arange(-5.0, 5.0, 0.01) for sigma in range(1, 10): pylab.clf() pylab.figtext(0,0,"sigma={0}".format(2**sigma)) pylab.axis([-6.0, 6.0, -1.1, 1.1]) A = ndi.median_filter(shallow(t), 2.0**sigma) B = ndi.median_filter(steep(t), 2.0**sigma) pylab.plot(t, A, 'b') pylab.plot(t, B, 'r') pylab.plot(t, -1.0 * (A > 0), 'b') pylab.plot(t, (B > 0), 'r') pylab.plot(t, zero(t), 'k') pylab.draw() pylab.savefig('frame{0}'.format(sigma))
def prep_data(dataset): df = dataset.copy() latlon = list(zip(df.lat, df.lon)) dist = np.array([distance(latlon[i + 1], latlon[i]) for i in range(len((latlon[:-1])))]) df["dist"] = np.concatenate(([0], np.cumsum(dist))) slope = np.abs(100 * np.diff(df.alt) / (1000 * dist)) slope[np.where( slope < 4) ] = 0 # "green" slope[np.where((slope >= 4) & (slope < 6))] = 1 # "yellow" slope[np.where((slope >= 6) & (slope < 10))] = 2 # "pink" slope[np.where((slope >= 10) & (slope < 15))] = 3 # "orange" slope[np.where( slope >= 15 )] = 4 # "red" slope = im.median_filter(slope, 6) colors = np.empty_like(slope, dtype=object) colors[np.where(slope == 0)] = "green" colors[np.where(slope == 1)] = "yellow" colors[np.where(slope == 2)] = "pink" colors[np.where(slope == 3)] = "orange" colors[np.where(slope == 4)] = "red" df["colors"] = list(colors) + [None] # NOTE: add [None] just make pandas happy return df
def _rs_large(sinogram, snr, size, matindex): """ Remove large stripes by: locating stripes, normalizing to remove full stripes, using the sorting technique to remove partial stripes. """ badpixelratio = 0.05 (nrow, ncol) = sinogram.shape ndrop = np.int16(badpixelratio * nrow) sinosorted = np.sort(sinogram, axis=0) sinosmoothed = median_filter(sinosorted, (1, size)) list1 = np.mean(sinosorted[ndrop:nrow - ndrop], axis=0) list2 = np.mean(sinosmoothed[ndrop:nrow - ndrop], axis=0) listfact = list1 / list2 # Locate stripes listmask = _detect_stripe(listfact, snr) listmask = binary_dilation(listmask, iterations=1).astype(listmask.dtype) matfact = np.tile(listfact, (nrow, 1)) # Normalize sinogram = sinogram / matfact sinogram1 = np.transpose(sinogram) matcombine = np.asarray(np.dstack((matindex, sinogram1))) matsort = np.asarray( [row[row[:, 1].argsort()] for row in matcombine]) matsort[:, :, 1] = np.transpose(sinosmoothed) matsortback = np.asarray( [row[row[:, 0].argsort()] for row in matsort]) sino_corrected = np.transpose(matsortback[:, :, 1]) listxmiss = np.where(listmask > 0.0)[0] sinogram[:, listxmiss] = sino_corrected[:, listxmiss] return sinogram
def remove_large_stripe(self, matindex, sinogram, snr, size): """ Algorithm 5 in the paper. Use to remove large stripes --------- Parameters: - sinogram: 2D array. - snr: ratio used to discriminate between useful information and noise. - size: window size of the median filter. --------- Return: - stripe-removed sinogram. """ badpixelratio = 0.05 (nrow, ncol) = sinogram.shape ndrop = np.int16(badpixelratio * nrow) sinosorted = np.sort(sinogram, axis=0) sinosmoothed = median_filter(sinosorted, (1, size)) list1 = np.mean(sinosorted[ndrop:nrow - ndrop], axis=0) list2 = np.mean(sinosmoothed[ndrop:nrow - ndrop], axis=0) listfact = list1 / list2 listmask = self.detect_stripe(listfact, snr) listmask = binary_dilation(listmask, iterations=1).astype(listmask.dtype) matfact = np.tile(listfact,(nrow,1)) sinogram = sinogram / matfact sinogram1 = np.transpose(sinogram) matcombine = np.asarray(np.dstack((matindex, sinogram1))) matsort = np.asarray( [row[row[:, 1].argsort()] for row in matcombine]) matsort[:, :, 1] = np.transpose(sinosmoothed) matsortback = np.asarray( [row[row[:, 0].argsort()] for row in matsort]) sino_corrected = np.transpose(matsortback[:, :, 1]) listxmiss = np.where(listmask > 0.0)[0] sinogram[:, listxmiss] = sino_corrected[:, listxmiss] return sinogram
def _rs_dead(sinogram, snr, size, matindex): """ Remove unresponsive and fluctuating stripes. """ sinogram = np.copy(sinogram) # Make it mutable (nrow, _) = sinogram.shape sinosmoothed = np.apply_along_axis(uniform_filter1d, 0, sinogram, 10) listdiff = np.sum(np.abs(sinogram - sinosmoothed), axis=0) nmean = np.mean(listdiff) listdiffbck = median_filter(listdiff, size) listdiffbck[listdiffbck == 0.0] = nmean listfact = listdiff / listdiffbck listmask = _detect_stripe(listfact, snr) listmask = binary_dilation(listmask, iterations=1).astype(listmask.dtype) listmask[0:2] = 0.0 listmask[-2:] = 0.0 listx = np.where(listmask < 1.0)[0] listy = np.arange(nrow) matz = sinogram[:, listx] finter = interpolate.interp2d(listx, listy, matz, kind='linear') listxmiss = np.where(listmask > 0.0)[0] if len(listxmiss) > 0: matzmiss = finter(listxmiss, listy) sinogram[:, listxmiss] = matzmiss # Use algorithm 5 to remove residual stripes sinogram = _rs_large(sinogram, snr, size, matindex) return sinogram
def filter_fmap(in_file, in_mask=None, out_file=None): from pyacwereg.filters import wavelets_denoise, laplacian_filter import numpy as np import nibabel as nb import os.path as op from math import pi from scipy.ndimage import median_filter if out_file is None: fname, fext = op.splitext(op.basename(in_file)) if fext == '.gz': fname, _ = op.splitext(fname) out_file = op.abspath('./%s_filtered.nii.gz' % fname) filtered = wavelets_denoise(laplacian_filter(in_file, in_mask)) im = nb.load(filtered) result = im.get_data() result = median_filter(result, 10) result -= np.median(result) result *= (pi / np.percentile(result, 99.97)) nb.Nifti1Image(result, im.get_affine(), im.get_header()).to_filename(out_file) return out_file
def center_galaxy(image, original_image, centroid=None): if centroid is None: # apply median filter to find the galaxy centroid centroid = median_filter(image, size=10).argmax() centroid = np.unravel_index(centroid, image.shape) # recenter image roffset = centroid[0] - image.shape[0] / 2 if roffset < 0: # add more white space to top of image extra_rows = image.shape[0] - 2 * centroid[0] image = np.vstack((np.zeros((extra_rows, image.shape[1])), image)) elif roffset > 0: # add more white space to bottom of image extra_rows = 2 * centroid[0] - image.shape[0] image = np.vstack((image, np.zeros((extra_rows, image.shape[1])))) coffset = centroid[1] - image.shape[1] / 2 if coffset > 0: # add more white space to right of image extra_columns = 2 * centroid[1] - image.shape[1] image = np.column_stack((image, np.zeros((image.shape[0], extra_columns)))) elif coffset < 0: # add more white space to left of image extra_columns = image.shape[1] - 2 * centroid[1] image = np.column_stack((np.zeros((image.shape[0], extra_columns)), image)) return image, centroid
def grey_processing(inputImg): # fp = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] fp = np.ones((3, 3)) data = nd.median_filter(inputImg, size=7) data = nd.grey_closing(data, footprint=fp) data = nd.grey_opening(data,footprint=fp) return data
def bfixpix(image_file, mask_file, outsuffix='_f', msksuffix='_s'): """ Inputs --------- image_file : string input image file to fix bad pixels on mask_file : string mask file (0 == good pixels, >0 == bad pixels outsuffix : string suffix for fixed image. default = '_f' msksuffix : string suffix for bad pixels significance mask. default = '_s' """ outf = image_file.replace('.fits', outsuffix + '.fits') outm = image_file.replace('.fits', msksuffix + '.fits') util.rmall([outf, outm]) print("bfixpix: {0} -> {1}".format(image_file, outf)) # fetch the image, fetch the mask img, hdr = fits.getdata(image_file, header=True) msk = fits.getdata(mask_file) # median the image medimg = ndimage.median_filter(img, 3, mode='nearest') # generate the pixel files outf_img = np.where(msk == 0, img, medimg) outm_img = np.where(msk == 1, (img - medimg), 0) fits.writeto(outf, outf_img, hdr) fits.writeto(outm, outm_img, hdr)
def testAcquire(self): im = self.acquireImage() if im is None: return # filter im = numpy.asarray(im, dtype=numpy.float32) medfilt = int(self.settings['medfilt']) lowfilt = float(self.settings['lowfilt']) if medfilt > 1: im = ndimage.median_filter(im, size=medfilt) if lowfilt > 0: im = ndimage.gaussian_filter(im, lowfilt) self.setImage(im) # find regions minsize = self.settings['minsize'] maxsize = self.settings['maxsize'] timeout = 300 #regions, image = libCVwrapper.FindRegions(im, minsize, maxsize) self.logger.info('running libCV.FindRegions, timeout = %d' % (timeout,)) try: regions,image = pyami.timedproc.call('leginon.libCVwrapper', 'FindRegions', args=(im,minsize,maxsize), timeout=timeout) except: self.logger.error('libCV.FindRegions failed') regions = [] image = None # this is copied from targetfinder: #regions,image = libCVwrapper.FindRegions(self.mosaicimage, minsize, maxsize) n = len(regions) self.logger.info('Regions found: %s' % (n,)) self.displayRegions(regions)
def Highpass(image): """ Returns: image after passing it through a highpass filter. ---------------------------------------------------------------------- Parameters: image """ return image - median_filter(image,size=9)
def remove_unresponsive_and_fluctuating_stripe(self, sinogram, snr, size): """ Algorithm 6 in the paper. Remove unresponsive or fluctuating stripes. --------- Parameters: - sinogram: 2D array. - snr: ratio used to discriminate between useful information and noise - size: window size of the median filter. --------- Return: - stripe-removed sinogram. """ (nrow, _) = sinogram.shape sinosmoothed = np.apply_along_axis(uniform_filter1d, 0, sinogram, 10) listdiff = np.sum(np.abs(sinogram - sinosmoothed), axis=0) nmean = np.mean(listdiff) listdiffbck = median_filter(listdiff, size) listdiffbck[listdiffbck == 0.0] = nmean listfact = listdiff / listdiffbck listmask = self.detect_stripe(listfact, snr) listmask = binary_dilation(listmask, iterations=1).astype(listmask.dtype) listmask[0:2] = 0.0 listmask[-2:] = 0.0 listx = np.where(listmask < 1.0)[0] listy = np.arange(nrow) matz = sinogram[:, listx] finter = interpolate.interp2d(listx, listy, matz, kind='linear') listxmiss = np.where(listmask > 0.0)[0] if len(listxmiss) > 0: matzmiss = finter(listxmiss, listy) sinogram[:, listxmiss] = matzmiss return sinogram
def processAndSaveFFT(self, imgdata, fftpath): if os.path.isfile(fftpath): print "FFT file found" if fftpath in self.freqdict.keys(): print "Freq found" return False print "Freq not found" print "creating FFT file: ", fftpath ### downsize and filter leginon image if self.params['uncorrected']: imgarray = imagefilter.correctImage(imgdata, params) else: imgarray = imgdata['image'] ### calculate power spectra apix = apDatabase.getPixelSize(imgdata) fftarray, freq = ctfpower.power(imgarray, apix, mask_radius=0.5, fieldsize=self.params['fieldsize']) #fftarray = imagefun.power(fftarray, mask_radius=1) fftarray = ndimage.median_filter(fftarray, 2) ## preform a rotational average and remove peaks rotfftarray = ctftools.rotationalAverage2D(fftarray) stdev = rotfftarray.std() rotplus = rotfftarray + stdev*4 fftarray = numpy.where(fftarray > rotplus, rotfftarray, fftarray) ### save to jpeg self.freqdict[fftpath] = freq mrc.write(fftarray, fftpath) self.saveFreqFile() return True
def process_frames(self, data): """ Algorithm 5 in the paper. Remove large stripes by: locating stripes, normalizing to remove full stripes, using the sorting technique to remove partial stripes. """ sinogram = np.copy(data[0]) badpixelratio = 0.05 # To avoid false detection ndrop = np.int16(badpixelratio * self.height1) sinosorted = np.sort(sinogram, axis=0) sinosmoothed = median_filter(sinosorted, (1, self.size)) list1 = np.mean(sinosorted[ndrop:self.height1 - ndrop], axis=0) list2 = np.mean(sinosmoothed[ndrop:self.height1 - ndrop], axis=0) listfact = list1 / list2 listmask = self.detect_stripe(listfact, self.snr) listmask = binary_dilation(listmask, iterations=1).astype(listmask.dtype) matfact = np.tile(listfact,(self.height1,1)) sinogram = sinogram / matfact sinogram1 = np.transpose(sinogram) matcombine = np.asarray(np.dstack((self.matindex, sinogram1))) matsort = np.asarray( [row[row[:, 1].argsort()] for row in matcombine]) matsort[:, :, 1] = np.transpose(sinosmoothed) matsortback = np.asarray( [row[row[:, 0].argsort()] for row in matsort]) sino_corrected = np.transpose(matsortback[:, :, 1]) listxmiss = np.where(listmask > 0.0)[0] sinogram[:, listxmiss] = sino_corrected[:, listxmiss] return sinogram
def blur_predict(model, X, type="median", filter_size=3, sigma=1.0): if type == "median": blured_X = np.array(list(map(lambda x: ndimage.median_filter(x, filter_size), X))) elif type == "gaussian": blured_X = np.array(list(map(lambda x: ndimage.gaussian_filter(x, filter_size), X))) elif type == "f_gaussian": blured_X = np.array(list(map(lambda x: filters.gaussian_filter(x.reshape((28, 28)), sigma=sigma).reshape(784), X))) elif type == "tv_chambolle": blured_X = np.array(list(map(lambda x: restoration.denoise_tv_chambolle(x.reshape((28, 28)), weight=0.2).reshape(784), X))) elif type == "tv_bregman": blured_X = np.array(list(map(lambda x: restoration.denoise_tv_bregman(x.reshape((28, 28)), weight=5.0).reshape(784), X))) elif type == "bilateral": blured_X = np.array(list(map(lambda x: restoration.denoise_bilateral(np.abs(x).reshape((28, 28))).reshape(784), X))) elif type == "nl_means": blured_X = np.array(list(map(lambda x: restoration.nl_means_denoising(x.reshape((28, 28))).reshape(784), X))) elif type == "none": blured_X = X else: raise ValueError("unsupported filter type", type) return predict(model, blured_X)
def run(self, ips, snap, img, para = None): nimg.median_filter(snap, para['size'], output=img)
f = 530 time, data = sft.ctime2data(data_dir, ti, tf) spec = data[0] # BE SURE TO LOOK AT THE POL11 STUFF TOO!! # show only this freq range in the rfi removed plot and SVD plot plot_if = 0 plot_ff = 2100 logdata = np.log10(spec) median_f = np.median(logdata, axis=0) flattened = logdata - median_f filtered = median_filter(flattened, [1, med_win]) noisy_corrected = flattened - filtered corrected = uniform_filter(noisy_corrected, uni_win) MAD = np.median(np.abs(corrected)) flags = (corrected > sensitivity * MAD) rfi_removed = np.ma.masked_where(flags, corrected) rfi_occ_freq = np.sum(flags, axis=0) / flags.shape[0] rfi_occ_time = np.sum(flags, axis=1) / flags.shape[1] rfi_removed_dB = 10 * rfi_removed
def PSNR(original, compressed): # This function for calculating PSNR value mse = np.mean((original - compressed)**2) maxl = 255.0 psnr = 10 * log10(maxl**2 / mse) return psnr img = mpimg.imread("noisy.jpg") # For reading the image noisy = np.array(img, dtype=float) # Convert to floating point img = mpimg.imread("original.jpg") # For reading the image original = np.array(img, dtype=float) # Convert to floating point # We use constant mode with 0 because default value of median filter on matlab is zeros as padding option result = ndimage.median_filter(noisy, size=3, mode='constant', cval=0) result2 = ndimage.median_filter(result, size=3, mode='constant', cval=0) plt.gray() fig, axs = plt.subplots(2, 2) axs[0, 0].imshow(original) axs[0, 0].set_title("Original Image") axs[0, 1].imshow(noisy) axs[0, 1].set_title("Noisy Image") axs[1, 0].imshow(result) axs[1, 0].set_title("After first filter") axs[1, 1].imshow(result2) axs[1, 1].set_title("After second filter") plt.show() print("PSNR value between original and noisy image : ", end="")
def run(self, show=False): # Load data filename = self.filename d = pyfits.getdata(filename) h = pyfits.getheader(filename) path, filename = os.path.split(filename) # Get wavelength calibration z = np.arange(h['naxis3']) w = h['CRVAL3'] + h['CDELT3'] * (z - h['CRPIX3']) # Signal-to-noise clipping s = d.sum(axis=2) s = s.sum(axis=1) gauss_pw, _ = self.fit_gaussian(z, s) log.debug("Gaussian parameters ---") log.debug("p[0] = %.2f" % gauss_pw[0]) log.debug("p[1] = %.2f" % gauss_pw[1]) log.debug("p[2] = %.2f" % gauss_pw[2]) log.debug("p[3] = %.2f" % gauss_pw[3]) lor_p = self.fit_lorentzian(z, s) log.debug("Lorentzian parameters ---") log.debug("p[0] = %.2f" % lor_p[0]) log.debug("p[1] = %.2f" % lor_p[1]) log.debug("p[2] = %.2f" % lor_p[2]) log.debug("p[3] = %.2f" % lor_p[3]) fwhm = np.abs(gauss_pw[2] * 2 * np.sqrt(2 * np.log(2))) filter_ = np.where(np.abs(z - gauss_pw[1]) < fwhm, True, False) if show: plt.plot(z, self.gaussian(gauss_pw, z), 'r-', lw=2, label='Gaussian Fit') plt.plot(z, self.lorentzian(lor_p, z), 'b-', lw=2, label='Lorentzian Fit') plt.plot(z, s, 'ko') plt.plot(z[filter_], s[filter_], 'ro') plt.title('Cube collapsed in XY and fits.') plt.grid() plt.legend(loc='best') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) plt.show() signal = d[filter_].mean(axis=0) noise = d[np.logical_not(filter_)].mean(axis=0) target_snr = 3 snr = signal / noise snr = ndimage.median_filter(snr, 3) snr_mask = np.where(signal > target_snr * noise, True, False) snr_laplacian = ndimage.morphological_laplace(snr * snr_mask, size=3) snr_mask *= np.where(np.abs(snr_laplacian) < 5.0, True, False) snr_mask = ndimage.binary_opening(snr_mask, iterations=5) snr_mask = ndimage.binary_closing(snr_mask, iterations=5) # SNR MASK Based on circular aperture # aperture_radius = 1 # arcmin # aperture_radius = aperture_radius / 60 # arcmin to deg # aperture_radius = np.abs(aperture_radius / h['CD1_1']) # deg to pix # print(aperture_radius) # c = SkyCoord('7:41:55.400', '-18:12:33.00', frame=h['RADECSYS'].lower(), unit=(u.hourangle, u.deg)) # x, y = np.arange(h['NAXIS1']), np.arange(h['NAXIS2']) # X, Y = np.meshgrid(x, y) # center_wcs = wcs.WCS(h) # center = center_wcs.wcs_world2pix(c.ra.deg, c.dec.deg, 6563, 1) # snr_mask = np.sqrt((X - center[0]) ** 2 + (Y - center[1]) ** 2) # snr_mask = np.where(snr_mask < aperture_radius, True, False) # plt.imshow(snr_mask) # plt.show() # SNR MASK Based on squared area aperture_width = 256 * 4.048e-1 # arcsec (from original image) aperture_width /= 3600 # arcsec to deg aperture_width /= np.abs(h['CD1_1']) # deg to pix c = SkyCoord('7:41:55.197', '-18:12:35.97', frame=h['RADECSYS'].lower(), unit=(u.hourangle, u.deg)) x, y = np.arange(h['NAXIS1']), np.arange(h['NAXIS2']) X, Y = np.meshgrid(x, y) center_wcs = wcs.WCS(h) center = center_wcs.wcs_world2pix(c.ra.deg, c.dec.deg, 6563, 1) print(center, np.abs(X - center[0]), np.abs(Y - center[1]), aperture_width) X = np.where(np.abs(X - center[0]) < aperture_width / 2, True, False) Y = np.where(np.abs(Y - center[1]) < aperture_width / 2, True, False) snr_mask = X * Y plt.imshow(snr_mask) plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) plt.show() if show: fig1 = plt.figure(figsize=(20, 5)) plt.title('Signal-to-Noise Ratio') gs = GridSpec(1, 3) ax1 = plt.subplot(gs[0]) ax1.set_title('SNR') im1 = ax1.imshow(snr, cmap='cubehelix', interpolation='nearest', origin='lower', vmin=3, vmax=20) div1 = make_axes_locatable(ax1) cax1 = div1.append_axes("right", size="5%", pad=0.05) cbar1 = plt.colorbar(mappable=im1, cax=cax1, use_gridspec=True, orientation='vertical') ax2 = plt.subplot(gs[1]) ax2.set_title('Mask') im2 = ax2.imshow(np.where(snr_mask, 1, 0), cmap='gray', interpolation='nearest', origin='lower', vmin=0, vmax=1) div2 = make_axes_locatable(ax2) cax2 = div2.append_axes("right", size="5%", pad=0.05) cbar2 = plt.colorbar(mappable=im2, cax=cax2, use_gridspec=True, orientation='vertical') cmap = plt.get_cmap('cubehelix') cmap.set_bad('w', 1.0) ax3 = plt.subplot(gs[2]) ax3.set_title('Masked') im3 = ax3.imshow(np.ma.masked_where(~snr_mask, snr), cmap=cmap, interpolation='nearest', origin='lower', vmin=0) div3 = make_axes_locatable(ax3) cax3 = div3.append_axes("right", size="5%", pad=0.05) cbar3 = plt.colorbar(mappable=im3, cax=cax3, use_gridspec=True, orientation='vertical') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) gs.tight_layout(fig1) plt.show() pyfits.writeto(filename.replace('.', '.SNR.'), snr, h, clobber=True) pyfits.writeto(filename.replace('.', '.SNR_LAPLACIAN.'), snr_laplacian, h, clobber=True) # Adjust continuum continuum = self.fit_continuum(d) # Subtract continuum continuum = np.reshape(continuum, (continuum.size, 1, 1)) continuum = np.repeat(continuum, d.shape[1], axis=1) continuum = np.repeat(continuum, d.shape[2], axis=2) d -= continuum del continuum # Integrate along the planetary nebulae d = d * snr_mask d = d.sum(axis=2) d = d.sum(axis=1) d = d / np.float(h['EXPTIME']) gauss_pw, _ = self.fit_gaussian(w, d) gauss_pc, _ = self.fit_gaussian(z, d) log.info("Gaussian parameters ---") log.info("p[0] = %.4f ADU/s" % gauss_pw[0]) log.info("p[1] = %.4f A = %.4f channels" % (gauss_pw[1], gauss_pc[1])) log.info("p[2] = %.4f A = %.4f channels" % (gauss_pw[2], gauss_pc[2])) log.info("p[3] = %.4f ADU/s" % gauss_pw[3]) # total_flux = (gauss_pc[0] - gauss_pc[3]) * np.sqrt(2 * np.pi) \ # * gauss_pc[2] # log.info("Total flux = (a - d) * sqrt(2pi) * c") # log.info(" %.5E ADU/s" % total_flux) fwhm = np.abs(gauss_pw[2] * 2 * np.sqrt(2 * np.log(2))) filter_ = np.where(np.abs(w - gauss_pw[1]) < fwhm, True, False) # d = d - d[~filter_].mean() if show: plt.plot(w, self.gaussian(gauss_pw, w), 'r-', lw=2, label='Gaussian Fit') # plt.plot(w, self.lorentzian(lor_p, w), 'b-', lw=2, label='Lorentzian Fit') plt.plot(w[~filter_], d[~filter_], 'ko') plt.plot(w[filter_], d[filter_], 'ro') plt.title('Spectral profile of the masked area.') plt.xlabel(u'Wavelenght [$\AA$]') plt.ylabel(u'Integrated Count Level [ADU/s]') plt.grid() plt.legend(loc='best') plt.gcf().canvas.mpl_connect('key_press_event', self.on_key_press) plt.show() integrated_flux = (gauss_pc[0] - gauss_pc[3]) \ * (gauss_pc[2] * np.sqrt(2 * np.pi)) log.info("Total flux: %.4E adu/s" % (integrated_flux)) snr_mask = np.where(snr_mask, 1, 0) pyfits.writeto(os.path.join(path, filename.replace('.fits', '.mask.fits')), snr_mask, h, clobber=True) return
plt.plot(np.arange(logdata[:,f2].size)[np.where(lowf_max > 1e10)],logdata[:,f2][np.where(lowf_max > 1e10)], 'r.') plt.savefig(f"{plot_dir}/{name}_logdata_{f2}f", dpi=600) plt.clf() """ u, s, v = np.linalg.svd(logdata, 0) for i in range(1,5): first_modes = np.matmul(u[:,:i], np.matmul(np.diag(s[:i]), v[:i,:])) plt.imshow(logdata - first_modes, aspect='auto',vmin=-0.01, vmax=0.01) plt.colorbar() plt.show() """ corrected = copy.deepcopy(logdata) filtered = median_filter(corrected, size=[wint,winf]) corrected -= filtered #corrected = corrected + np.random.normal(loc=0, scale=noise_scale, size=corrected.shape) plt.imshow(corrected[:,plot_if:plot_ff], aspect='auto', vmin=-0.001, vmax=0.002) plt.colorbar() plt.savefig(f"{plot_dir}/{name}_corrected", dpi=600) plt.clf() MAD = np.median(np.abs(corrected), axis=0) globalMAD = np.median(np.abs(corrected)) axes = plt.gca()
def validate(theta1, theta2, X, act='sig'): aa1 = h(theta1, X, act) aa1 = np.insert(aa1, 0, 1, axis=0) aa2 = h(theta2, aa1.T, 'softmax') accu_matrix = np.argmax(aa2, axis=0) return accu_matrix for imagepath in paths.list_images( '/Users/rjosyula/Pictures/ImageClassification3/Images/Bad/'): ## Extracting the Info from Training Set Results from tmp dir inp_image = ms.imread(imagepath, mode="L") #print(inp_image.shape) ## Rescalling the Inputs #inp_image = inp_image.flatten() inp_image = nd.median_filter(inp_image, 3) #print(inp_image.shape) orig_reso = (964, 1280) #inp_image = resize(inp_image, orig_reso) inp_image = ms.imresize(inp_image.reshape(orig_reso[0], orig_reso[1]), (100, 100), interp='cubic').flatten() inp_image = inp_image / 255.0 #print(inp_image.shape) inp_image = np.insert(inp_image, 0, 1, axis=0) #print(inp_image.shape) ## Orignal Resolution Of Image ## construct the argument parse and parse the arguments ap = argparse.ArgumentParser()
def median_filter(array): return im.median_filter(array, size=(2, 2))
def remove_saturated_pixel(ds, threshold=0.1, minimum=None, maximum=None): """ Remove saturated fixes from an array inplace. :param ds: a dataset as ndarray :param float threshold: what is the upper limit? all pixel > max*(1-threshold) are discareded. :param float minimum: minumum valid value (or True for auto-guess) :param float maximum: maximum valid value :return: the input dataset """ shape = ds.shape if ds.dtype == numpy.uint16: maxt = (1.0 - threshold) * 65535.0 elif ds.dtype == numpy.int16: maxt = (1.0 - threshold) * 32767.0 elif ds.dtype == numpy.uint8: maxt = (1.0 - threshold) * 255.0 elif ds.dtype == numpy.int8: maxt = (1.0 - threshold) * 127.0 else: if maximum is None: maxt = (1.0 - threshold) * ds.max() else: maxt = maximum if maximum is not None: maxt = min(maxt, maximum) invalid = (ds > maxt) if minimum: if minimum is True: # automatic guess of the best minimum TODO: use the HWHM to guess the minumum... data_min = ds.min() x, y = numpy.histogram(numpy.log(ds - data_min + 1.0), bins=100) f = interp1d((y[1:] + y[:-1]) / 2.0, -x, bounds_error=False, fill_value=-x.min()) max_low = fmin(f, y[1], disp=0) max_hi = fmin(f, y[-1], disp=0) if max_hi > max_low: f = interp1d((y[1:] + y[:-1]) / 2.0, x, bounds_error=False) min_center = fminbound(f, max_low, max_hi) else: min_center = max_hi minimum = float(numpy.exp(y[( (min_center / y) > 1).sum() - 1])) - 1.0 + data_min logger.debug("removeSaturatedPixel: best minimum guessed is %s", minimum) ds[ds < minimum] = minimum ds -= minimum # - 1.0 if invalid.sum(dtype=int) == 0: logger.debug("No saturated area where found") return ds gi = ndimage.morphology.binary_dilation(invalid) lgi, nc = ndimage.label(gi) if nc > 100: logger.warning( "More than 100 saturated zones were found on this image !!!!") for zone in range(nc + 1): dzone = (lgi == zone) if dzone.sum(dtype=int) > ds.size // 2: continue min0, min1, max0, max1 = bounding_box(dzone) ksize = min(max0 - min0, max1 - min1) subset = ds[max(0, min0 - 4 * ksize):min(shape[0], max0 + 4 * ksize), max(0, min1 - 4 * ksize):min(shape[1], max1 + 4 * ksize)] while subset.max() > maxt: subset = ndimage.median_filter(subset, ksize) ds[max(0, min0 - 4 * ksize):min(shape[0], max0 + 4 * ksize), max(0, min1 - 4 * ksize):min(shape[1], max1 + 4 * ksize)] = subset return ds
def findOutlierPixels(data, tolerance=1, worry_about_edges=True): """This function finds the hot or dead pixels in a 2D dataset. tolerance is the number of standard deviations used to cutoff the hot pixels If you want to ignore the edges and greatly speed up the code, then set worry_about_edges to False. The function returns a list of hot pixels and also an image with with hot pixels removed""" from scipy.ndimage import median_filter blurred = median_filter(data, size=2) difference = data - blurred threshold = tolerance * np.std(difference) #Find the hot pixels, but ignore the edges hot_pixels = np.nonzero((np.abs(difference[1:-1, 1:-1]) > threshold)) hot_pixels = np.array( hot_pixels) + 1 #because we ignored the first row and first column fixed_image = np.copy(data) #This is the image with the hot pixels removed for y, x in zip(hot_pixels[0], hot_pixels[1]): fixed_image[y, x] = blurred[y, x] if worry_about_edges == True: height, width = np.shape(data) ###Now get the pixels on the edges (but not the corners)### #left and right sides for index in range(1, height - 1): #left side: med = np.median(data[index - 1:index + 2, 0:2]) diff = np.abs(data[index, 0] - med) if diff > threshold: hot_pixels = np.hstack((hot_pixels, [[index], [0]])) fixed_image[index, 0] = med #right side: med = np.median(data[index - 1:index + 2, -2:]) diff = np.abs(data[index, -1] - med) if diff > threshold: hot_pixels = np.hstack((hot_pixels, [[index], [width - 1]])) fixed_image[index, -1] = med #Then the top and bottom for index in range(1, width - 1): #bottom: med = np.median(data[0:2, index - 1:index + 2]) diff = np.abs(data[0, index] - med) if diff > threshold: hot_pixels = np.hstack((hot_pixels, [[0], [index]])) fixed_image[0, index] = med #top: med = np.median(data[-2:, index - 1:index + 2]) diff = np.abs(data[-1, index] - med) if diff > threshold: hot_pixels = np.hstack((hot_pixels, [[height - 1], [index]])) fixed_image[-1, index] = med ###Then the corners### #bottom left med = np.median(data[0:2, 0:2]) diff = np.abs(data[0, 0] - med) if diff > threshold: hot_pixels = np.hstack((hot_pixels, [[0], [0]])) fixed_image[0, 0] = med #bottom right med = np.median(data[0:2, -2:]) diff = np.abs(data[0, -1] - med) if diff > threshold: hot_pixels = np.hstack((hot_pixels, [[0], [width - 1]])) fixed_image[0, -1] = med #top left med = np.median(data[-2:, 0:2]) diff = np.abs(data[-1, 0] - med) if diff > threshold: hot_pixels = np.hstack((hot_pixels, [[height - 1], [0]])) fixed_image[-1, 0] = med #top right med = np.median(data[-2:, -2:]) diff = np.abs(data[-1, -1] - med) if diff > threshold: hot_pixels = np.hstack((hot_pixels, [[height - 1], [width - 1]])) fixed_image[-1, -1] = med return fixed_image
# --- Error height (h) --- # print("detrending and calc std ...") std = np.apply_along_axis(std_residuals, 2, ds.dh) mask_nan = np.isnan(std) print("filtering std field ...") std[std > 1] = 1.0 # Limit max std to 1m (to avoid garbage) std = interpolate_replace_nans(std, Gaussian2DKernel(2), boundary="extend") std = median_filter(std, size=3) std[mask_nan] = np.nan err_std = std / np.sqrt(6) # four missions and two modes (ice/oce) # NOTE: Do not use this! if 0: # Using each mission error (3D) err_ra = np.array([0.23, 0.21, 0.13, 0.11]) / np.sqrt(2) # One per mission err_full = [np.sqrt(err_std ** 2 + e_ra ** 2) for e_ra in err_ra] # Four 2D fields # 2Ds -> 3D err_dh = np.dstack(
from scipy import ndimage dat = imread('MV_HFV_012.jpg') dat = dat[60:] filtdat = ndimage.median_filter(dat, size=(7,7)) hi_dat = np.histogram(dat, bins=np.arange(256)) hi_filtdat = np.histogram(filtdat, bins=np.arange(256)) void = filtdat <= 50 sand = np.logical_and(filtdat>50, filtdat<=114) glass = filtdat > 114 phases = void.astype(np.int) + 2*glass.astype(np.int) +\ 3*sand.astype(np.int) sand = ndimage.binary_opening(sand, iterations=2)
def set_images(self, sample): tmp = sample tmp = tmp.resize(self.img_size, PIL.Image.ANTIALIAS) tmp = np.array(tmp) tmp = ndimage.median_filter(tmp, 5) self.sample = np.array(tmp)
def predict(model, Xi, Yi=None, is_training=False, output_latent=False, inference=None, known_order=None): # Compute the best label for each timesteo if type(Xi) is list: out = [] for i in range(len(Xi)): Yi_ = None if Yi is None else Yi[i] out += [ model.predict(Xi[i], Yi_, is_training, output_latent, inference, known_order) ] return out # Check that Xi is of size FxT if Xi.shape[0] > Xi.shape[0]: Xi = Xi.T if Yi is not None: assert Xi.shape[1] == Yi.shape[ 0], "Error: Xi and Yi are of shapes {} and {}".format( Xi.shape[1], Yi.shape[0]) _, n_timesteps = Xi.shape n_nodes = model.n_nodes # Initialize score score = np.zeros([n_nodes, n_timesteps], np.float64) # loss augmented inference (if training) if is_training: if model.is_latent: score += ssvm.latent_loss_augmented_unaries( score, Yi, model.n_latent) else: score += ssvm.loss_augmented_unaries(score, Yi) # Add potentials to score for key in model.potentials: score = model.potentials[key].compute(model, Xi, score) if model.is_latent and (not is_training and not output_latent): score = ssvm.reduce_latent_states(score, model.n_latent, model.n_classes) # Get predictions inference_type = inference if inference is not None else model.inference_type if inference_type is "framewise": path = score.argmax(0) elif inference_type is "filtered": assert hasattr(model, "filter_len"), "filter_len must be set" path = score.argmax(0) path = nd.median_filter(path, model.filter_len) elif "segmental" in inference_type: normalized = True if "normalized" in inference_type else False if known_order is not None: path = infer_known_ordering(score.T, known_order) else: assert hasattr(model, "max_segs"), "max_segs must be set" # Check if there is a segmental pw.pairwise term seg_term = [ p.name for p in model.potentials.values() if type(p) is pw.segmental_pairwise ] if len(seg_term) >= 1: path = segmental_inference(score.T, model.max_segs, pw=model.ws[seg_term[0]], normalized=normalized) else: path = segmental_inference(score.T, model.max_segs, normalized=normalized) return path
# def rgb2gray(rgbImg): return np.dot(rgbImg[..., :3], [0.299, 0.587, 0.114]).astype(np.uint8) averageMask3x3 = np.array([[1 / 9, 1 / 9, 1 / 9], [1 / 9, 1 / 9, 1 / 9], [1 / 9, 1 / 9, 1 / 9]]) img2 = misc.imread('/Users/jadson/Desktop/DL04_Img2.2.jpg') img3 = misc.imread('/Users/jadson/Desktop/DL04_Img2.1.jpg') img2Gray = rgb2gray(img2) img3Gray = rgb2gray(img3) # apply the media filter medianImg = ndimage.median_filter(img2Gray, 3) # apply the average 3x3 filter averageImg = ndimage.convolve(img2Gray, averageMask3x3) plt.imshow(medianImg, cmap=plt.get_cmap('gray')) plt.suptitle('Reconstruction with Median Filter 3x3') plt.show() plt.imshow(averageImg, cmap=plt.get_cmap('gray')) plt.suptitle('Reconstruction with Average Filter 3x3') plt.show() plt.imshow(img3Gray, cmap=plt.get_cmap('gray')) plt.suptitle('Original Image') plt.show()
def apply_modifier(self): """ Apply a selected filter to a image. Parameters ---------- input_image : nparray Represents the image to be filtered filter_type: str Must in one of: 'gaussian', 'uniform', 'median', 'maximum', 'minimum', 'sharpening', 'percentile', 'wiener', 'sobel' *args: Arguments of the selected filter, see details for more information. **kwargs: The key arguments of the selected filter, see details for more information. Returns ------- nparray The filtered image as the same format of the input. Details ------- Arguments for the filters and the default values: ============= =========================== Filter Kwargs ============= =========================== 'gaussian' sigma: 3 'uniform' size: 3 'median' size: 3 'maximum' size: 3 'minimum' size: 3 'sharpening' alpha: 30, filter_sigma: 1 'percentile' percentile: 75, size: 3 'wiener' noise power and size 'sobel' None ============= =========================== This details also are defined in this module as a argument. """ complete_file_name = self.imagem.edited_image.path # Open the image file input_image = imageio.imread(complete_file_name) # Apply the Modifier if self.filter_type == self.GAUSSIAN: output_image = ndimage.gaussian_filter( input_image, sigma=float(self.filter_argument_value), ) elif self.filter_type == self.UNIFORM: output_image = ndimage.uniform_filter( input_image, size=int(self.size_value), ) elif self.filter_type == self.MEDIAN: output_image = ndimage.median_filter( input_image, size=int(self.size_value), ) elif self.filter_type == self.MAXIMUM: output_image = ndimage.maximum_filter( input_image, size=int(self.size_value), ) elif self.filter_type == self.MINIMUM: output_image = ndimage.minimum_filter( input_image, size=int(self.size_value), ) elif self.filter_type == self.SHARPENING: output_image = self.sharpenning_filter( input_image, alpha=float(self.filter_argument_value), filter_sigma=float(self.size_value) ) elif self.filter_type == self.PERCENTILE: output_image = ndimage.percentile_filter( input_image, percentile=int(self.filter_argument_value), size=int(self.size_value), ) elif self.filter_type == self.WIENER: noise = None if self.filter_argument_value >= 0: noise = float(self.filter_argument_value) output_image = signal.wiener( input_image, mysize=int(self.size_value), # TODO: Should be odd, add clean noise=noise, ) elif self.filter_type == self.SOBEL: output_image = self.sobel_filter( input_image ) # Save the result output_image = output_image.astype(input_image.dtype) imageio.imwrite(complete_file_name, output_image) return output_image
def __init__(self, image=[[0.0]], size=1, **options): from scipy import ndimage self.ax = ndimage.median_filter(image, size, **options)
def removebadpix(data, mask): medianed_image = median_filter(data, size=2) data[np.where(mask > 0)] = medianed_image[np.where(mask > 0)] return data
def threshold_local(image, block_size, method='gaussian', offset=0, mode='reflect', param=None, cval=0): """Compute a threshold mask image based on local pixel neighborhood. Also known as adaptive or dynamic thresholding. The threshold value is the weighted mean for the local neighborhood of a pixel subtracted by a constant. Alternatively the threshold can be determined dynamically by a given function, using the 'generic' method. Parameters ---------- image : (N, M) ndarray Input image. block_size : int Odd size of pixel neighborhood which is used to calculate the threshold value (e.g. 3, 5, 7, ..., 21, ...). method : {'generic', 'gaussian', 'mean', 'median'}, optional Method used to determine adaptive threshold for local neighbourhood in weighted mean image. * 'generic': use custom function (see ``param`` parameter) * 'gaussian': apply gaussian filter (see ``param`` parameter for custom\ sigma value) * 'mean': apply arithmetic mean filter * 'median': apply median rank filter By default the 'gaussian' method is used. offset : float, optional Constant subtracted from weighted mean of neighborhood to calculate the local threshold value. Default offset is 0. mode : {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, optional The mode parameter determines how the array borders are handled, where cval is the value when mode is equal to 'constant'. Default is 'reflect'. param : {int, function}, optional Either specify sigma for 'gaussian' method or function object for 'generic' method. This functions takes the flat array of local neighbourhood as a single argument and returns the calculated threshold for the centre pixel. cval : float, optional Value to fill past edges of input if mode is 'constant'. Returns ------- threshold : (N, M) ndarray Threshold image. All pixels in the input image higher than the corresponding pixel in the threshold image are considered foreground. References ---------- .. [1] https://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#adaptivethreshold Examples -------- >>> from skimage.data import camera >>> image = camera()[:50, :50] >>> binary_image1 = image > threshold_local(image, 15, 'mean') >>> func = lambda arr: arr.mean() >>> binary_image2 = image > threshold_local(image, 15, 'generic', ... param=func) """ if block_size % 2 == 0: raise ValueError("The kwarg ``block_size`` must be odd! Given " "``block_size`` {0} is even.".format(block_size)) check_nD(image, 2) thresh_image = np.zeros(image.shape, 'double') if method == 'generic': ndi.generic_filter(image, param, block_size, output=thresh_image, mode=mode, cval=cval) elif method == 'gaussian': if param is None: # automatically determine sigma which covers > 99% of distribution sigma = (block_size - 1) / 6.0 else: sigma = param ndi.gaussian_filter(image, sigma, output=thresh_image, mode=mode, cval=cval) elif method == 'mean': mask = 1. / block_size * np.ones((block_size, )) # separation of filters to speedup convolution ndi.convolve1d(image, mask, axis=0, output=thresh_image, mode=mode, cval=cval) ndi.convolve1d(thresh_image, mask, axis=1, output=thresh_image, mode=mode, cval=cval) elif method == 'median': ndi.median_filter(image, block_size, output=thresh_image, mode=mode, cval=cval) else: raise ValueError("Invalid method specified. Please use `generic`, " "`gaussian`, `mean`, or `median`.") return thresh_image - offset
def start(self): ### start the outfile name fileroot = os.path.splitext(self.params['filename'])[0] fileroot += "-"+self.timestamp self.params['box'] = apVolume.getModelDimensions(self.params['file']) if self.params['ampfile'] is not None: ### run amplitude correction spifile = apVolume.MRCtoSPI(self.params['file'], self.params['rundir']) tmpfile = apAmpCorrect.createAmpcorBatchFile(spifile, self.params) apAmpCorrect.runAmpcor() ### check if spider was successful fileroot += ".amp" if not os.path.isfile(tmpfile) : apDisplay.printError("amplitude correction failed") ### convert amplitude corrected file back to mrc fileroot += ".amp" a = spider.read(tmpfile) outfile = os.path.join( self.params['rundir'], "ampl-fix.mrc" ) mrc.write(a, outfile) curfile = outfile elif self.params['bfactor'] is True: outfile = os.path.join( self.params['rundir'], "bfactor-fix.mrc" ) outfile = apAmpCorrect.applyBfactor(self.params['file'], fscfile=self.params['fscfile'], apix=self.params['apix'], mass=self.params['mass'], outfile=outfile) curfile = outfile else : ### just run proc3d curfile = self.params['file'] emancmd = "proc3d "+self.params['file']+" " emancmd = "proc3d "+curfile+" " if self.params['median'] is not None: data = mrc.read(curfile) data = ndimage.median_filter(data, size=self.params['median']) mrc.write(data, curfile) emancmd+="apix=%s " %self.params['apix'] if self.params['lp'] is not None: fileroot += (".lp%d" % ( int(self.params['lp']), )) emancmd += "lp=%d " %self.params['lp'] if self.params['hp'] is not None: fileroot += (".hp%d" % ( int(self.params['hp']), )) emancmd += "hp=%d " %self.params['hp'] if self.params['yflip'] is True: fileroot += ".yflip" emancmd +="yflip " if self.params['invert'] is True: fileroot += ".inv" emancmd +="mult=-1 " if self.params['viper'] is True: fileroot += ".vip" emancmd +="icos5fTo2f " if self.params['mask'] is not None: # convert ang to pixels maskpix=int(self.params['mask']/self.params['apix']) fileroot += (".m%d" % ( int(self.params['mask']), )) emancmd += "mask=%d " %maskpix self.params['mask'] = maskpix if self.params['imask'] is not None: # convert ang to pixels maskpix=int(self.params['imask']/self.params['apix']) fileroot += (".im%d" % ( int(self.params['imask']), )) emancmd += "imask=%d " %maskpix self.params['imask'] = maskpix if self.params['norm'] is True: fileroot += ".norm" emancmd += "norm=0,1 " ### add output filename to emancmd string fileroot += ".mrc" self.params['name'] = fileroot outfile = os.path.join(self.params['rundir'], fileroot) emancmd = re.sub(" apix=",(" %s apix=" % outfile), emancmd) apEMAN.executeEmanCmd(emancmd) if self.params['description'] is None: self.params['description'] = "Volume from recon with amplitude adjustment" ### clean up files created during amp correction if self.params['ampfile'] is not None: apFile.removeFile(spifile) apFile.removeFile(tmpfile) if self.params['commit'] is True: symdata = apSymmetry.findSymmetry(self.params['sym']) symmetry = symdata['eman_name'] self.params['reconid'] = self.params['reconiterid'] self.insert3dDensity() ### render chimera images of model apChimera.filterAndChimera(outfile, res=self.params['res'], apix=self.params['apix'], box=self.params['box'], chimtype='snapshot', mass=self.params['mass'], contour=self.params['contour'], zoom=self.params['zoom'], sym=symmetry)
else: difflist.append('NA') # identify frame where the difference in tongue shape is lowest. print(difflist) min_val, min_idx = min((val,idx) for (idx, val) in enumerate(difflist)) print(min_val) print(min_idx) # add frame to array frame_n = imlist[min_idx] print(frame_n) frame_number = os.path.splitext(os.path.splitext(frame_n)[0])[1][1:] # actual frame number, not INDEX print(frame_number) framenos.append(frame_number) openframe = np.array(Image.open(os.path.join(bmpdir,frame_n))) myrframe = ndimage.median_filter(openframe,5) print(myrframe) rpca[tindex,:,:] = myrframe print(syncfile) try: sm = audiolabel.LabelManager(from_file = syncfile, from_type = 'table', sep = '\t', fields_in_head = False, fields = 't1,frameidx') rmidfrm = int(sm.tier('frameidx').label_at(rt_frmtime).text) except ValueError: syncfile = os.path.join(utt, (timestamp + '.bpr.sync.TextGrid')) # print(syncfile) sm = audiolabel.LabelManager(from_file = syncfile, from_type='praat')#,sep = '\t', fields_in_head = True, fields = 'seconds,pulse_idx,raw_data_idx') # print(sm) rmidfrm = int(sm.tier('pulse_idx').label_at(rt_frmtime).text) framename = timestamp + '.' + str(rmidfrm) + '.jpg' if not os.path.isfile(os.path.join(bmpdir,framename)):
def statistical_analysis(outname, image, image_mri, mzs, n, is_ratio, top, post_process, is_norm, is_denoise, is_memmap, translate_components): if is_denoise: size = (5, 5, 1) if len(image.shape) == 4: size += (1, ) image = median_filter(image, size=size) # image[image_mri == 0] = 0 if is_ratio: ratio_images, ratio_mzs = fusion.extract_ratio_images(image, mzs) image = np.concatenate((image, ratio_images), axis=-1) mzs = np.concatenate((mzs, ratio_mzs)) indices_delete = fusion.remove_indices(image) image = np.delete(image, indices_delete, axis=-1) mzs = np.delete(mzs, indices_delete) memmap_dir = os.path.dirname(outname) + os.path.sep + "mmap" + os.path.sep memmap_basename = os.path.splitext(os.path.basename(outname))[0] memmap_image_filename = memmap_dir + memmap_basename + ".npy" memmap_files_exist = (os.path.exists(memmap_dir) and os.path.exists(memmap_image_filename)) if is_memmap and not memmap_files_exist: print("Writing mem-map") os.makedirs(memmap_dir, exist_ok=True) np.save(memmap_image_filename, image) if is_memmap: print("Reading mem-map") image = np.load(memmap_image_filename, mmap_mode="r") image_norm = imzmlio.normalize(image) mri_norm = imzmlio.normalize(image_mri) print(image_norm.shape) if is_norm: image_norm = image_norm.astype(np.float64) mri_norm = mri_norm.astype(np.float64) for index in range(image_norm.shape[-1]): current_image = image_norm[..., index] norm = np.linalg.norm(current_image) if norm > 0: current_image /= norm image_norm[..., index] = current_image mri_norm /= np.linalg.norm(mri_norm) image_norm = fusion.flatten(image_norm, is_spectral=True) mri_norm = fusion.flatten(mri_norm) print("Computing Dimension reduction") nmf = NMF(n_components=n, init='nndsvda', solver='mu', random_state=0, max_iter=1000) # fit_red = nmf.fit(image_norm.T) # eigenvectors = fit_red.components_ #H # image_eigenvectors = nmf.transform(image_norm.T); #W # mri_eigenvectors = nmf.transform(mri_norm.T) # shape_mri = image_mri.shape + (mri_eigenvectors.shape[-1],) # mri_eigenvectors = mri_eigenvectors.reshape(shape_mri) fit_red = nmf.fit(image_norm) W = fit_red.transform(image_norm) H = fit_red.components_ image_eigenvectors = H.T new_shape = image.shape[:-1] + (image_eigenvectors.shape[-1], ) image_eigenvectors = image_eigenvectors.reshape(new_shape) image_eigenvectors_translated = image_eigenvectors H_translated = H if translate_components: image_eigenvectors_translated, translated_image = reg.register_component_images( image_mri, image, image_eigenvectors, 3) H_translated = image_eigenvectors_translated.reshape(H.T.shape).T # We use translated components ONLY for MRI reconstruction fit_red.components_ = H_translated point = fit_red.transform(mri_norm) #Normal components for MS images fit_red.components_ = H X_r = fit_red.transform(image_norm) centers = X_r point_mri = point print("Explained variance ratio=", fusion.get_score(fit_red, image_norm)) if post_process: X_train, X_test = fusion.post_processing(X_r, point) centers = X_train point_mri = X_test clustering = fusion.clustering_kmeans(X_train) plt.plot(X_train[:, 0], X_train[:, 1], "b.") plt.plot(X_test[:, 0], X_test[:, 1], "ro") plt.show() plt.close() if not is_ratio: X_all = np.concatenate((centers, point_mri), axis=0) tsne_all = StandardScaler().fit_transform(X_all) X_r_all = np.concatenate((X_r, point), axis=0) pca_all = StandardScaler().fit_transform(X_r_all) mri = StandardScaler().fit_transform(point) pca_all = pca_all[..., :2] size = (100, 100) if len(image.shape) == 3: images_maldi = [cv2.resize(i, size) for i in image.T] image_mri = cv2.resize(image_mri.T, size) # visualize_scatter_with_images(pca_all, # images_maldi, # image_mri, # figsize=size, # image_zoom=0.7) elif len(image.shape) == 4: images_maldi = [ cv2.resize(i[..., i.shape[-1] // 2], size) for i in np.transpose(image, (3, 0, 1, 2)) ] thumbnail_mri = image_mri.copy() thumbnail_mri = cv2.resize( thumbnail_mri[..., thumbnail_mri.shape[-1] // 2], size) #visualize_scatter_with_images(pca_all, # images_maldi, # thumbnail_mri, # figsize=size, # image_zoom=0.7) # plt.plot(X_r[:, 0], X_r[:, 1], "b.") # plt.plot(point[:, 0], point[:, 1], "ro") # plt.show() # plt.close() labels = None weights = [1 for i in range(centers.shape[1])] if top is not None: af = fusion.clustering_kmeans(image_norm, X_r) labels = af.labels_ centers = af.cluster_centers_ similar_images, similar_mzs, distances = fusion.select_images( image, point_mri, centers, weights, mzs, labels, None) print("Selecting images end") diff = fusion.closest_reconstruction(image, X_r, point, image_eigenvectors, image_eigenvectors_translated) print(mzs.shape) print(similar_mzs[:10], mzs[diff.argsort()][:10]) similar_images = similar_images[..., 0:100] similar_images = imzmlio.normalize(similar_images) index_closest = np.where(mzs == similar_mzs[0]) index = index_closest[0] w = X_r[index, ...] / np.sum(X_r[index, ...]) image_closest = fusion.reconstruct_image_from_components( image_eigenvectors, w.T) image_closest = image_closest.T image_closest = imzmlio.normalize(image_closest) w_mri = point / np.sum(point) mri_reconstructed = fusion.reconstruct_image_from_components( image_eigenvectors_translated, w_mri.T) mri_reconstructed = mri_reconstructed.T i = np.where((mri_reconstructed > 0)) img_tmp = np.reshape(mri_norm, image_mri.shape) mri_reconstructed = imzmlio.normalize(mri_reconstructed) mri_reconstructed_diff = mri_reconstructed.copy().astype(float) if is_norm: mri_reconstructed_diff /= np.linalg.norm(mri_reconstructed_diff) diff_reconstruction = np.mean( np.abs(mri_reconstructed_diff[i] - img_tmp[i])) / np.max(img_tmp) print("Average diff NMF reconstruction (percentage)=", "{:.5f}".format(diff_reconstruction)) if len(similar_images.shape) == 3: fig, ax = plt.subplots(1, 4) ax[0].imshow(similar_images[..., 0], cmap="gray") ax[1].imshow(np.reshape(mri_norm, image_mri.shape), cmap="gray") ax[2].imshow(image_closest, cmap="gray") ax[3].imshow(mri_reconstructed, cmap="gray") # plt.show() elif len(similar_images.shape) == 4: fig, ax = plt.subplots(1, 4) tracker = SliceViewer(ax, np.transpose(similar_images[..., 0], (2, 1, 0)), np.transpose( np.reshape(mri_norm, image_mri.shape), (2, 1, 0)), np.transpose(image_closest, (2, 1, 0)), np.transpose(mri_reconstructed, (2, 1, 0)), vmin=0, vmax=255) fig.canvas.mpl_connect('scroll_event', tracker.onscroll) # plt.show() if len(similar_images.shape) == 4: s = similar_images.shape similar_images = similar_images.reshape(s[0], s[1], s[2], s[3], order="F") tifffile.imwrite(outname, similar_images.T, imagej=True) else: itk_similar_images = sitk.GetImageFromArray(similar_images.T) sitk.WriteImage(itk_similar_images, outname) sitk.WriteImage(sitk.GetImageFromArray(mri_reconstructed.T), os.path.splitext(outname)[0] + "_reconstruction.tif") outname_csv = os.path.splitext(outname)[0] + ".csv" np.savetxt(outname_csv, np.transpose((similar_mzs, distances)), delimiter=";", fmt="%s")
def get_heightmap(color_img, depth_img, cam_intrinsics, cam_pose, workspace_limits, heightmap_resolution, background_heightmap=None, median_filter_pixels=5): if median_filter_pixels > 0: depth_img = ndimage.median_filter(depth_img, size=median_filter_pixels) # Compute heightmap size heightmap_size = np.round(((workspace_limits[1][1] - workspace_limits[1][0])/heightmap_resolution, (workspace_limits[0][1] - workspace_limits[0][0])/heightmap_resolution)).astype(int) depth_heightmap = np.zeros(heightmap_size) # Get 3D point cloud from RGB-D images surface_pts, color_pts = get_pointcloud(color_img, depth_img, cam_intrinsics) # Transform 3D point cloud from camera coordinates to robot coordinates surface_pts = np.transpose(np.dot(cam_pose[0:3,0:3],np.transpose(surface_pts)) + np.tile(cam_pose[0:3,3:],(1,surface_pts.shape[0]))) # Sort surface points by z value sort_z_ind = np.argsort(surface_pts[:,2]) surface_pts = surface_pts[sort_z_ind] color_pts = color_pts[sort_z_ind] # Filter out surface points outside heightmap boundaries heightmap_valid_ind = np.logical_and(np.logical_and(np.logical_and(np.logical_and(surface_pts[:,0] >= workspace_limits[0][0], surface_pts[:,0] < workspace_limits[0][1]), surface_pts[:,1] >= workspace_limits[1][0]), surface_pts[:,1] < workspace_limits[1][1]), surface_pts[:,2] < workspace_limits[2][1]) surface_pts = surface_pts[heightmap_valid_ind] color_pts = color_pts[heightmap_valid_ind] # Create orthographic top-down-view RGB-D depth heightmap heightmap_pix_x = np.floor((surface_pts[:,0] - workspace_limits[0][0])/heightmap_resolution).astype(int) heightmap_pix_y = np.floor((surface_pts[:,1] - workspace_limits[1][0])/heightmap_resolution).astype(int) depth_heightmap[heightmap_pix_y,heightmap_pix_x] = surface_pts[:,2] z_bottom = workspace_limits[2][0] depth_heightmap = depth_heightmap - z_bottom depth_heightmap[depth_heightmap < 0] = 0 if median_filter_pixels > 0: depth_heightmap = ndimage.median_filter(depth_heightmap, size=median_filter_pixels) depth_heightmap[depth_heightmap == -z_bottom] = np.nan # subtract out the scene background heights, if available if background_heightmap is not None: depth_heightmap -= background_heightmap min_z = np.nanmin(depth_heightmap) if min_z < 0: depth_heightmap = np.clip(depth_heightmap, 0, None) if min_z < -0.005: print('WARNING: get_heightmap() depth_heightmap contains negative heights with min ' + str(min_z) + ', ' 'saved depth heightmap png files may be invalid! ' 'See README.md for instructions to collect the depth heightmap again. ' 'Clipping the minimum to 0 for now.') # Create orthographic top-down-view RGB-D color heightmaps color_heightmap_r = np.zeros((heightmap_size[0], heightmap_size[1], 1), dtype=np.uint8) color_heightmap_g = np.zeros((heightmap_size[0], heightmap_size[1], 1), dtype=np.uint8) color_heightmap_b = np.zeros((heightmap_size[0], heightmap_size[1], 1), dtype=np.uint8) color_heightmap_r[heightmap_pix_y,heightmap_pix_x] = color_pts[:,[0]] color_heightmap_g[heightmap_pix_y,heightmap_pix_x] = color_pts[:,[1]] color_heightmap_b[heightmap_pix_y,heightmap_pix_x] = color_pts[:,[2]] if median_filter_pixels > 0: color_heightmap_r = ndimage.median_filter(color_heightmap_r, size=median_filter_pixels) color_heightmap_b = ndimage.median_filter(color_heightmap_b, size=median_filter_pixels) color_heightmap_g = ndimage.median_filter(color_heightmap_g, size=median_filter_pixels) color_heightmap = np.concatenate((color_heightmap_r, color_heightmap_g, color_heightmap_b), axis=2) return color_heightmap, depth_heightmap
def hpss(S, kernel_size=31, power=2.0, mask=False): """Median-filtering harmonic percussive source separation (HPSS). Decomposes an input spectrogram `S = H + P` where `H` contains the harmonic components, and `P` contains the percussive components. This implementation is based upon the algorithm described by [1]_. .. [1] Fitzgerald, Derry. "Harmonic/percussive separation using median filtering." 13th International Conference on Digital Audio Effects (DAFX10), Graz, Austria, 2010. Parameters ---------- S : np.ndarray [shape=(d, n)] input spectrogram. May be real (magnitude) or complex. kernel_size : int or tuple (kernel_harmonic, kernel_percussive) kernel size(s) for the median filters. - If scalar, the same size is used for both harmonic and percussive. - If iterable, the first value specifies the width of the harmonic filter, and the second value specifies the width of the percussive filter. power : float >= 0 [scalar] Exponent for the Wiener filter when constructing mask matrices. Mask matrices are defined by `mask_H = (r_H ** power) / (r_H ** power + r_P ** power)` where `r_H` and `r_P` are the median-filter responses for harmonic and percussive components. mask : bool Return the masking matrices instead of components Returns ------- harmonic : np.ndarray [shape=(d, n)] harmonic component (or mask) percussive : np.ndarray [shape=(d, n)] percussive component (or mask) Examples -------- Separate into harmonic and percussive >>> y, sr = librosa.load(librosa.util.example_audio_file(), duration=15) >>> D = librosa.stft(y) >>> H, P = librosa.decompose.hpss(D) >>> import matplotlib.pyplot as plt >>> plt.figure() >>> plt.subplot(3, 1, 1) >>> librosa.display.specshow(librosa.logamplitude(np.abs(D)**2, ... ref_power=np.max), ... y_axis='log') >>> plt.colorbar(format='%+2.0f dB') >>> plt.title('Full power spectrogram') >>> plt.subplot(3, 1, 2) >>> librosa.display.specshow(librosa.logamplitude(np.abs(H)**2, ... ref_power=np.max), ... y_axis='log') >>> plt.colorbar(format='%+2.0f dB') >>> plt.title('Harmonic power spectrogram') >>> plt.subplot(3, 1, 3) >>> librosa.display.specshow(librosa.logamplitude(np.abs(P)**2, ... ref_power=np.max), ... y_axis='log') >>> plt.colorbar(format='%+2.0f dB') >>> plt.title('Percussive power spectrogram') >>> plt.tight_layout() Or with a narrower horizontal filter >>> H, P = librosa.decompose.hpss(D, kernel_size=(13, 31)) Just get harmonic/percussive masks, not the spectra >>> mask_H, mask_P = librosa.decompose.hpss(D, mask=True) >>> mask_H array([[ 1., 0., ..., 0., 0.], [ 1., 0., ..., 0., 0.], ..., [ 0., 0., ..., 0., 0.], [ 0., 0., ..., 0., 0.]]) >>> mask_P array([[ 0., 1., ..., 1., 1.], [ 0., 1., ..., 1., 1.], ..., [ 1., 1., ..., 1., 1.], [ 1., 1., ..., 1., 1.]]) """ if np.iscomplexobj(S): S, phase = core.magphase(S) else: phase = 1 if np.isscalar(kernel_size): win_harm = kernel_size win_perc = kernel_size else: win_harm = kernel_size[0] win_perc = kernel_size[1] # Compute median filters. Pre-allocation here preserves memory layout. harm = np.empty_like(S) harm[:] = median_filter(S, size=(1, win_harm), mode='reflect') perc = np.empty_like(S) perc[:] = median_filter(S, size=(win_perc, 1), mode='reflect') if mask or power < util.SMALL_FLOAT: mask_harm = (harm > perc).astype(float) mask_perc = 1 - mask_harm if mask: return mask_harm, mask_perc else: perc = perc**power zero_perc = (perc < util.SMALL_FLOAT) perc[zero_perc] = 0.0 harm = harm**power zero_harm = (harm < util.SMALL_FLOAT) harm[zero_harm] = 0.0 # Find points where both are zero, equalize harm[zero_harm & zero_perc] = 0.5 perc[zero_harm & zero_perc] = 0.5 # Compute harmonic mask mask_harm = harm / (harm + perc) mask_perc = perc / (harm + perc) return ((S * mask_harm) * phase, (S * mask_perc) * phase)
def update_dual(self, dual, primal): dual += primal - spimg.median_filter(primal, self.filt_size)
def getClusters(self,plot=False): from sklearn.cluster import DBSCAN from iDBSCAN import iDBSCAN from sklearn import metrics from scipy.spatial import distance from scipy.stats import pearsonr from random import random outname = self.options.plotDir if outname and not os.path.exists(outname): os.system("mkdir -p "+outname) os.system("cp utils/index.php "+outname) # Plot parameters # vmin=1 vmax=5 # IDBSCAN parameters # tip = self.options.tip scale = 1 iterative = self.options.iterative # number of iterations for the IDBSC vector_eps = self.options.vector_eps vector_min_samples = self.options.vector_min_samples vector_eps = list(np.array(vector_eps, dtype=float)*scale) vector_min_samples = list(np.array(vector_min_samples, dtype=float)*scale) cuts = self.options.cuts nb_it = 3 #-----Pre-Processing----------------# rescale=int(self.geometry.npixx/self.rebin) filtimage = median_filter(self.image_fr_zs, size=2) edges = self.ct.arrrebin(filtimage,self.rebin) edcopy = edges.copy() edcopyTight = tl.noisereductor(edcopy,rescale,self.options.min_neighbors_average) # make the clustering with DBSCAN algo # this kills all macrobins with N photons < 1 points = np.array(np.nonzero(np.round(edcopyTight))).astype(int).T lp = points.shape[0] ## apply vignetting (if not applied, vignette map is all ones) ## this is done only for energy calculation, not for clustering (would make it crazy) image_fr_vignetted = self.ct.vignette_corr(self.image_fr,self.vignette) image_fr_zs_vignetted = self.ct.vignette_corr(self.image_fr_zs,self.vignette) if tip=='3D': Xl = [(ix,iy) for ix,iy in points] # Aux variable to simulate the Z-dimension X1 = np.array(Xl).copy() # variable to keep the 2D coordinates for ix,iy in points: # Looping over the non-empty coordinates nreplicas = int(self.image[ix,iy])-1 for count in range(nreplicas): # Looping over the number of 'photons' in that coordinate Xl.append((ix,iy)) # add a coordinate repeatedly X = np.array(Xl) # Convert the list to an array else: X = points.copy() X1 = X if self.options.debug_mode == 0: self.options.flag_plot_noise = 0 # returned collections clusters = [] superclusters = [] # clustering will crash if the vector of pixels is empty (it may happen after the zero-suppression + noise filtering) if len(X)==0: return clusters,superclusters t0 = time.perf_counter() # - - - - - - - - - - - - - - db = iDBSCAN(iterative = iterative, vector_eps = vector_eps, vector_min_samples = vector_min_samples, cuts = cuts, flag_plot_noise = self.options.flag_plot_noise).fit(X) t1 = time.perf_counter() if self.options.debug_mode: print(f"basic clustering in {t1 - t0:0.4f} seconds") if self.options.debug_mode == 1 and self.options.flag_plot_noise == 1: for ext in ['png','pdf']: plt.savefig('{pdir}/{name}_{esp}.{ext}'.format(pdir=outname,name=self.name,esp='0th',ext=ext), bbox_inches='tight', pad_inches=0) plt.gcf().clear() plt.close('all') # Returning to '2' dimensions if tip == '3D': db.labels_ = db.labels_[range(0,lp)] # Returning theses variables to the length db.tag_ = db.tag_[range(0,lp)] # of the 'real' edges, to exclude the fake repetitions. # - - - - - - - - - - - - - - labels = db.labels_ # Number of clusters in labels, ignoring noise if present. n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) ##################### plot # the following is to preserve the square aspect ratio with all the camera pixels # plt.axes().set_aspect('equal','box') # plt.ylim(0,2040) # plt.xlim(0,2040) # Black removed and is used for noise instead. unique_labels = set(labels) colors = [(random(),random(),random(),1.0) for each in range(len(unique_labels))] # colors = [plt.cm.Spectral(each) # for each in np.linspace(0, 1, len(unique_labels))] #canv = ROOT.TCanvas('c1','',600,600) if plot: #fig_edges = plt.figure(figsize=(10, 10)) #plt.imshow(self.image.T, cmap='gray', vmin=0, vmax=1, origin='lower' ) #plt.savefig('{pdir}/{name}_edges.png'.format(pdir=outname,name=self.name)) fig = plt.figure(figsize=(10, 10)) plt.imshow(self.image,cmap='viridis', vmin=1, vmax=10, interpolation=None, origin='lower' ) #plt.savefig('{pdir}/{name}_edges.png'.format(pdir=outname,name=self.name)) for k, col in zip(unique_labels, colors): if k == -1: col = [0, 0, 0, 1] break # noise: the unclustered class_member_mask = (labels == k) #xy = X[class_member_mask & core_samples_mask] xy = X1[class_member_mask] x = xy[:, 0]; y = xy[:, 1] # only add the cores to the clusters saved in the event if k>-1 and len(x)>1: cl = Cluster(xy,self.rebin,image_fr_vignetted,image_fr_zs_vignetted,self.options.geometry,debug=False) cl.iteration = db.tag_[labels == k][0] cl.nclu = k #corr, p_value = pearsonr(x, y) cl.pearson = 999#p_value clusters.append(cl) if plot: xri,yri = tl.getContours(y,x) cline = {1:'r',2:'b',3:'y'} plt.plot(xri,yri,'-{lcol}'.format(lcol=cline[cl.iteration]),linewidth=0.5) # if plot: cl.plotAxes(plot=plt,num_steps=100) # cl.calcProfiles(plot=None) # for dir in ['long','lat']: # prof = cl.getProfile(dir) # if prof and cl.widths[dir]>10: # plot the profiles only of sufficiently long snakes # prof.Draw() # for ext in ['png','pdf']: # canv.SaveAs('{pdir}/{name}_snake{iclu}_{dir}profile.{ext}'.format(pdir=outname,name=self.name,iclu=k,dir=dir,ext=ext)) t2 = time.perf_counter() if self.options.debug_mode: print(f"label basic clusters in {t2 - t1:0.4f} seconds") ## SUPERCLUSTERING from supercluster import SuperClusterAlgorithm superclusterContours = [] scAlgo = SuperClusterAlgorithm(self.options,shape=rescale) u,indices = np.unique(db.labels_,return_index = True) allclusters_it1 = [X1[db.labels_ == i] for i in u[list(np.where(db.tag_[indices] == 1)[0])].tolist()] allclusters_it2 = [X1[db.labels_ == i] for i in u[list(np.where(db.tag_[indices] == 2)[0])].tolist()] allclusters_it12 = allclusters_it1 + allclusters_it2 t3 = time.perf_counter() if self.options.debug_mode: print(f"supercl prep in {t3 - t2:0.4f} seconds") # note: passing the edges, not the filtered ones for deeper information superclusters,superclusterContours = scAlgo.findSuperClusters(allclusters_it12,edges,image_fr_vignetted,image_fr_zs_vignetted,0) t4 = time.perf_counter() if self.options.debug_mode: print(f"supercl in {t4 - t3:0.4f} seconds") if plot: for ext in ['png','pdf']: plt.savefig('{pdir}/{name}.{ext}'.format(pdir=outname,name=self.name,ext=ext), bbox_inches='tight', pad_inches=0) plt.gcf().clear() plt.close('all') ## DEBUG MODE if self.options.debug_mode == 1: print('[DEBUG-MODE ON]') print('[%s Method]' % (self.options.tip)) if self.options.flag_full_image or self.options.flag_rebin_image or self.options.flag_edges_image or self.options.flag_first_it or self.options.flag_second_it or self.options.flag_third_it or self.options.flag_all_it or self.options.flag_supercluster : import matplotlib.pyplot as plt if self.options.flag_full_image == 1: fig = plt.figure(figsize=(self.options.figsizeX, self.options.figsizeY)) #plt.imshow(np.flipud(self.image_fr_zs),cmap=self.options.cmapcolor, vmin=0, vmax=10,origin='upper' ) plt.imshow(np.flipud(self.image_fr_zs),cmap=self.options.cmapcolor, vmin=vmin, vmax=vmax,origin='upper' ) plt.title("Original Image") for ext in ['png','pdf']: plt.savefig('{pdir}/{name}_{esp}.{ext}'.format(pdir=outname,name=self.name,esp='oriIma',ext=ext), bbox_inches='tight', pad_inches=0) with open('{pdir}/{name}_{esp}.pkl'.format(pdir=outname,name=self.name,esp='oriIma',ext=ext), "wb") as fp: pickle.dump(fig, fp, protocol=4) plt.gcf().clear() plt.close('all') if self.options.flag_rebin_image == 1: fig = plt.figure(figsize=(self.options.figsizeX, self.options.figsizeY)) plt.imshow(self.image,cmap=self.options.cmapcolor, vmin=1, vmax=vmax, origin='lower' ) plt.title("Rebin Image") for ext in ['png','pdf']: plt.savefig('{pdir}/{name}_{esp}.{ext}'.format(pdir=outname,name=self.name,esp='rebinIma',ext=ext), bbox_inches='tight', pad_inches=0) plt.gcf().clear() plt.close('all') if self.options.flag_edges_image == 1: fig = plt.figure(figsize=(self.options.figsizeX, self.options.figsizeY)) plt.imshow(edcopyTight, cmap=self.options.cmapcolor, vmin=0, vmax=1, origin='lower' ) plt.title('Edges after Filtering') for ext in ['png','pdf']: plt.savefig('{pdir}/{name}_{esp}.{ext}'.format(pdir=outname,name=self.name,esp='edgesIma',ext=ext), bbox_inches='tight', pad_inches=0) plt.gcf().clear() plt.close('all') if self.options.flag_stats == 1: print('[Statistics]') n_clusters_ = len(set(db.labels_)) - (1 if -1 in db.labels_ else 0) print("Total number of Clusters: %d" % (n_clusters_)) u,indices = np.unique(db.labels_,return_index = True) print("Clusters found in iteration 1: %d" % (sum(db.tag_[indices] == 1))) print("Clusters found in iteration 2: %d" % (sum(db.tag_[indices] == 2))) print("Clusters found in iteration 3: %d" % (sum(db.tag_[indices] == 3))) print("SuperClusters found: %d" % len(superclusters)) if self.options.flag_first_it == 1: print('[Plotting 1st iteration]') u,indices = np.unique(db.labels_,return_index = True) clu = [X1[db.labels_ == i] for i in u[list(np.where(db.tag_[indices] == 1)[0])].tolist()] fig = plt.figure(figsize=(self.options.figsizeX, self.options.figsizeY)) plt.imshow(self.image,cmap=self.options.cmapcolor,vmin=vmin, vmax=vmax,origin='lower' ) plt.title("Clusters found in iteration 1") for j in range(0,np.shape(clu)[0]): ybox = clu[j][:,0] xbox = clu[j][:,1] if (len(ybox) > 0) and (len(xbox) > 0): contours = tl.findedges(ybox,xbox,self.geometry.npixx,self.rebin) for n, contour in enumerate(contours): plt.plot(contour[:, 1],contour[:, 0], '-r',linewidth=2.5) for ext in ['png','pdf']: plt.savefig('{pdir}/{name}_{esp}_{tip}.{ext}'.format(pdir=outname, name=self.name, esp='1st', ext=ext, tip=self.options.tip), bbox_inches='tight', pad_inches=0) with open('{pdir}/{name}_{esp}_{tip}.pkl'.format(pdir=outname,name=self.name,esp='1st',ext=ext,tip=self.options.tip), "wb") as fp: pickle.dump(fig, fp, protocol=4) plt.gcf().clear() plt.close('all') if self.options.flag_second_it == 1: print('[Plotting 2nd iteration]') u,indices = np.unique(db.labels_,return_index = True) clu = [X1[db.labels_ == i] for i in u[list(np.where(db.tag_[indices] == 2)[0])].tolist()] fig = plt.figure(figsize=(self.options.figsizeX, self.options.figsizeY)) plt.imshow(self.image,cmap=self.options.cmapcolor, vmin=vmin, vmax=vmax,origin='lower' ) plt.title("Clusters found in iteration 2") for j in range(0,np.shape(clu)[0]): ybox = clu[j][:,0] xbox = clu[j][:,1] if (len(ybox) > 0) and (len(xbox) > 0): contours = tl.findedges(ybox,xbox,self.geometry.npixx,self.rebin) for n, contour in enumerate(contours): plt.plot(contour[:, 1],contour[:, 0], '-b',linewidth=2.5) for ext in ['png','pdf']: plt.savefig('{pdir}/{name}_{esp}_{tip}.{ext}'.format(pdir=outname, name=self.name, esp='2nd', ext=ext, tip=self.options.tip), bbox_inches='tight', pad_inches=0) with open('{pdir}/{name}_{esp}_{tip}.pkl'.format(pdir=outname,name=self.name,esp='2nd',ext=ext,tip=self.options.tip), "wb") as fp: pickle.dump(fig, fp, protocol=4) plt.gcf().clear() plt.close('all') if self.options.flag_third_it == 1: print('[Plotting 3rd iteration]') u,indices = np.unique(db.labels_,return_index = True) clu = [X1[db.labels_ == i] for i in u[list(np.where(db.tag_[indices] == 3)[0])].tolist()] fig = plt.figure(figsize=(self.options.figsizeX, self.options.figsizeY)) plt.imshow(self.image,cmap=self.options.cmapcolor, vmin=vmin, vmax=vmax,origin='lower' ) plt.title("Clusters found in iteration 3") for j in range(0,np.shape(clu)[0]): ybox = clu[j][:,0] xbox = clu[j][:,1] if (len(ybox) > 0) and (len(xbox) > 0): contours = tl.findedges(ybox,xbox,self.geometry.npixx,self.rebin) for n, contour in enumerate(contours): plt.plot(contour[:, 1],contour[:, 0], '-y',linewidth=2.5) for ext in ['png','pdf']: plt.savefig('{pdir}/{name}_{esp}_{tip}.{ext}'.format(pdir=outname, name=self.name, esp='3rd', ext=ext, tip=self.options.tip), bbox_inches='tight', pad_inches=0) plt.gcf().clear() plt.close('all') if self.options.flag_all_it == 1: print('[Plotting ALL iteration]') u,indices = np.unique(db.labels_,return_index = True) clu = [X1[db.labels_ == i] for i in u[list(np.where(db.tag_[indices] == 1)[0])].tolist()] fig = plt.figure(figsize=(self.options.figsizeX, self.options.figsizeY)) plt.imshow(self.image,cmap=self.options.cmapcolor, vmin=vmin, vmax=vmax,origin='lower' ) plt.title("Final Image") for j in range(0,np.shape(clu)[0]): ybox = clu[j][:,0] xbox = clu[j][:,1] if (len(ybox) > 0) and (len(xbox) > 0): contours = tl.findedges(ybox,xbox,self.geometry.npixx,self.rebin) for n, contour in enumerate(contours): line, = plt.plot(contour[:, 1],contour[:, 0], '-r',linewidth=2.5) if j == 0: line.set_label('1st Iteration') clu = [X1[db.labels_ == i] for i in u[list(np.where(db.tag_[indices] == 2)[0])].tolist()] for j in range(0,np.shape(clu)[0]): ybox = clu[j][:,0] xbox = clu[j][:,1] if (len(ybox) > 0) and (len(xbox) > 0): contours = tl.findedges(ybox,xbox,self.geometry.npixx,self.rebin) for n, contour in enumerate(contours): line, = plt.plot(contour[:, 1],contour[:, 0], '-b',linewidth=2.5) if j == 0: line.set_label('2nd Iteration') clu = [X1[db.labels_ == i] for i in u[list(np.where(db.tag_[indices] == 3)[0])].tolist()] for j in range(0,np.shape(clu)[0]): ybox = clu[j][:,0] xbox = clu[j][:,1] if (len(ybox) > 0) and (len(xbox) > 0): contours = tl.findedges(ybox,xbox,self.geometry.npixx,self.rebin) for n, contour in enumerate(contours): line, = plt.plot(contour[:, 1],contour[:, 0], '-y',linewidth=2.5) if j == 0: line.set_label('3rd Iteration') plt.legend(loc='upper left') if len(superclusters): supercluster_contour = plt.contour(superclusterContours, [0.5], colors='limegreen', linewidths=2) supercluster_contour.collections[0].set_label('supercluster') for ext in ['png','pdf']: plt.savefig('{pdir}/{name}_{esp}_{tip}.{ext}'.format(pdir=outname, name=self.name, esp='all', ext=ext, tip=self.options.tip), bbox_inches='tight', pad_inches=0) with open('{pdir}/{name}_{esp}_{tip}.pkl'.format(pdir=outname,name=self.name,esp='all',ext=ext,tip=self.options.tip), "wb") as fp: pickle.dump(fig, fp, protocol=4) plt.gcf().clear() plt.close('all') #################### PLOT SUPERCLUSTER ONLY ############################### if self.options.flag_supercluster == 1: if len(superclusters): fig = plt.figure(figsize=(self.options.figsizeX, self.options.figsizeY)) supercluster_contour = plt.contour(superclusterContours, [0.5], colors='limegreen', linewidths=2,alpha=0.5) #supercluster_contour.collections[0].set_label('supercluster it 1+2') plt.imshow(self.image,cmap=self.options.cmapcolor,vmin=vmin, vmax=vmax,origin='lower' ) plt.title("Superclusters found") for ext in ['png','pdf']: plt.savefig('{pdir}/{name}_{esp}_{tip}.{ext}'.format(pdir=outname, name=self.name, esp='sc', ext=ext, tip=self.options.tip), bbox_inches='tight', pad_inches=0) with open('{pdir}/{name}_{esp}_{tip}.pkl'.format(pdir=outname,name=self.name,esp='sc',ext=ext,tip=self.options.tip), "wb") as fp: pickle.dump(fig, fp, protocol=4) plt.gcf().clear() plt.close('all') #################### PLOT SUPERCLUSTER ONLY ############################### if self.options.nclu >= 0: print('[Plotting just the cluster %d]' % (self.options.nclu)) fig = plt.figure(figsize=(self.options.figsizeX, self.options.figsizeY)) plt.imshow(self.image,cmap=self.options.cmapcolor, vmin=vmin, vmax=vmax,origin='lower' ) plt.title('Plotting just the cluster %d' % (self.options.nclu)) cl_mask = (db.labels_ == self.options.nclu) xy = X1[cl_mask] xbox = xy[:, 1] ybox = xy[:, 0] if (len(ybox) > 0) and (len(xbox) > 0): contours = tl.findedges(ybox,xbox,self.geometry.npixx,self.rebin) for n, contour in enumerate(contours): line, = plt.plot(contour[:, 1],contour[:, 0], '-r',linewidth=2.5) for ext in ['png','pdf']: plt.savefig('{pdir}/{name}_{tip}_{nclu}.{ext}'.format(pdir=outname, name=self.name, ext=ext, tip = self.options.tip, nclu = self.options.nclu), bbox_inches='tight', pad_inches=0) plt.gcf().clear() plt.close('all') return clusters,superclusters
#fit_up_pos = ffit_up_pos/conductance_quantumf print(area) b1s_upplus.append(B1) b2s_upplus.append(B2) print('Delta B', np.abs(B2-B1)) linear_vector = np.linspace(0, 14, 100) # Up-Sweep Positive conductance_fit = 1/ffit_up_pos/conductance_quantum diff_up_pos = median_filter(np.abs(1/ffit_up_pos(field_up_pos[small_locs])/conductance_quantum - 1/resistance_up_pos[small_locs]/conductance_quantum),4) up_plus.append(diff_up_pos) vec_up_plus.append(field_up_pos[small_locs]) max_up_plus.append(np.max(diff_up_pos)) max_up_plus_field.append(field_up_pos[small_locs][np.argmax(diff_up_pos)]) # Down Sweep Positive resistance_down_pos = resistance[sweep_down_locs_pos_field] field_down_pos = field[sweep_down_locs_pos_field] conductance_down_pos = 1 / resistance_down_pos / conductance_quantum ffit_down_pos, B1, B2, area, small_locs = fit_wo_step(resistance_down_pos, field_down_pos,return_field=True, return_area=True,return_shortened_locs=True) linear_vector = np.linspace(0, 14, 100)
def findEdgesFluor(imagestack, max_ind, min_ind, imagenumber, sigma=10, number_of_slices=4): # Determine the centre and angle of each image from analysis of fluorescence signal ### Threshold, binary, and denoise image. threshold = threshold_otsu(imagestack[int(imagenumber), :, :]) binary = imagestack[int(imagenumber), :, :] > threshold binimg = np.where(binary == True, 1.0, 0.0) if max_ind > (4 * min_ind): # Define low contrast images. denoise = ndimage.median_filter(binimg, size=2) else: denoise = ndimage.median_filter( binimg, size=6) # <<<<===== size=6 for low-constrast edges = feature.canny( denoise, sigma=sigma) # Find edges using skimage canny. # <<<<<<<<======== shape = np.shape(edges) limiter = np.zeros(number_of_slices) section = np.zeros(number_of_slices * int(shape[1])).reshape( number_of_slices, int(shape[1])) locs = np.zeros(2 * number_of_slices).reshape(number_of_slices, 2) loclist = [] ### Centre locator ycentre = shape[1] / 2 centrepeaks = np.nonzero(edges[int(ycentre), :] != 0)[0] # We need to ensure we have only two non-zero locations for edges: if len(centrepeaks) > 2: a = np.arange(len(centrepeaks)) b = centrepeaks kmeans = KMeans(n_clusters=2, n_init=1) kmeans.fit_predict(np.transpose(np.stack((a.T, b.T)))) c = kmeans.cluster_centers_ centrepeaks = np.sort(c[:, 1]) else: centrepeaks = centrepeaks centre = np.average(centrepeaks) ### Angle calculation for i in range(number_of_slices): limiter[i] = int(shape[1] * (i + 1) / (number_of_slices + 1)) section[i] = edges[int(limiter[i]), :] loclist.append(np.nonzero(section[i] != 0)[0]) # We need to ensure we have only two non-zero locations for edges: if len(loclist[i]) > 2: a = np.arange(len(loclist[i])) b = loclist[i] kmeans = KMeans(n_clusters=2, n_init=1) kmeans.fit_predict(np.transpose(np.stack( (a.T, b.T)))) # Reduce edge locations to 2. c = kmeans.cluster_centers_ locs[i] = c[:, 1] elif len(loclist[i]) < 2: break else: locs[i] = loclist[i] angle1 = np.zeros(number_of_slices - 1) angle2 = np.zeros(number_of_slices - 1) for i in range(number_of_slices - 1): dx1 = locs[i + 1, 0] - locs[i, 0] dx2 = locs[i + 1, 1] - locs[i, 1] dy = shape[0] / (number_of_slices + 1) angle1[i] = (np.arctan( dx1 / dy)) / (2 * np.pi) * 360 # Calculate angle from vertical. angle2[i] = (np.arctan( dx2 / dy)) / (2 * np.pi) * 360 # Calculate angle from vertical. angles = np.concatenate((angle1, angle2)) angle_avg = np.average(angles) return (angle_avg, centre)
def brain_masker(in_file, out_file=None, padding=5): """Use grayscale morphological operations to obtain a quick mask of EPI data.""" from pathlib import Path import re import nibabel as nb import numpy as np from scipy import ndimage from skimage.morphology import ball from skimage.filters import threshold_otsu from skimage.segmentation import random_walker # Load data img = nb.load(in_file) data = np.pad(img.get_fdata(dtype="float32"), padding) hdr = img.header.copy() # Cleanup background and invert intensity data[data < np.percentile(data[data > 0], 15)] = 0 data[data > 0] -= data[data > 0].min() datainv = -data.copy() datainv -= datainv.min() datainv /= datainv.max() # Grayscale closing to enhance CSF layer surrounding the brain closed = ndimage.grey_closing(datainv, structure=ball(1)) denoised = ndimage.median_filter(closed, footprint=ball(3)) th = threshold_otsu(denoised) # Rough binary mask closedbin = np.zeros_like(closed) closedbin[closed < th] = 1 closedbin = ndimage.binary_opening(closedbin, ball(3)).astype("uint8") label_im, nb_labels = ndimage.label(closedbin) sizes = ndimage.sum(closedbin, label_im, range(nb_labels + 1)) mask = sizes == sizes.max() closedbin = mask[label_im] closedbin = ndimage.binary_closing(closedbin, ball(5)).astype("uint8") # Prepare markers markers = np.ones_like(closed, dtype="int8") * 2 markers[1:-1, 1:-1, 1:-1] = 0 closedbin_dil = ndimage.binary_dilation(closedbin, ball(5)) markers[closedbin_dil] = 0 closed_eroded = ndimage.binary_erosion(closedbin, structure=ball(5)) markers[closed_eroded] = 1 # Run random walker closed[closed > 0.0] -= closed[closed > 0.0].min() segtarget = (2 * closed / closed.max()) - 1.0 labels = random_walker(segtarget, markers, spacing=img.header.get_zooms()[:3], return_full_prob=True)[..., padding:-padding, padding:-padding, padding:-padding] out_mask = Path(out_file or "brain_mask.nii.gz").absolute() hdr.set_data_dtype("uint8") img.__class__((labels[0, ...] >= 0.5).astype("uint8"), img.affine, hdr).to_filename(out_mask) out_probseg = re.sub(r"\.nii(\.gz)$", r"_probseg.nii\1", str(out_mask).replace("_mask.", ".")) hdr.set_data_dtype("float32") img.__class__((labels[0, ...]), img.affine, hdr).to_filename(out_probseg) out_brain = re.sub(r"\.nii(\.gz)$", r"_brainmasked.nii\1", str(out_mask).replace("_mask.", ".")) data = np.asanyarray(img.dataobj) data[labels[0, ...] < 0.5] = 0 img.__class__(data, img.affine, img.header).to_filename(out_brain) return str(out_brain), str(out_probseg), str(out_mask)
def med_filt_1d(ecg): first_filtered = ndimage.median_filter(ecg, size=7) second_filtered = ndimage.median_filter(first_filtered, 215) ecg_deno = ecg - second_filtered return ecg_deno
def execute(chunk: np.ndarray, size: tuple = (3, 1, 1), mode: str = 'reflect'): chunk = median_filter(chunk, size=size, mode=mode) return [chunk]