def example_lines(): """Plot for example_lines, illustrating entire forward process both with and without hanning filter.""" im = TestImage(shift=True, nx=1000, ny=1000) im.addLines(width=10, spacing=75, value=5, angle=45) im.zeroPad() im.calcAll() im.plotMore() pylab.savefig('example_lines_a.%s' %(figformat), format='%s' %(figformat)) pylab.close() im = TestImage(shift=True, nx=1000, ny=1000) im.addLines(width=10, spacing=75, value=5, angle=45) im.hanningFilter() im.zeroPad() im.calcAll() im.plotMore() pylab.savefig('example_lines_b.%s' %(figformat), format='%s' %(figformat)) pylab.close() return
def elliptical(): """Generate a test image with random ellipses and background noise.""" im = TestImage(shift=True, nx=1000, ny=1000) im.addEllipseRandom(nEllipse=100, value=5) im.addNoise(sigma=1) im.hanningFilter() #im.zeroPad() im.calcAll(min_npix=2, min_dr=1) im.plotMore() pylab.savefig('elliptical.%s' %(figformat), format='%s' %(figformat)) # Invert from ACovF 1d without phases im.invertAcovf1d() im.invertAcovf2d(useI=True) im.invertPsd2d(useI=True) im.invertFft(useI=True) im.showImageAndImageI() pylab.savefig('elliptical_invert.%s' %(figformat), format='%s' %(figformat)) pylab.close() return
# Use TestImage to set up the image im = TestImage(shift=True, nx=750, ny=750) im.addLines(spacing=50, width=10, value=10, angle=0) #im.addGaussian(xwidth=30, ywidth=30) #im.addSin(scale=100) #im.addCircle(radius=20) #im.addEllipseRandom(nEllipse=100, value=5) im.addNoise(sigma=1.) #im.hanningFilter() #im.zeroPad() # Calculate FFT/PSD2d/ACovF/PSD1d in one go (can do these separately too). # Use automatic binsize or user-defined binsize. im.calcAll(min_dr=1.0, min_npix=2) im.plotMore() #im.showImage() #im.showAcovf2d(log=True, imag=False) #im.showPsd1d() #im.showAcovf1d() #im.showSf() # Start at various points in inverting to reconstruct image (comment out stages above where you want to start). # What I find is that not including the phases means a randomly lumpy reconstructed image - nothing like original # image. # However, if you start with the 1d ACovF (even with no phase info), then you can reconstruct something which then # will let you recalculate a pretty good copy of the original 1d ACovF. # Similarly, if you start with the 1D PSD, you can reconstruct a pretty good copy of the 1d PSD. # BUT, starting with the 1d PSD means that the 1d ACovF will not be so good at large scales, and starting with the # 1d ACovF means that the reconstructed 1d PSD will not be so good at small scales (although these scales seem to be so
def gaussian_example(): """Generate the plots for the example gaussian ... a more detailed version of this is walked through in the testGaussian.py code. """ gauss = TestImage(shift=True, nx=1000, ny=1000) sigma_x = 10. gauss.addGaussian(xwidth=sigma_x, ywidth=sigma_x, value=1) gauss.zeroPad() gauss.calcAll(min_npix=2, min_dr=1) gauss.plotMore() pylab.savefig('gauss_all.%s' %(figformat), format='%s' %(figformat)) # pull slice of image x = numpy.arange(0, gauss.nx, 1.0) d = gauss.image[round(gauss.ny/2.0)][:] mean = gauss.xcen sigma = sigma_x fitval, expval = dofit(x, d, mean, sigma) doplot(x, d, fitval, expval, 'Image slice', xlabel='Pixels') pylab.savefig('gauss_image.%s' %(figformat), format='%s' %(figformat)) # pull slice of FFT x = gauss.xfreq d = fftpack.ifftshift(gauss.fimage)[0][:].real d = numpy.abs(d) idx = numpy.argsort(x) d = d[idx] x = x[idx] mean = 0 sigma_fft = 1/(2.0*numpy.pi*sigma_x) fitval, expval = dofit(x, d, mean, sigma_fft) doplot(x, d, fitval, expval, 'FFT slice', xlabel='Frequency') pylab.xlim(-.2, .2) pylab.savefig('gauss_fft.%s' %(figformat), format='%s' %(figformat)) # pull slice from PSD d = fftpack.ifftshift(gauss.psd2d)[0][:].real d = d[idx] mean = 0 sigma_psd_freq = sigma_fft/numpy.sqrt(2) fitval, expval = dofit(x, d, mean, sigma_psd_freq) doplot(x, d, fitval, expval, 'PSD 2-d slice', xlabel='Frequency') pylab.xlim(-.2, .2) pylab.savefig('gauss_psd_freq.%s' %(figformat), format='%s' %(figformat)) # and look at slice from PSD in spatial scale x = numpy.arange(-gauss.xcen, gauss.nx-gauss.xcen, 1.0) d = gauss.psd2d[round(gauss.ny/2.0)][:].real mean = 0 sigma_psd_pix = 1/(sigma_x*numpy.sqrt(2))*numpy.sqrt(gauss.nx*gauss.ny)/(2.0*numpy.pi) fitval, expval = dofit(x, d, mean, sigma_psd_pix) doplot(x, d, fitval, expval, 'PSD 2-d slice, spatial scale', xlabel='"Pixels"') pylab.xlim(-200, 200) pylab.savefig('gauss_psd_x.%s' %(figformat), format='%s' %(figformat)) # Show 1d PSD in both frequency and pixel space gauss.showPsd1d(linear=True) pylab.savefig('gauss_psd1d_all.%s' %(figformat), format='%s' %(figformat)) # and check 1d PSD in frequency space (spatial space doesn't work ...) x = gauss.rfreq d = gauss.psd1d.real sigma = sigma_psd_freq fitval, expval = dofit(x, d, 0., sigma) doplot(x, d, fitval, expval, 'PSD 1-d', xlabel='Frequency') pylab.savefig('gauss_psd1d.%s' %(figformat), format='%s' %(figformat)) # pull slice from ACovF x = numpy.arange(-gauss.xcen, gauss.nx-gauss.xcen, 1.) d = gauss.acovf.real[round(gauss.ny/2.0)][:] mean = 0 sigma_acovf = sigma_x*numpy.sqrt(2) fitval, expval = dofit(x, d, mean, sigma_acovf) doplot(x, d, fitval, expval, 'ACovF 2-d slice', xlabel='Pixels') pylab.xlim(-200, 200) pylab.savefig('gauss_acovf.%s' %(figformat), format='%s' %(figformat)) # and check 1d ACovF x = gauss.acovfx d = gauss.acovf1d.real sigma_acovf = sigma_x*numpy.sqrt(2) fitval, expval = dofit(x, d, mean, sigma_acovf) doplot(x, d, fitval, expval, 'ACovF 1-d', xlabel='Pixels') pylab.savefig('gauss_acovf1d.%s' %(figformat), format='%s' %(figformat)) pylab.close() return
import pylab from testImage import TestImage # Plain image, with noise im = TestImage() im.addLines(width=10, spacing=50, value=5) #im.addNoise() im.hanningFilter() im.zeroPad() im.calcAll() #im.plotAll(title='Gaussian Noise') im.plotMore(title='Gaussian Noise') pylab.show() exit() # Gaussian image im = TestImage() im.addGaussian(xwidth=20, ywidth=20) im.hanningFilter() im.zeroPad() im.calcAll() im.plotAll(title='Gaussian') # Sin, s=100 im = TestImage() im.addSin(scale=100) im.hanningFilter() im.zeroPad() im.calcAll() im.plotAll(title='Sin, scale=100')
import numpy import pylab from testImage import TestImage for i in range(1, 5): im =TestImage(shift=True, nx=500, ny=500) im.addSin(scale=100) #im.addLines(angle=30, width=20, spacing=50) #im.image += 10.0 width=500/float(i) print width im.reflectEdges(width=width) im.hanningFilter() #im.showImage() im.calcAll() im.plotMore() pylab.suptitle('Reflect edges (width %f) and hanning filter' %(width)) width= 0 for i in (0, 1, 2, 3, 4): im = TestImage(shift=True, nx=500, ny=500) im.addSin(scale=100) #im.addLines(angle=30, width=20, spacing=50) #im.image += 10.0 if i == 0: title = 'Plain image only' if i == 1: im.hanningFilter() title = 'Hanning filter only' if i == 2: im.zeroPad()