def psd1d(image,dx,binsize=1.,pad=True): """ NAME: psd1d PURPOSE: Calculate the 1D, azimuthally averaged power spectrum of an image INPUT: image - the image [NxM] dx- spacing in X and Y directions binsize= (1) radial binsize in terms of Nyquist frequency OUTPUT: the 1D power spectrum using definitions of NR 13.4 eqn. (13.4.5) HISTORY: 2014-06-06 - Written - Bovy (IAS) """ #First subtract DC component image-= numpy.mean(image[True-numpy.isnan(image)]) #Calculate the 2D PSD ret2d= psd2d(image,pad=pad) nr, radii,ret= azimuthalAverage(ret2d,returnradii=False,binsize=binsize, dx=1./2./dx/image.shape[0], dy=1./2./dx/image.shape[1], interpnan=False, return_nr=True) return (radii/2.**pad/dx/(image.shape[0]/2.-0.),ret,ret/numpy.sqrt(nr))
def powerspectrum(f): ff = np.fft.fftshift(np.fft.fft2(f)) ff = ff / ff.shape[0] / ff.shape[1] ps2 = np.abs(ff)**2 r, ps = radialprofile.azimuthalAverage(ps2, returnradii=True) return np.column_stack((r, ps))
def crosspower(f1,f2): ff1 = np.fft.fftshift(np.fft.fft2(f1)) ff2 = np.fft.fftshift(np.fft.fft2(f2)) ff1 = ff1 / ff1.shape[0] / ff1.shape[1] ff2 = ff2 / ff2.shape[0] / ff2.shape[1] ps2 = np.real(ff1 * np.conjugate(ff2)) r, ps = radialprofile.azimuthalAverage(ps2, returnradii = True) return np.column_stack((r, ps))
def residual_prof(label): colours = ['r', 'g', 'b'] for i, band in enumerate(bands): f = 'stackres_%s_%s.fits'%(label, band) if os.path.exists(f): res = pyfits.getdata(f) rad, prof = azimuthalAverage(res, returnradii=True, binsize=1.0) middle = rad < res.shape[0]/2.0 rad, prof = [x[middle] for x in (rad, prof)] rad /= scales pyplot.plot(rad, prof, '-', color=colours[i]) pyplot.axis('on') else: pyplot.axis('off')
def reduce(self): '''convert raw image data to R(Q), also get other terms''' if self.image is None: # TODO: make this conditional on need to reduce image data self.read_image_data() if abs(self.image.xsize - self.image.ysize) > PIXEL_SIZE_TOLERANCE: raise ValueError('X & Y pixels have different sizes, not prepared for this') # TODO: construct the image mask # radial averaging (average around the azimuth at constant radius, repeat at all radii) with numpy.errstate(invalid='ignore'): radii, rAvg = azimuthalAverage(self.image.image, center=(self.image.x0, self.image.y0), returnradii=True) # standard deviation results do not look right, skip that in full data reduction # with numpy.errstate(invalid='ignore'): # with warnings.catch_warnings(): # # sift out this RuntimeWarning warning from numpy # warnings.filterwarnings('ignore', r'Degrees of freedom <= 0 for slice') # rAvgDev = azimuthalAverage(self.image.image, # center=(self.image.x0, self.image.y0), # stddev=True) radii *= self.image.xsize Q = (4*math.pi / self.image.wavelength) * numpy.sin(0.5*numpy.arctan2(radii, self.image.SDD)) # scale_factor = 1 /self.image.I0_gain / self.image.I0 # TODO: verify the equation scale_factor = 1 / self.image.I0 # TODO: verify the equation rAvg = rAvg * scale_factor # remove NaNs and non-positives from output data rAvg = numpy.ma.masked_less_equal(rAvg, 0) # mask the non-positives rAvg = numpy.ma.masked_invalid(rAvg) # mask the NaNs Q = calc.remove_masked_data(Q, rAvg.mask) radii = calc.remove_masked_data(radii, rAvg.mask) # rAvgDev = calc.remove_masked_data(rAvgDev, rAvg.mask) rAvg = calc.remove_masked_data(rAvg, rAvg.mask) # always remove the masked array last full = dict(Q=Q, R=rAvg, x=radii) self.reduced = dict(full = full) # reset the entire dictionary with new "full" reduction
def main(imagefile = "IsoToRaster01_small.jpg"): image = cv2.imread("./data/"+imagefile) im_gray = cv2.imread("./data/"+imagefile, cv2.CV_LOAD_IMAGE_GRAYSCALE) (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # Take the fourier transform of the image. F1 = fftpack.fft2(im_bw) #F1 = cv2.dft(np.float32(im_bw),flags = cv2.DFT_COMPLEX_OUTPUT) # Now shift the quadrants around so that low spatial frequencies are in # the center of the 2D fourier transformed image. F2 = fftpack.fftshift( F1 ) # Calculate a 2D power spectrum psd2D = np.abs( F2 )**2 # Calculate the azimuthally averaged 1D power spectrum psd1D = radialprofile.azimuthalAverage(psd2D) # Now plot up both py.figure(1) py.clf() py.imshow( np.log10( image ), cmap=py.cm.Greys) py.figure(2) py.clf() py.imshow( np.log10( psd2D )) py.figure(3) py.clf() py.semilogy( psd1D ) py.xlabel('Spatial Frequency') py.ylabel('Power Spectrum') py.show() return 0
Z = clip(Z,0,2) rows,cols = Z.shape Z[:rows-1,:cols-1] if fa == 1: # First for SFR # Take the fourier transform of the image. F1 = fftpack.fft2(Z) # Now shift the quadrants around so that low spatial frequencies are in # the center of the 2D fourier transformed image. F2 = fftpack.fftshift(F1) # Calculate a 2D power spectrum psd2D = np.abs( F2 )**2 # Calculate the azimuthally averaged 1D power spectrum psd1D = radialprofile.azimuthalAverage(psd2D) #Secondly for Dencity # Take the fourier transform of the image. F1den = fftpack.fft2(Zden) # Now shift the quadrants around so that low spatial frequencies are in # the center of the 2D fourier transformed image. F2den = fftpack.fftshift(F1den) # Calculate a 2D power spectrum psd2Dden = np.abs( F2den )**2 # Calculate the azimuthally averaged 1D power spectrum psd1Dden = radialprofile.azimuthalAverage(psd2Dden) print 'creating cross power spectrum' G1 = F2 G2con = np.conjugate(F2den)
def getFreqs(ar): freqImage = numpy.abs(numpy.fft.fft2(ar)) freqs = azimuthalAverage(freqImage) return freqs
#img = np.random.randint(0, 255, (100,100)) x, y = np.mgrid[-100:100:200j, -100:100:200j] dist = np.hypot(x, y) # Linear distance from point 0, 0 img = np.cos(dist / 5 / np.pi) plt.figure() plt.imshow(img) plt.colorbar() plt.show() img = convert_to_8bits(img) plt.figure() plt.imshow(img, cmap="hot") plt.colorbar() plt.show() # center, radi = find_centroid(img) center, radi = (100, 100), 5 rad = radial_profile(img, center) plt.figure("Method 1") plt.plot(rad[radi:]) #plt.show() # Other method az = azimuthalAverage(img) plt.figure("Method 2") plt.plot(az) plt.show()
x, y = np.mgrid[-100:100:200j, -100:100:200j] dist = np.hypot(x, y) # Linear distance from point 0, 0 img = np.cos( dist/5 / np.pi) plt.figure() plt.imshow(img) plt.colorbar() plt.show() img = convert_to_8bits(img) plt.figure() plt.imshow(img,cmap="hot") plt.colorbar() plt.show() # center, radi = find_centroid(img) center, radi = (100, 100), 5 rad = radial_profile(img, center) plt.figure("Method 1") plt.plot(rad[radi:]) #plt.show() # Other method az = azimuthalAverage(img) plt.figure("Method 2") plt.plot(az) plt.show()
rainfieldSmoothed * window) # FFTW implementation # Turn on the cache for optimum performance pyfftw.interfaces.cache.enable() # Shift frequencies fprecip = np.fft.fftshift(fprecipNoShift) # Compute 2D power spectrum psd2d = np.abs(fprecip)**2 / (fftDomainSize * fftDomainSize) psd2dNoShift = np.abs(fprecipNoShift)**2 / (fftDomainSize * fftDomainSize) # Compute 1D radially averaged power spectrum bin_size = 1 nr_pixels, bin_centers, psd1d = radialprofile.azimuthalAverage( psd2d, binsize=bin_size, return_nr=True) fieldSize = rainrate.shape minFieldSize = np.min(fieldSize) # Extract subset of spectrum validBins = ( bin_centers < minFieldSize / 2 ) # takes the minimum dimension of the image and divide it by two psd1d = psd1d[validBins] # Compute frequencies freq = fftpack.fftfreq(minFieldSize, d=float(resKm)) freqAll = np.fft.fftshift(freq) # Select only positive frequencies freq = freqAll[len(psd1d):]
fprecipNoShift = np.fft.fft2(rainfieldSmoothed*window) # Numpy implementation if FFTmod == 'FFTW': fprecipNoShift = pyfftw.interfaces.numpy_fft.fft2(rainfieldSmoothed*window) # FFTW implementation # Turn on the cache for optimum performance pyfftw.interfaces.cache.enable() # Shift frequencies fprecip = np.fft.fftshift(fprecipNoShift) # Compute 2D power spectrum psd2d = np.abs(fprecip)**2/(fftDomainSize*fftDomainSize) psd2dNoShift = np.abs(fprecipNoShift)**2/(fftDomainSize*fftDomainSize) # Compute 1D radially averaged power spectrum bin_size = 1 nr_pixels, bin_centers, psd1d = radialprofile.azimuthalAverage(psd2d, binsize=bin_size, return_nr=True) fieldSize = rainrate.shape minFieldSize = np.min(fieldSize) # Extract subset of spectrum validBins = (bin_centers < minFieldSize/2) # takes the minimum dimension of the image and divide it by two psd1d = psd1d[validBins] # Compute frequencies freq = fftpack.fftfreq(minFieldSize, d=float(resKm)) freqAll = np.fft.fftshift(freq) # Select only positive frequencies freq = freqAll[len(psd1d):] # Compute wavelength [km]