Exemplo n.º 1
0
def compare1d_psd_acovf():
    """Compare 1d ACovF in physical coordinates to 1d PSD in physical coordinates, for two similar but different images."""
    im = TestImage(shift=True, nx=1000, ny=1000)
    scale = 100
    im.addSin(scale=scale)
    im.hanningFilter()
    im.zeroPad()
    im.calcAll(min_npix=1, min_dr=1)
    im.showImage()
    pylab.grid()
    pylab.savefig('compare1d_image1.%s' %(figformat), format='%s' %(figformat))
    im.showPsd2d()
    pylab.savefig('compare1d_psd2d1.%s' %(figformat), format='%s' %(figformat))
    im.showPsd1d()
    pylab.savefig('compare1d_psd1.%s' %(figformat), format='%s' %(figformat))
    im.showAcovf1d()
    pylab.savefig('compare1d_acovf1.%s' %(figformat), format='%s' %(figformat))
    im = TestImage(shift=True, nx=1000, ny=1000)
    im.addSin(scale=scale*2)
    im.hanningFilter()
    im.zeroPad()
    im.calcAll(min_npix=1, min_dr=1)
    im.showImage()
    pylab.grid()
    pylab.savefig('compare1d_image2.%s' %(figformat), format='%s' %(figformat))
    im.showPsd2d()
    pylab.savefig('compare1d_psd2d2.%s' %(figformat), format='%s' %(figformat))
    im.showPsd1d()
    pylab.savefig('compare1d_psd2.%s' %(figformat), format='%s' %(figformat))
    im.showAcovf1d()
    pylab.savefig('compare1d_acovf2.%s' %(figformat), format='%s' %(figformat))
    pylab.close()
    return
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
def inversion():
    """Generate some example images & invert them to reconstruct the original image."""
    im = TestImage(shift=True, nx=1000, ny=1000)
    #im.addEllipseGrid(gridX=200, gridY=100, semiX=50, semiY=25, value=1)
    im.addLines(width=20, spacing=200, value=1, angle=45)
    im.addSin(scale=300)
    im.hanningFilter()
    im.zeroPad()
    #cmap = pylab.cm.gray_r
    cmap = None
    clims = im.showImage(cmap=cmap)
    pylab.savefig('invert_image.%s' %(figformat), format='%s' %(figformat))
    im.calcAll(min_npix=1, min_dr=1)
    # Invert from ACovF and show perfect reconstruction.
    im.invertAcovf2d()
    im.invertPsd2d(useI=True)
    im.invertFft(useI=True)
    im.showImageI(clims=clims, cmap=cmap)
    pylab.savefig('invert_acovf2d_good.%s' %(figformat), format='%s' %(figformat))
    # Invert from ACovF 2d without phases
    im.invertAcovf2d(usePhasespec=False, seed=42)
    im.invertPsd2d(useI=True)
    im.invertFft(useI=True)    
    im.showImageI(clims=clims, cmap=cmap)
    pylab.savefig('invert_acovf2d_nophases.%s' %(figformat), format='%s' %(figformat))
    # Invert from ACovF 1d with phases
    im.invertAcovf1d(phasespec=im.phasespec)
    im.invertAcovf2d(useI=True)
    im.invertPsd2d(useI=True)
    im.invertFft(useI=True)
    im.showImageI(clims=clims, cmap=cmap)
    pylab.savefig('invert_acovf1d_phases.%s' %(figformat), format='%s' %(figformat))
    # Invert from ACovF 1d without phases
    im.invertAcovf1d(seed=42)
    im.invertAcovf2d(useI=True)
    im.invertPsd2d(useI=True)
    im.invertFft(useI=True)
    im.showImageI(clims=clims, cmap=cmap)
    pylab.savefig('invert_acovf1d_nophases.%s' %(figformat), format='%s' %(figformat))
    # Recalculate 1-d PSD and ACovF from this last reconstructed image (ACovF1d no phases)
    im2 = PImagePlots()
    im2.setImage(im.imageI)
    im2.calcAll(min_npix=1, min_dr=1)
    legendlabels=['Reconstructed', 'Original']
    im2.showPsd1d(comparison=im, legendlabels=legendlabels)
    pylab.savefig('invert_recalc_ACovF_Psd1d.%s' %(figformat), format='%s' %(figformat))
    im2.showAcovf1d(comparison=im, legendlabels=legendlabels)
    pylab.savefig('invert_recalc_ACovF_Acovf1d.%s' %(figformat), format='%s' %(figformat))
    # Invert from PSD and show perfect reconstruction.                          
    im.invertPsd2d()
    im.invertFft(useI=True)
    im.showImageI(clims=clims, cmap=cmap)
    pylab.savefig('invert_psd2d_good.%s' %(figformat), format='%s' %(figformat))
    # Invert from PSD 2d without phases
    im.invertPsd2d(usePhasespec=False, seed=42)
    im.invertFft(useI=True)
    im.showImageI(clims=clims, cmap=cmap)
    pylab.savefig('invert_psd2d_nophases.%s' %(figformat), format='%s' %(figformat))
    # Invert from PSD 1d with phases                                   
    im.invertPsd1d(phasespec=im.phasespec)
    im.invertPsd2d(useI=True)
    im.invertFft(useI=True)
    im.showImageI(clims=clims, cmap=cmap)
    pylab.savefig('invert_psd1d_phases.%s' %(figformat), format='%s' %(figformat))
    # Invert from PSD 1d without phases                                             
    im.invertPsd1d(seed=42)
    im.invertPsd2d(useI=True)
    im.invertFft(useI=True)
    im.showImageI(clims=clims, cmap=cmap)
    pylab.savefig('invert_psd1d_nophases.%s' %(figformat), format='%s' %(figformat))
    # Recalculate 1-d PSD and ACovF from this last reconstructed image (PSD1d no phases)
    im2 = PImagePlots()
    im2.setImage(im.imageI)
    im2.calcAll(min_npix=1, min_dr=1)
    im2.showPsd1d(comparison=im, legendlabels=legendlabels)
    pylab.savefig('invert_recalc_PSD_Psd1d.%s' %(figformat), format='%s' %(figformat))
    im2.showAcovf1d(comparison=im, legendlabels=legendlabels)
    pylab.savefig('invert_recalc_PSD_Acovf1d.%s' %(figformat), format='%s' %(figformat))
    pylab.close()
    return
Exemplo n.º 5
0
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')