├── .gitignore ├── README.md ├── einstein.bmp ├── img-reconstruct.py ├── progress.gif └── transform.png /.gitignore: -------------------------------------------------------------------------------- 1 | frequencyspace/* 2 | realspace/* 3 | save/* 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Image-Reconstruction 2 | Image reconstruction from a diffraction pattern, as in Coherent Diffraction Imaging (CDI) 3 | 4 | This is an implementation of the hybrid input-output (HIO) algorithm, done in python using the numpy and scipy libraries. 5 | The algorithm works by iteratively transforming an image between real space and Fourier space, applying constraints at each step. 6 | In this implementation, the real space image is constrained to be positive, real, and required to have compact support (this is valid since the diffraction pattern is taken with oversampling). 7 | The Fourier constraint is that the intensity of the transformed image must be the measured intensity given as input. 8 | 9 | For example, given a diffraction pattern: 10 | 11 | ![Diffract](https://raw.githubusercontent.com/cwg45/Image-Reconstruction/master/transform.png) 12 | 13 | We can reconstruct the original image: 14 | 15 | ![Progress](https://raw.githubusercontent.com/cwg45/Image-Reconstruction/master/progress.gif) 16 | 17 | Citations: 18 | 19 | J. R. Fienup, "Phase retrieval algorithms: a comparison," Appl. Opt. 21, 2758-2769 (1982) 20 | 21 | -------------------------------------------------------------------------------- /einstein.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwgoddard/Image-Reconstruction/d889f9a6a17b6d9083f0a2b1371f4b2e67f88787/einstein.bmp -------------------------------------------------------------------------------- /img-reconstruct.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import numpy.fft as fft 3 | import scipy.ndimage as nd 4 | import scipy.misc as misc 5 | from math import pi 6 | 7 | #Read in source image 8 | source = nd.imread("einstein.bmp", flatten=True) 9 | 10 | #Pad image to simulate oversampling 11 | pad_len = len(source) 12 | padded = np.pad(source, ((pad_len, pad_len),(pad_len, pad_len)), 'constant', 13 | constant_values=((0,0),(0,0))) 14 | 15 | ft = fft.fft2(padded) 16 | 17 | #simulate diffraction pattern 18 | diffract = np.abs(ft) 19 | 20 | l = len(padded) 21 | 22 | #keep track of where the image is vs the padding 23 | mask = np.ones((pad_len+2,pad_len+2)) 24 | mask = np.pad(mask, ((pad_len-1, pad_len-1),(pad_len-1, pad_len-1)), 'constant', 25 | constant_values=((0,0),(0,0))) 26 | 27 | #Initial guess using random phase info 28 | guess = diffract * np.exp(1j * np.random.rand(l,l) * 2 * pi) 29 | 30 | #number of iterations 31 | r = 801 32 | 33 | #step size parameter 34 | beta = 0.8 35 | 36 | #previous result 37 | prev = None 38 | for s in range(0,r): 39 | #apply fourier domain constraints 40 | update = diffract * np.exp(1j * np.angle(guess)) 41 | 42 | inv = fft.ifft2(update) 43 | inv = np.real(inv) 44 | if prev is None: 45 | prev = inv 46 | 47 | #apply real-space constraints 48 | temp = inv 49 | for i in range(0,l): 50 | for j in range(0,l): 51 | #image region must be positive 52 | if inv[i,j] < 0 and mask[i,j] == 1: 53 | inv[i,j] = prev[i,j] - beta*inv[i,j] 54 | #push support region intensity toward zero 55 | if mask[i,j] == 0: 56 | inv[i,j] = prev[i,j] - beta*inv[i,j] 57 | 58 | 59 | prev = temp 60 | 61 | guess = fft.fft2(inv) 62 | 63 | #save an image of the progress 64 | if s % 10 == 0: 65 | misc.imsave("/Users/chasegoddard/Stuff/CDI/code/save/progress" + str(s) + 66 | ".bmp", prev) 67 | print(s) 68 | 69 | 70 | -------------------------------------------------------------------------------- /progress.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwgoddard/Image-Reconstruction/d889f9a6a17b6d9083f0a2b1371f4b2e67f88787/progress.gif -------------------------------------------------------------------------------- /transform.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwgoddard/Image-Reconstruction/d889f9a6a17b6d9083f0a2b1371f4b2e67f88787/transform.png --------------------------------------------------------------------------------