def main(argv): TI = TestImage() file1 = argv[0] file2 = argv[1] width1 = int(argv[2]) width2 = int(argv[3]) test = int(argv[4]) TI.test1(file1, file2, width1, width2, test)
# Walk through constructing & inverting the Image/FFT/PSD/ACovF & 1d PSD/1d ACovF. # Comment and uncomment lines to do different things (this is really just a sort of convenient working script). import numpy import pylab from testImage import TestImage # from pImage import PImage # if you don't need to do any plotting from pImagePlots import PImagePlots # if you do need to use the plotting functions for PImage # 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()
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
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
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 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
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
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')
# This snippet of code was to play around with the reflectEdges method & hanning filter, looking at the effect on # the 2-d transforms (look for artifacts). # The wider widths for the reflect edges (which bring edges into the final image) definitely make more artifacts. # The hanning filter also seems to introduce its own artifacts (but which perhaps could be removed?). 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: