def smooth(arr, sigma=5): """A 2d smoothing filter for the heights array""" # arr = arr.astype("float32") fft2 = np.fft.fft2(arr) ndimage.fourier_gaussian(fft2, sigma=sigma, output=fft2) positive = np.fft.ifft2(fft2).real return positive
def f_bandpass(f_data,band): filt_data = np.copy(f_data) # perform the bandpass as 2 lowpass operations filt_data = ndi.fourier_gaussian(filt_data - ndi.fourier_gaussian(filt_data,band[1]),band[0]) #phplot.imageshow(phplot.dB_data(filt_data)) return filt_data
def __compute_contrasts(im, sigma_g1s, sigma_g2s): """ Compute the contrasts for measuring contrast enhancement. This computes c_σ_g₁ from [1] for each pixel across all of the frequencies, which is defined as: c_σ_g₁(x,y) = O(x,y) / S(x,y) for each σ_g1 (eq 22) O(x,y) = C(x,y) - S(x,y) (eq 19) C(x,y) = im ⊗ g₁(x,y) (eq 20) S(x,y) = im ⊗ g₂(x,y) (eq 21) σ_g₁ = 2 log(1/M²)/(1-M²) /𝜈² for 𝜈∈[0,π] (eq 23) 𝜈 = 72π/80, 69π/80, ..., 6π/80, 3π/80 g(x,y) = exp(-1/(2σ²(x²+y²))) - a Gaussian with σ_g1 given above or σ_g₂=M*σ_g₁ where ⊗ is a convolution. All of these are fairly easily expanded to any number of dimensions by using a higher-dimensional Gaussian kernel (actually a 1D Gaussian kernel is applied along each dimension). The g₁ and g₂ kernels are given for all σs along with the image. REFERENCES: 1. Sen D and Pal S K, May 2011, "Automatic Exact Histogram Specification for Contrast Enhancement and Visual System Based Quantitative Evaluation", IEEE Transactions on Image Processing, 20(5):1211-1220. """ from .util import EPS from scipy.ndimage import fourier_gaussian contrasts = empty(im.shape + (len(sigma_g1s), )) rfftn, irfftn, empty_aligned = __get_rfft() im = rfftn(im.astype(float, copy=False)) temp = empty_aligned(im.shape, im.dtype) for i, (sigma_g1, sigma_g2) in enumerate(zip(sigma_g1s, sigma_g2s)): # Using real-space correlations (also need to remove rfftn() above but keep the astype) # The real-space correlations produce lower values #center = im if g_1.size == 1 else ndi.gaussian_filter(im, sigma_g1, truncate=3) #surround = ndi.gaussian_filter(im, sigma_g2, output=temp, truncate=3) center = irfftn( fourier_gaussian(im, sigma_g1, im.shape[-1], output=temp)) surround = irfftn( fourier_gaussian(im, sigma_g2, im.shape[-1], output=temp)) surround[surround == 0] = EPS contrast = contrasts[..., i] subtract(center, surround, contrast) contrast /= surround #abs(contrast, contrast) # norm will automatically take the absolute value for us return contrasts
def image_process(data, mode="gaussian", sigma=2.5, order=0, weight=None, weightMtxSize=7, convMode="constant", cval=0.0, splineOrder=1.0): if mode == "convolve": # if weight is None, create a weight matrix if weight is None: weight = np.ones((weightMtxSize, weightMtxSize)) center = (weightMtxSize - 1) / 2 for i in range(-(center), center + 1, 1): for j in range(-(center), center + 1, 1): weight[i][j] /= math.pow(2.0, max(abs(i), abs(j))) return ndimage.convolve(data, weight, mode=convMode, cval=cval) elif mode == "fourier_gaussian": return ndimage.fourier_gaussian(data, sigma=sigma) elif mode == "spline": return ndimage.spline_filter1d(data, order=splineOrder) else: if mode != "gaussian": print "apply Gaussian filter in image_process()" return ndimage.gaussian_filter(data, sigma=sigma, order=order)
def _smooth(image, variance): """ Gaussian smoothing using the fast-fourier-transform (FFT) Parameters ---------- image: nd-array Input image variance: float Variance of the Gaussian kernel. Returns ------- image: nd-array An image convolved with the Gaussian kernel. See also -------- regisger.Register.smooth """ return np.real( np.fft.ifft2( nd.fourier_gaussian( np.fft.fft2(image), variance ) ) )
def fft_gauss(in_array, kernel_size): ''' Perform FFR Gauss low-pass filtering in_array: input image kernel_size: kernel size for smoothing ''' in_array[np.isinf(in_array)] = 0 in_array[np.isnan(in_array)] = 0 # FFT filter im_fft = np.fft.rfftn(in_array) im_rfft_filtered = ndimage.fourier_gaussian(im_fft, kernel_size, in_array.shape[1]) im_filtered = np.fft.irfftn(im_rfft_filtered) # Power spectrum pwr_spectrum = abs(np.fft.fftshift(im_fft))**2 pwr_spectrum_filtered = abs(np.fft.fftshift(im_rfft_filtered))**2 # Re-normalization sum_ratio = in_array.sum()/im_filtered.sum() im_filtered = im_filtered*sum_ratio return im_filtered, pwr_spectrum, pwr_spectrum_filtered
def findedges(image, image_file, issave): data = image[y0:y1, x0:x1, :] x, y = np.meshgrid(np.arange(data.shape[1]), np.arange(data.shape[0])) gaus = scimg.fourier_gaussian(data[:, :, 0], sigma=0.01) can_x = scimg.prewitt(gaus, axis=0) can_y = scimg.prewitt(gaus, axis=1) can = np.hypot(can_x, can_y) # pulling out object edges fig3, ax3 = plt.subplots(2, 1, figsize=(10, 7)) ax3[0].pcolormesh(x, y, can, cmap='gist_ncar') bin_size = 30 # total bins to show percent_cutoff = 0.05 # cutoff once main peak tapers to 5% of max hist_vec = np.histogram(can.ravel(), bins=bin_size) hist_x, hist_y = hist_vec[0], hist_vec[1] for ii in range(np.argmax(hist_x), bin_size): hist_max = hist_y[ii] if hist_x[ii] < percent_cutoff * np.max(hist_x): break # scatter points where objects exist ax3[1].plot(x[can > hist_max], y[can > hist_max], marker='.', linestyle='', label='Scatter Above 5% Dropoff') ax3[1].set_xlim(np.min(x), np.max(x)) ax3[1].set_ylim(np.min(y), np.max(y)) ax3[1].legend() if issave: plt.savefig(image_file.split('.')[0] + '_edges.png') else: plt.show() print(hist_vec[1]) return stat_dsp(hist_vec[1], threshold)
def elastic_deform_coordinates_2(coordinates, sigmas, magnitudes): ''' magnitude can be a tuple/list :param coordinates: :param sigma: :param magnitude: :return: ''' if not isinstance(magnitudes, (tuple, list)): magnitudes = [magnitudes] * (len(coordinates) - 1) if not isinstance(sigmas, (tuple, list)): sigmas = [sigmas] * (len(coordinates) - 1) n_dim = len(coordinates) offsets = [] for d in range(n_dim): random_values = np.random.random(coordinates.shape[1:]) * 2 - 1 random_values_ = numpy.fft.fftn(random_values) deformation_field = fourier_gaussian(random_values_, sigmas) deformation_field = numpy.fft.ifftn(deformation_field).real offsets.append(deformation_field) mx = np.max(np.abs(offsets[-1])) offsets[-1] = offsets[-1] / (mx / (magnitudes[d] + 1e-8)) offsets = np.array(offsets) indices = offsets + coordinates return indices
def smooth(self, idata, mode, **kwargs): """Smooth data for contour() and contourf() idata is smoothed by convolve, fourier_gaussian, spline or gaussian (default). If contour_mode is 'convolve' and weight is None, the weight matrix is created automatically. Parameters ----------- idata : numpy 2D array Input data mode : string 'convolve','fourier','spline' or 'gaussian' Returns --------- odata : numpy 2D array See also ---------- http://docs.scipy.org/doc/scipy/reference/ndimage.html """ # modify default parameter self._set_defaults(kwargs) if mode == 'convolve': # if weight is None, create a weight matrix if self.convolve_weights is None: weights = np.ones( (self.convolve_weightSize, self.convolve_weightSize)) center = int((self.convolve_weightSize - 1) / 2) for i in range(-(center), center + 1, 1): for j in range(-(center), center + 1, 1): weights[i][j] /= pow(2.0, max(abs(i), abs(j))) self.convolve_weights = weights odata = ndimage.convolve(idata, weights=self.convolve_weights, mode=self.convolve_mode, cval=self.convolve_cval, origin=self.convolve_origin) elif mode == 'fourier': odata = ndimage.fourier_gaussian(idata, sigma=self.fourier_sigma, n=self.fourier_n, axis=self.fourier_axis) elif mode == 'spline': odata = ndimage.spline_filter1d(idata, order=self.spline_order, axis=self.spline_axis) else: if mode != 'gaussian': print('apply Gaussian filter in image_process()') odata = ndimage.gaussian_filter(idata, sigma=self.gaussian_sigma, order=self.gaussian_order, mode=self.gaussian_mode, cval=self.gaussian_cval) return odata
def smooth(self, idata, mode, **kwargs): '''Smooth data for contour() and contourf() idata is smoothed by convolve, fourier_gaussian, spline or gaussian (default). If contour_mode is 'convolve' and weight is None, the weight matrix is created automatically. Parameters ----------- idata : numpy 2D array Input data mode : string 'convolve','fourier','spline' or 'gaussian' Returns --------- odata : numpy 2D array See also ---------- http://docs.scipy.org/doc/scipy/reference/ndimage.html ''' # modify default parameter self._set_defaults(kwargs) if mode == 'convolve': # if weight is None, create a weight matrix if self.convolve_weights is None: weights = np.ones((self.convolve_weightSize, self.convolve_weightSize)) center = (self.convolve_weightSize - 1) / 2 for i in range(- (center), center + 1, 1): for j in range(- (center), center + 1, 1): weights[i][j] /= pow(2.0, max(abs(i), abs(j))) self.convolve_weights = weights odata = ndimage.convolve(idata, weights=self.convolve_weights, mode=self.convolve_mode, cval=self.convolve_cval, origin=self.convolve_origin) elif mode == 'fourier': odata = ndimage.fourier_gaussian(idata, sigma=self.fourier_sigma, n=self.fourier_n, axis=self.fourier_axis) elif mode == 'spline': odata = ndimage.spline_filter1d(idata, order=self.spline_order, axis=self.spline_axis) else: if mode != 'gaussian': print 'apply Gaussian filter in image_process()' odata = ndimage.gaussian_filter(idata, sigma=self.gaussian_sigma, order=self.gaussian_order, mode=self.gaussian_mode, cval=self.gaussian_cval) return odata
def transform_image(dimensions, image): sigma = dimensions[0] image = image.copy() / 255. for channel in range(image.shape[2]): image[:, :, channel] = fftpack.ifft2( fourier_gaussian(fftpack.fft2(image[:, :, channel]), sigma)).real return np.clip(image * 255., 0., 255.)
def filterfft(vorti, mask, sigma=20): vort = vorti.copy() vort[np.isnan(vort)] = np.nanmean(vort) input_ = np.fft.fft2(vort) result = ndimage.fourier_gaussian(input_, sigma=sigma) result = np.real(np.fft.ifft2(result)) result[mask] = np.nan return result
def test_fourier_gaussian_complex01(self, shape, dtype, dec): a = numpy.zeros(shape, dtype) a[0, 0] = 1.0 a = fft.fft(a, shape[0], 0) a = fft.fft(a, shape[1], 1) a = ndimage.fourier_gaussian(a, [5.0, 2.5], -1, 0) a = fft.ifft(a, shape[1], 1) a = fft.ifft(a, shape[0], 0) assert_almost_equal(ndimage.sum(a.real), 1.0, decimal=dec)
def gaussian_blur_box(self, prot, resolution, box_size_x, box_size_y, box_size_z, sigma_coeff=0.356, normalise=True,filename="None"): """ Returns a Map instance based on a Gaussian blurring of a protein. The convolution of atomic structures is done in reciprocal space. Arguments: *prot* the Structure instance to be blurred. *resolution* the resolution, in Angstroms, to blur the protein to. *box_size_x* x dimension of map box in Angstroms. *box_size_y* y dimension of map box in Angstroms. *box_size_z* z dimension of map box in Angstroms. *sigma_coeff* the sigma value (multiplied by the resolution) that controls the width of the Gaussian. Default values is 0.356. Other values used : 0.187R corresponding with the Gaussian width of the Fourier transform falling to half the maximum at 1/resolution, as used in Situs (Wriggers et al, 1999); 0.225R which makes the Fourier transform of the distribution fall to 1/e of its maximum value at wavenumber 1/resolution, the default in Chimera (Petterson et al, 2004) 0.356R corresponding to the Gaussian width at 1/e maximum height equaling the resolution, an option in Chimera (Petterson et al, 2004); 0.425R the fullwidth half maximum being equal to the resolution, as used by FlexEM (Topf et al, 2008); 0.5R the distance between the two inflection points being the same length as the resolution, an option in Chimera (Petterson et al, 2004); 1R where the sigma value simply equal to the resolution, as used by NMFF (Tama et al, 2004). *filename* output name of the map file. """ densMap = self.protMapBox(prot, 1, resolution, box_size_x, box_size_y, box_size_z, filename) x_s = int(densMap.x_size()*densMap.apix) y_s = int(densMap.y_size()*densMap.apix) z_s = int(densMap.z_size()*densMap.apix) ##newMap = densMap.resample_by_box_size([z_s, y_s, x_s]) ##newMap.fullMap *= 0 newMap = densMap.copy() newMap.fullMap = zeros((z_s, y_s, x_s)) newMap.apix = (densMap.apix*densMap.x_size())/x_s sigma = sigma_coeff*resolution newMap = self.make_atom_overlay_map(newMap, prot) fou_map = fourier_gaussian(fftn(newMap.fullMap), sigma) newMap.fullMap = real(ifftn(fou_map)) newMap = newMap.resample_by_box_size(densMap.box_size()) if normalise: newMap = newMap.normalise() return newMap
def get_cor_mask( cor_image, cor_thresh, smooth=True, winfrac=10, return_smoothed=False, strel_size=3, morph_func="closing", ): """Get a mask of the correlation values in `cor_image` Args: cor_image (np.ndarray): 2D array of correlation values cor_thresh (float): threshold for correlation values smooth (bool, optional): whether to remove a gaussian filtered version before masking. Removes any long-wavelength trend in correlation. Defaults to True. Returns: np.ndarray: 2D boolean array of same shape as `image` """ import scipy.ndimage as ndi from skimage import morphology if np.isscalar(cor_thresh): cor_thresh = [cor_thresh] cor_image = cor_image.copy() cormask = np.logical_or(np.isnan(cor_image), cor_image == 0) if smooth: sigma = min(cor_image.shape) / winfrac cor_smooth = np.fft.ifft2( ndi.fourier_gaussian( # cor_smooth = ndi.filters.gaussian_filter( np.fft.fft2(cor_image), # cor_image, sigma=sigma, ) ).real cor_image -= cor_smooth cor_image[cormask] = 0 cor_image -= np.min(cor_image) cor_image[cormask] = 0 masks = [] for c in cor_thresh: mask = cor_image < c selem = morphology.disk(strel_size) # Closing makes the mask bigger, opening smaller func = getattr(morphology, morph_func) mask = func(mask, selem) masks.append(mask) mask = np.stack(masks) if len(cor_thresh) > 1 else masks[0] return (mask, cor_image) if return_smoothed else mask
def ConvolveWithPSF_2D(image_frame, gauss_kernel_rad): """ convolves a 2d image with a gaussian kernel by multipliying in fourierspace """ PSF_Type = "Gauss" if PSF_Type == "Gauss": image_frame_filtered = np.real(np.fft.ifft2(ndimage.fourier_gaussian(np.fft.fft2(image_frame), sigma=[gauss_kernel_rad ,gauss_kernel_rad]))) return image_frame_filtered
def my_gaussian_filter_2(img, sigma, mode='highpass'): img = np.asarray(img.convert('L')) img_fft = np.fft.fft2(img) G = fourier_gaussian(img_fft, sigma) if mode == 'highpass': img_g = rescale(np.real(np.fft.ifft2(img_fft - G)), 0, 1) elif mode == 'lowpass': img_g = rescale(np.real(np.fft.ifft2(G)), 0, 1) else: print('no such mode!') return None img_g = np.stack((img_g, ) * 3, axis=-1) return Image.fromarray(np.uint8(img_g * 255))
def updatefig(*args): z = np.random.randint(low=0, high=2**Nbits - 1, size=zz.shape, dtype='uint') input_ = np.fft.fft2(z) result = ndimage.fourier_gaussian(input_, sigma=1) z = np.fft.ifft2(result) z = np.abs(z) im.set_array(z) return im,
def gaussian_filter(self, width): """Return the filtered velocity fields with Gaussian filter. Use a series of 1D Gaussian filtering filters for the velocity fields. The Gaussian kernel is defined as: - :math:`G(x-y) = \\sqrt{\\frac{\\gamma}{\\pi \\bar{\\Delta}^2}} \exp{\\left( \\frac{-\\gamma |x-y|^2}{\\bar{\\Delta}^2}\\right)}` where :math:`\\gamma=6`, :math:`\\bar{\\Delta} = \\omega \\Delta x`, and :math:`\\omega` is the filter width. Since we are using the SciPy filtering function, we have to adjust the variance parameter to ensure that we are getting the result we want. This filter is applied in Fourier and spatial domains. :param width: width of filter in :math:`\\Delta x` units, :math:`\\omega` :type width: double :return: filtered velocity :rtype: Velocity """ gamma = 6.0 sigma = width / np.sqrt(2 * gamma) Ufh = [ spn.fourier_gaussian(self.Uf[0], sigma, n=self.N[0]), spn.fourier_gaussian(self.Uf[1], sigma, n=self.N[0]), spn.fourier_gaussian(self.Uf[2], sigma, n=self.N[0]), ] # Same as spn.filters.gaussian_filter(U,sigma,mode='wrap',truncate=6) Uh = [ np.fft.irfftn(Ufh[0]), np.fft.irfftn(Ufh[1]), np.fft.irfftn(Ufh[2]) ] return Velocity(self.L, Uh, Uf=Ufh)
def _smooth(image, variance): """ A simple image smoothing method - using a Gaussian kernel. @param image: the input image, a numpy ndarray object. @param variance: the width of the smoothing kernel. @return: the smoothing input image. """ return np.real( np.fft.ifft2( nd.fourier_gaussian( np.fft.fft2(image), variance ) ) )
def apply_fft_highpass_flt(images, sigma=50): if images.ndim == 2: images = np.expand_dims(images, axis=0) squeeze_image = True else: squeeze_image = False images = images.astype('float') # print("Applying sharpen....") for frame_idx, image in enumerate(images): fft_image = np.fft.fft2(image) fft_image = ndimage.fourier_gaussian(fft_image, sigma) fft_image = np.fft.ifft2(fft_image) images[frame_idx, :, :] = image - fft_image.real if squeeze_image: images = np.squeeze(images) return images
def apply_log_fft_highpass_flt(images, sigma=50): if images.ndim == 2: images = np.expand_dims(images, axis=0) squeeze_image = True else: squeeze_image = False images = images.astype('float') # print("Applying sharpen....") for frame_idx, image in enumerate(images): log_image = np.log1p((image - image.min()) / (image.max() - image.min())) fft_image = np.fft.fft2(log_image) fft_image = ndimage.fourier_gaussian(fft_image, sigma) fft_image = np.fft.ifft2(fft_image) log_image = log_image - fft_image.real images[frame_idx, :, :] = (np.expm1(log_image) * (image.max() - image.min())) + image.min() if squeeze_image: images = np.squeeze(images) return images
def diffract(self, rot, cutout=True): """Calculate diffraction pattern from rotation matrix. 2D FFT to get diffraction pattern from intensity matrix. Parameters ---------- rot: numpy.ndarray (3, 3), Rotation matrix cutout: bool, default True Return diffraction pattern with circle cutout Returns ------- numpy.ndarray (N,N), Diffraction pattern """ N = self.N / self.zoom inv_shear = self.calc_proj(rot) xy = np.copy(np.dot(self.orig, rot)[:, 0:2]) xy = np.dot(xy, inv_shear.T) xy = self.pbc_2d(xy, N) im = self.bin(xy, N) dp = np.fft.fft2(im) dp = fourier_gaussian(dp, self.peak_width / self.zoom) dp = np.fft.fftshift(dp) dp = np.absolute(dp) dp *= dp dp = self.scale(dp) dp = self.shear_back(dp, inv_shear) dp /= dp.max() dp[dp < self.bot] = self.bot dp[dp > self.top] = self.top dp = np.log10(dp) if not cutout: self.dp = dp return dp idbig = self.circle_cutout(dp) dp[np.unravel_index(idbig, (self.N, self.N))] = np.log10(self.bot) self.dp = dp return dp
def remove_lowpass( z, lowpass_sigma_pct=0.25, mask=np.ma.nomask, copy=True, dtype=np.float32, dem=None ): """Subtracts the output of a low-pass filter (aka: performs high pass filter) Used to remove noise artifacts from unwrapped interferograms Args: z (ndarray): 2D array, interpreted as heights deramp_order (int): degree of surface estimation deramp_order = 1 removes linear ramp, deramp_order = 2 fits quadratic surface Returns: ndarray: flattened 2D array with estimated surface removed """ import scipy.ndimage as ndi from scipy.fft import fft2, ifft2 from scipy.interpolate import NearestNDInterpolator if z.ndim > 2: return np.stack( [remove_lowpass(layer, lowpass_sigma_pct, mask, copy, dtype) for layer in z] ) z_masked = z.copy() if copy else z # For FFT filtering, need 0s instead of nans (or all become nans) z_masked[..., mask] = 0 # z_masked[..., mask] = np.nan # nomask = np.where(~mask) # interp = NearestNDInterpolator(np.transpose(nomask), z_masked[nomask]) # z_masked = interp(*np.indices(z_masked.shape)) # Create the sigma as a percentage of the image size sigma = lowpass_sigma_pct * min(z.shape[-2:]) input_ = fft2(z_masked, workers=-1) result = ndi.fourier_gaussian(input_, sigma=sigma) z_fit = ifft2(result, workers=-1).real z_fit[..., mask] = np.nan # Then use the non-masked as return value return (z - z_fit).astype(dtype)
def convolveTEM(img, Nz, sigma1=7, sigma2=7, binaryThresh=0.0146): # doesn't affect AF or VF but ensures the particles are connected zPos = zDisplacements(img.shape[0], img.shape[1], Nz, sigma1) microStruct = np.zeros((img.shape[0], img.shape[1], Nz)) for x in range(img.shape[0]): for y in range(img.shape[1]): microStruct[x, y, zPos[x, y]] = img[x, y] # Convolve the constructed microstructure with a Gaussian to give them a shape convolveMS = np.fft.ifftn( fourier_gaussian(np.fft.fftn(microStruct), sigma=sigma2)).real binaryMS = np.where(convolveMS < binaryThresh, 0, 1) # binarize # Change the orientation such the z-dir is along longer direction (Change XYZ to ZXY) # TODO use np.stack binaryMSrot = np.zeros((Nz, img.shape[0], img.shape[1])) for z in range(Nz): binaryMSrot[z, ...] = binaryMS[..., z] return binaryMSrot
def image_process(data, mode="gaussian", sigma=2.5, order=0, weight=None, weightMtxSize=7, convMode="constant", cval=0.0, splineOrder=1.0): if mode=="convolve": # if weight is None, create a weight matrix if weight is None: weight = np.ones((weightMtxSize, weightMtxSize)) center = (weightMtxSize - 1) / 2 for i in range(-(center), center+1, 1): for j in range(-(center), center+1, 1): weight[i][j] /= math.pow(2.0, max(abs(i),abs(j))) return ndimage.convolve(data, weight, mode=convMode, cval=cval) elif mode=="fourier_gaussian": return ndimage.fourier_gaussian(data, sigma=sigma) elif mode=="spline": return ndimage.spline_filter1d(data, order=splineOrder) else: if mode!="gaussian": print "apply Gaussian filter in image_process()" return ndimage.gaussian_filter(data, sigma=sigma, order=order)
def _smooth(image, variance): """ Gaussian smoothing using the fast-fourier-transform (FFT) Parameters ---------- image: nd-array Input image variance: float Variance of the Gaussian kernel. Returns ------- image: nd-array An image convolved with the Gaussian kernel. See also -------- regisger.Register.smooth """ return np.real( np.fft.ifft2(nd.fourier_gaussian(np.fft.fft2(image), variance)))
def calc_shift_direct(ft_1, ft_2, origin=0, debug_cross_cor=None): """ Does the actual fft cross correlation. Clean up - including cropping, thresholding, mask dilation. Performs n dimension gaussian fit and returns center. """ ft_1 = ndimage.fourier_gaussian(ft_1, 0.5) ft_2 = ndimage.fourier_gaussian(ft_2, 0.5) # module level for multiprocessing tmp = ft_1 * np.conj(ft_2) del ft_1, ft_2 cross_corr = np.abs(np.fft.ifftshift(np.fft.irfftn(tmp))) flat_dims = np.where(np.asarray(cross_corr.shape) == 1)[0] if len(flat_dims) > 0: cross_corr = cross_corr.squeeze() if cross_corr.sum() == 0: return origin * np.nan # threshold = np.ptp(cross_corr) * 0.5 + np.min(cross_corr) # cheat and striaght up crop out 3/4 of the image if it's large # i.e. drift not allow to span 1/4 the image width cropping = [ slice(dim * 6 // 16, -dim * 6 // 16) if dim >= 16 else slice(None, None) for dim in cross_corr.shape ] cross_corr_mask = np.zeros(cross_corr.shape) cross_corr_mask[tuple(cropping)] = True # threshold = np.percentile(cross_corr[cropping], 95) threshold = np.ptp( cross_corr[cross_corr_mask.astype(bool)]) * 0.75 + np.min( cross_corr[cross_corr_mask.astype(bool)]) cross_corr_mask *= cross_corr > threshold # difficult to adjust for complete despeckling. slow? # cross_corr_mask = ndimage.binary_erosion(cross_corr_mask, structure=np.ones((1,)*cross_corr_mask.ndim), iterations=1, border_value=1, ) # cross_corr_mask = ndimage.binary_dilation(cross_corr_mask, structure=np.ones((3,)*cross_corr_mask.ndim), iterations=1, border_value=0, ) # print("mask {}".format(cross_corr_mask.sum())) labeled_image, labeled_counts = ndimage.label(cross_corr_mask) if labeled_counts > 1: max_index = np.argmax( ndimage.mean(cross_corr_mask, labeled_image, range(1, labeled_counts + 1))) + 1 cross_corr_mask = labeled_image == max_index cross_corr_mask = ndimage.binary_dilation( cross_corr_mask, structure=np.ones((5, ) * cross_corr_mask.ndim), iterations=1, border_value=0, ) cross_corr_thresholded = cross_corr * cross_corr_mask # if not debug_cross_cor is None: i, (path, dtype, shape) = debug_cross_cor cc_images = np.memmap(path, mode="r+", dtype=dtype, shape=shape) cross_corr_thresholded_repadded = cross_corr_thresholded.view() for d in flat_dims: cross_corr_thresholded_repadded = np.expand_dims( cross_corr_thresholded_repadded, d) short_axis = np.argmin(cross_corr_thresholded_repadded.shape) cc_images[i] = cross_corr_thresholded_repadded.mean(axis=short_axis) del cc_images dims = range(len(cross_corr.shape)) # crop out masked area bounds = np.zeros((len(dims), 2), dtype=np.int) for d in dims: dims_tmp = list(dims) dims_tmp.remove(d) mask_1d = np.any(cross_corr_mask, axis=tuple(dims_tmp)) bounds[d] = np.where(mask_1d)[0][[0, -1]] + [0, 1] cross_corr_thresholded = cross_corr_thresholded.take( np.arange(*bounds[d]), axis=d) # offset = np.zeros(len(dims)) # for d, length in enumerate(cross_corr_thresholded.shape): # dims_tmp = list(dims) # dims_tmp.remove(d) # offset[d] = np.sum(np.arange(length) * cross_corr_thresholded.sum(axis=tuple(dims_tmp))) # offset /= cross_corr_thresholded.sum() # offset += bounds[:, 0] cross_corr_thresholded[cross_corr_thresholded == 0] = np.nan # cross_corr_thresholded -= np.nanmin(cross_corr_thresholded) cross_corr_thresholded /= np.nanmax(cross_corr_thresholded) # ### Gaussian fit ## p0 = [np.nanmax(cross_corr_thresholded), np.nanmin(cross_corr_thresholded)] # p0 = [1, 0] # grids = list() # for i, d in enumerate(cross_corr_thresholded.shape): # grids.append(np.arange(d)) # p0.extend([(d-1)*0.5, 0.5*d]) ## print grids ## print p0 # res = optimize.least_squares(guassian_nd_error, p0, args=(grids, cross_corr_thresholded)) ## print res.x ### Rbf peak finding p0 = [] grids = list() for i, d in enumerate(cross_corr_thresholded.shape): grids.append(np.arange(d)) p0.append(0.5 * d) rbf_interpolator = build_rbf(grids, cross_corr_thresholded) res = optimize.minimize(rbf_nd_error, p0, args=rbf_interpolator) offset = list() for i in range(len(cross_corr_thresholded.shape)): # offset.append(res.x[2*i+2]) offset.append(res.x[i]) offset += bounds[:, 0] # print offset if len(flat_dims) > 0: offset = np.insert(offset, flat_dims, 0) return offset - origin
def test_density_plane(return_results=False): """ Tests cutting density planes from snapshots against lenstools """ klin = np.loadtxt(data_path + '/flowpm/data/Planck15_a1p00.txt').T[0] plin = np.loadtxt(data_path + '/flowpm/data/Planck15_a1p00.txt').T[1] ipklin = iuspline(klin, plin) cosmo = flowpm.cosmology.Planck15() a0 = 0.9 r0 = flowpm.background.rad_comoving_distance(cosmo, a0) # Create a state vector initial_conditions = flowpm.linear_field(nc, bs, ipklin, batch_size=2) state = flowpm.lpt_init(cosmo, initial_conditions, a0) # Export the snapshot flowpm.io.save_state(cosmo, state, a0, [nc, nc, nc], [bs, bs, bs], 'snapshot_density_testing', attrs={'comoving_distance': r0}) # Reload the snapshot with lenstools snapshot = FlowPMSnapshot.open('snapshot_density_testing') # Cut a lensplane in the middle of the volume lt_plane, resolution, NumPart = snapshot.cutPlaneGaussianGrid( normal=2, plane_resolution=plane_resolution, center=(bs / 2) * snapshot.Mpc_over_h, thickness=(bs / 4) * snapshot.Mpc_over_h, left_corner=np.zeros(3) * snapshot.Mpc_over_h, smooth=None, kind='density') # Cut the same lensplane with flowpm fpm_plane = flowpm.raytracing.density_plane( state, nc, center=nc / 2, width=nc / 4, plane_resolution=plane_resolution) # Apply additional normalization terms to match lenstools definitions constant_factor = 3 / 2 * cosmo.Omega_m * (constants.H0 / constants.c)**2 density_normalization = bs / 4 * r0 / a0 fpm_plane = fpm_plane * density_normalization * constant_factor # Checking first the mean value, which accounts for any normalization # issues assert_allclose(np.mean(fpm_plane[0]), np.mean(lt_plane), rtol=1e-5) # To check pixelwise difference, we need to do some smoothing as lenstools and # flowpm use different painting kernels smooth_lt_plane = np.fft.ifft2(fourier_gaussian(np.fft.fft2(lt_plane), 3)).real smooth_fpm_plane = np.fft.ifft2( fourier_gaussian(np.fft.fft2(fpm_plane[0]), 3)).real assert_allclose(smooth_fpm_plane, smooth_lt_plane, rtol=2e-2) if return_results: return fpm_plane, lt_plane, smooth_fpm_plane, smooth_lt_plane
from pandas import read_csv def blur(arr): a1 = roll(arr, 1, axis=0) a2 = roll(arr, -1, axis=0) a3 = roll(arr, 1, axis=1) a4 = roll(arr, -1, axis=1) return 0.2 * (arr + a1 + a2 + a3 + a4) def apply_kernel(d, k): return real(fft.ifft2(k * fft.fft2(d))) if __name__ == '__main__': import sys shape = (4096, 4096) lpkernel = fourier_gaussian(ones(shape), 12) hpkernel = 1.-lpkernel for fn in sys.argv[1:]: data = array(read_csv(fn, sep = ' ', header = None, usecols = (2, ))) data = data.reshape(shape) data = apply_kernel(data, hpkernel) data[:] **= 2 data = apply_kernel(data, lpkernel) #print('max=%f' % data.max()) data[:] /= 15.7 data = data >= 0.5 solid_fr = data.sum() / prod(shape) time = int(fn.split('_')[1].split('.')[0]) print('%f\t%f' % (time, solid_fr))
import scipy.ndimage as ndi import scipy.misc as misc import matplotlib.pyplot as plt import numpy as np img = misc.ascent() noisy = img + 0.09 * img.std() * np.random.random(img.shape) fe = ndi.fourier_ellipsoid(img, 1) fg = ndi.fourier_gaussian(img, 1) fs = ndi.fourier_shift(img, 1) fu = ndi.fourier_uniform(img, 1) title = ['original', 'Noisy', 'Fourier Ellipsoid', 'Fourier Gaussian', 'Fourier Shift', 'Fourier uniform'] output = [img, noisy, fe, fg, fs, fu] for i in range(6): plt.subplot(2, 3, i + 1) plt.imshow(np.float64(output[i]), cmap='gray') plt.title(title[i]) plt.axis('off') plt.show()
from scipy import ndimage, misc import numpy.fft import matplotlib.pyplot as plt fig, (ax1, ax2) = plt.subplots(1, 2) plt.gray() # show the filtered result in grayscale ascent = misc.ascent() input_ = numpy.fft.fft2(ascent) result = ndimage.fourier_gaussian(input_, sigma=4) result = numpy.fft.ifft2(result) ax1.imshow(ascent) ax2.imshow(result.real) # the imaginary part is an artifact plt.show()
def smooth_image(img_arr, sigma=3): freq = fp.fft2(img_arr) freq_gaussian = ndimage.fourier_gaussian(freq, sigma=sigma) im = fp.ifft2(freq_gaussian) return im.real, im.imag
x, y = np.meshgrid(np.arange(cam_res[0]), np.arange(cam_res[1])) # different edge detection methods cam.capture(data, 'rgb') # capture image # diff of gaussians t0 = time.time() grad_xy = scimg.gaussian_gradient_magnitude(data[:, :, 0], sigma=1.5) ##grad_xy = np.mean(grad_xy,2) t_grad_xy = time.time() - t0 # laplacian of gaussian t0 = time.time() lap = scimg.gaussian_laplace(data[:, :, 0], sigma=0.7) t_lap = time.time() - t0 # Canny method without angle t0 = time.time() gaus = scimg.fourier_gaussian(data[:, :, 0], sigma=0.05) can_x = scimg.prewitt(gaus, axis=0) can_y = scimg.prewitt(gaus, axis=1) can = np.hypot(can_x, can_y) ##can = np.mean(can,2) t_can = time.time() - t0 # Sobel method t0 = time.time() sob_x = scimg.sobel(data[:, :, 0], axis=0) sob_y = scimg.sobel(data[:, :, 0], axis=1) sob = np.hypot(sob_x, sob_y) ##sob = np.mean(sob,2) t_sob = time.time() - t0 # plotting routines and labeling fig, ax = plt.subplots(2, 2, figsize=(12, 6))
def apply_fourier(img_path, val): img = Image.open(img_path) return fourier_gaussian(img, sigma=val)
for j in range(0, mx + 2): sist1.input['grupos'] = j sist1.compute() rt[j] = sist1.output['salida'] rt[0] = 0 rt[1] = 0 rt[2] = 0 rt[mx + 1] = 255 rt[mx] = 255 Im2 = np.interp(Ima.flatten(), range(257), rt) Im2 = np.reshape(Im2, [fil, col]) plt.subplot(122) plt.axis('off') plt.imshow(Im2, cmap=plt.cm.gray, clim=(0, 255)) plt.title('Metodo Propuesto') plt.show() plt.imshow(Im2, cmap='gray') plt.axis('off') plt.show() input_ = np.fft.fft2(Im2) result = ni.fourier_gaussian(input_, sigma=1) result = np.fft.ifft2(result) plt.imshow(result.real, cmap='gray') # the imaginary part is an artifact plt.show()
def _fourierGaussian_fired(self): #fourier gaussian transform BUTTON self.imArr = ndimage.fourier_gaussian(self.imArr, 2.0) self.imshow()