def phase_retrieval(args): """ Perform single-material phase retrieval using projection data. Parameters ---------- data : ndarray Projection data. pixel_size : scalar Detector pixel size in cm. dist : scalar Propagation distance of x-rays in cm. energy : scalar Energy of x-rays in keV. alpha : scalar, optional Regularization parameter. padding : bool, optional Applies padding for Fourier transform. For quick testing you can use False for faster results. Returns ------- phase : ndarray Retrieved phase. References ---------- - `J. of Microscopy, Vol 206(1), 33-40, 2001 \ <http://onlinelibrary.wiley.com/doi/10.1046/j.1365-2818.2002.01010.x/abstract>`_ """ data, args, ind_start, ind_end = args H, x_shift, y_shift, tmp_proj, padding = args num_proj, dx, dy = data.shape # dx:slices, dy:pixels for m in range(num_proj): proj = data[m, :, :] if padding: tmp_proj[x_shift:dx+x_shift, y_shift:dy+y_shift] = proj fft_proj = fftw.fftw2(tmp_proj) filtered_proj = np.multiply(H, fft_proj) tmp = np.real(fftw.ifftw2(filtered_proj)) proj = tmp[x_shift:dx+x_shift, y_shift:dy+y_shift] elif not padding: fft_proj = fftw.fftw2(proj) filtered_proj = np.multiply(H, fft_proj) proj = np.real(fftw.ifftw2(filtered_proj)) / np.max(H) data[m, :, :] = proj return ind_start, ind_end, data
def upsample2df(data, level): """ Upsample the slices in Fourier domain. Parameters ---------- data : ndarray, float32 3-D reconstructed data with dimensions: [slices, pixels, pixels] level : scalar, int32 Upsampling level. For example level=2 means, the sinogram will be upsampled by 4, and level=3 means upsampled by 8. Returns ------- output : ndarray Downsampled reconstructed 3-D data with dimensions: [slices, pixels*level^2, pixels*level^2] """ num_slices = np.array(data.shape[0], dtype='int32') num_pixels = np.array(data.shape[1], dtype='int32') binsize = np.power(2, level) fftw2data = np.zeros((num_pixels*binsize, num_pixels*binsize), dtype='complex') upsampled_data = np.zeros((num_slices, num_pixels*binsize, num_pixels*binsize), dtype='float32') ind = slice(num_pixels*(binsize-1)/2, num_pixels*(binsize-1)/2+num_pixels, 1) for m in range(num_slices): fftw2data[ind, ind] = np.fft.fftshift(fftw2(data[m, :, :])) upsampled_data[m, :, :] = np.real(ifftw2(np.fft.ifftshift(fftw2data))) return upsampled_data
def upsample2df(data, level): """ Upsample the slices in Fourier domain. Parameters ---------- data : ndarray, float32 3-D reconstructed data with dimensions: [slices, pixels, pixels] level : scalar, int32 Upsampling level. For example level=2 means, the sinogram will be upsampled by 4, and level=3 means upsampled by 8. Returns ------- output : ndarray Downsampled reconstructed 3-D data with dimensions: [slices, pixels*level^2, pixels*level^2] """ num_slices = np.array(data.shape[0], dtype='int32') num_pixels = np.array(data.shape[1], dtype='int32') binsize = np.power(2, level) fftw2data = np.zeros((num_pixels * binsize, num_pixels * binsize), dtype='complex') upsampled_data = np.zeros( (num_slices, num_pixels * binsize, num_pixels * binsize), dtype='float32') ind = slice(num_pixels * (binsize - 1) / 2, num_pixels * (binsize - 1) / 2 + num_pixels, 1) for m in range(num_slices): fftw2data[ind, ind] = np.fft.fftshift(fftw2(data[m, :, :])) upsampled_data[m, :, :] = np.real(ifftw2(np.fft.ifftshift(fftw2data))) return upsampled_data
def phase_retrieval(args): """ Perform single-material phase retrieval using projection data. Parameters ---------- data : ndarray 3-D tomographic data with dimensions: [projections, slices, pixels] pixel_size : scalar Detector pixel size in cm. dist : scalar Propagation distance of x-rays in cm. energy : scalar Energy of x-rays in keV. alpha : scalar, optional Regularization parameter. padding : bool, optional Applies padding for Fourier transform. For quick testing you can use False for faster results. Returns ------- phase : ndarray Retrieved phase. References ---------- - `J. of Microscopy, Vol 206(1), 33-40, 2001 \ <http://onlinelibrary.wiley.com/doi/10.1046/j.1365-2818.2002.01010.x/abstract>`_ Examples -------- - Phase retrieval: >>> import tomopy >>> >>> # Load data >>> myfile = 'demo/data.h5' >>> data, white, dark, theta = tomopy.xtomo_reader(myfile) >>> >>> # Construct tomo object >>> d = tomopy.xtomo_dataset(log='error') >>> d.dataset(data, white, dark, theta) >>> d.normalize() >>> >>> # Reconstruct data before phase retrieval >>> d.center=661.5 >>> d.gridrec() >>> >>> # Save reconstructed data before phase retrieval >>> output_file='tmp/before_phase_retrieval_' >>> tomopy.xtomo_writer(d.data_recon, output_file) >>> print "Images are succesfully saved at " + output_file + '...' >>> >>> # Reconstruct data after phase retrieval >>> d.phase_retrieval(pixel_size=1e-4, dist=70, energy=20) >>> d.gridrec() >>> >>> # Save reconstructed data after phase retrieval >>> output_file='tmp/after_phase_retrieval_' >>> tomopy.xtomo_writer(d.data_recon, output_file) >>> print "Images are succesfully saved at " + output_file + '...' """ # Arguments passed by multi-processing wrapper ind, dshape, inputs = args # Function inputs data = mp.tonumpyarray(mp.shared_arr, dshape) # shared-array pixel_size, dist, energy, alpha, padding = inputs # Compute the filter. H, x_shift, y_shift, tmp_proj = paganin_filter(data, pixel_size, dist, energy, alpha, padding) num_proj, dx, dy = dshape # dx:slices, dy:pixels for m in ind: proj = data[m, :, :] if padding: tmp_proj[x_shift:dx+x_shift, y_shift:dy+y_shift] = proj fft_proj = fftw.fftw2(tmp_proj) filtered_proj = np.multiply(H, fft_proj) tmp = np.real(fftw.ifftw2(filtered_proj))/np.max(H) proj = tmp[x_shift:dx+x_shift, y_shift:dy+y_shift] elif not padding: fft_proj = fftw.fftw2(proj) filtered_proj = np.multiply(H, fft_proj) proj = np.real(fftw.ifftw2(filtered_proj))/np.max(H) data[m, :, :] = proj
def phase_retrieval(args): """ Perform single-material phase retrieval using projection data. Parameters ---------- data : ndarray 3-D tomographic data with dimensions: [projections, slices, pixels] pixel_size : scalar Detector pixel size in cm. dist : scalar Propagation distance of x-rays in cm. energy : scalar Energy of x-rays in keV. alpha : scalar, optional Regularization parameter. padding : bool, optional Applies padding for Fourier transform. For quick testing you can use False for faster results. Returns ------- phase : ndarray Retrieved phase. References ---------- - `J. of Microscopy, Vol 206(1), 33-40, 2001 \ <http://onlinelibrary.wiley.com/doi/10.1046/j.1365-2818.2002.01010.x/abstract>`_ Examples -------- - Phase retrieval: >>> import tomopy >>> >>> # Load data >>> myfile = 'demo/data.h5' >>> data, white, dark, theta = tomopy.xtomo_reader(myfile) >>> >>> # Construct tomo object >>> d = tomopy.xtomo_dataset(log='error') >>> d.dataset(data, white, dark, theta) >>> d.normalize() >>> >>> # Reconstruct data before phase retrieval >>> d.center=661.5 >>> d.gridrec() >>> >>> # Save reconstructed data before phase retrieval >>> output_file='tmp/before_phase_retrieval_' >>> tomopy.xtomo_writer(d.data_recon, output_file) >>> print "Images are succesfully saved at " + output_file + '...' >>> >>> # Reconstruct data after phase retrieval >>> d.phase_retrieval(pixel_size=1e-4, dist=70, energy=20) >>> d.gridrec() >>> >>> # Save reconstructed data after phase retrieval >>> output_file='tmp/after_phase_retrieval_' >>> tomopy.xtomo_writer(d.data_recon, output_file) >>> print "Images are succesfully saved at " + output_file + '...' """ # Arguments passed by multi-processing wrapper ind, dshape, inputs = args # Function inputs data = mp.tonumpyarray(mp.shared_arr, dshape) # shared-array pixel_size, dist, energy, alpha, padding = inputs # Compute the filter. H, x_shift, y_shift, tmp_proj = paganin_filter(data, pixel_size, dist, energy, alpha, padding) num_proj, dx, dy = dshape # dx:slices, dy:pixels for m in ind: proj = data[m, :, :] if padding: tmp_proj[x_shift:dx + x_shift, y_shift:dy + y_shift] = proj fft_proj = fftw.fftw2(tmp_proj) filtered_proj = np.multiply(H, fft_proj) tmp = np.real(fftw.ifftw2(filtered_proj)) / np.max(H) proj = tmp[x_shift:dx + x_shift, y_shift:dy + y_shift] elif not padding: fft_proj = fftw.fftw2(proj) filtered_proj = np.multiply(H, fft_proj) proj = np.real(fftw.ifftw2(filtered_proj)) / np.max(H) data[m, :, :] = proj