def get_mask(samples, backgrounds=None, xoff=None, yoff=None, cfg=None): """ Returns a thresholded mask where an imaged object is suspected to be. It could be useful in high contrast images where object borders are sufficiently well determined. Args: ----- samples: the acquired samples as a dictionary with angles as keys. backgrounds: the acquired background as a dictionary with angles as keys. They must be acquired right after or before taking the samples. xoff: offset in the 'x' direction of this sample's center. yoff: offset in the 'y' direction of this sample's center. cfg: configuration (named tuple) Returns: -------- (ndarray) mask containing the object to be reconstructed. """ corr_ims = list() iterator = fpmm.set_iterator(cfg) for index, theta, shift in iterator: image = samples[(theta, shift)] image = fpmm.crop_image(image, cfg.patch_size, xoff, yoff) # image, image_size = image_rescaling(image, cfg) background = backgrounds[(theta, shift)] background = fpmm.crop_image(background, cfg.patch_size, xoff, yoff) # background, image_size = image_rescaling(background, cfg) corr_ims.append(image_correction(image, background, mode='background')) mask = np.mean(corr_ims, axis=0) # thres = 140 # hardcoded mask[mask < thres] = 1 mask[mask > thres] = 0 # print(Et[np.abs(Et) > .1]) return mask
def initialize(hrsize=None, backgrounds=None, xoff=None, yoff=None, cfg=None, mode='zero'): """ Initializes the algorithm using one of various modalities. Args: ----- samples: the acquired samples as a dictionary with angles as keys. backgrounds: the acquired background as a dictionary with angles as keys. They must be acquired right after or before taking the samples. xoff: offset in the 'x' direction of this sample's center. yoff: offset in the 'y' direction of this sample's center. cfg: configuration (named tuple) mode: the modality according to which the algorithm is going to be initilized. Can be one of the following options: * zero: constant phase and zero amplitude. * transmission: the transmitted image at (0, 0) angles. * mean: takes all the samples and substracts the (measured) backround. Then takes the mean of all of them. Returns: -------- (ndarray) a complex image with the same size of the samples. """ # image = fpmm.crop_image(samples[(0, 0)], cfg.patch_size, xoff, yoff) # image, image_size = image_rescaling(image, cfg) if mode == 'zero': Ih_sq = 0.5 * np.ones(hrsize) # Homogeneous amplitude Ph = np.zeros(hrsize) # and null phase Et = Ih_sq * np.exp(1j * Ph) elif mode == 'transmission': background = fpmm.crop_image(backgrounds[(0, 0)], cfg.patch_size, xoff, yoff) background, image_size = image_rescaling(background, cfg) image = image_correction(image, background, mode='background') Ih_sq = np.sqrt(image) Ph = np.zeros_like(Ih_sq) # and null phase Et = Ih_sq * np.exp(1j * Ph) elif mode == 'mean': corr_ims = list() iterator = fpmm.set_iterator(cfg) for index, theta, shift in iterator: # Final step: squared inverse fft for visualization image = fpmm.crop_image(samples[(theta, shift)], cfg.patch_size, xoff, yoff) background = fpmm.crop_image(backgrounds[(theta, shift)], cfg.patch_size, xoff, yoff) image, image_size = image_rescaling(image, cfg) background, image_size = image_rescaling(background, cfg) corr_ims.append( image_correction(image, background, mode='background')) Ih = np.mean(corr_ims, axis=0) # Ph = 0.5+np.pi*np.abs(Et)/np.max(Et) Et = np.sqrt(Ih) * np.exp(1j * 0) return Et
# from pyfpm.data import json_savemeta, json_loadmeta import pyfpm.data as dt from pyfpm.coordinates import PlatformCoordinates from pyfpm import implot import pyfpm.local as local # Simulation parameters CONFIG_FILE = './etc/config.yaml' cfg = dt.load_config(CONFIG_FILE) simclient = local.SimClient(cfg=cfg) pc = PlatformCoordinates(theta=0, phi=0, height=cfg.sample_height, cfg=cfg) in_file = os.path.join(cfg.output_sample, '2017-04-12_182910.npy') ss_dict = np.load(cfg.output_sample+'2017-04-12_182910ss_dict.npy')[()] power_dict = np.load(cfg.output_sample+'2017-04-12_182910power_dict.npy')[()] iterator = fpm.set_iterator(cfg) image_dict = np.load(in_file)[()] fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(25, 15)) plt.grid(False) fig.show() cum_diff = 0 for index, theta, phi in iterator: # Coordinates math # time.sleep(0.5) pc.heigth = 88 pc.platform_tilt = [0, 2] pc.source_center = [0, 2] #[xcoord, ycoord] of the calibrated center pc.set_coordinates(theta=theta, phi=phi, units='degrees') t_corr, p_corr = pc.source_coordinates(mode='angular')
from pyfpm import web from pyfpm.reconstruct import fpm_reconstruct from pyfpm.fpmmath import set_iterator from pyfpm.data import save_yaml_metadata import pyfpm.data as dt from pyfpm.coordinates import PlatformCoordinates # Simulation parameters cfg = dt.load_config() samples, comp_cfg = dt.open_sampled('2017-05-26_152307.npy') background, comp_cfg = dt.open_sampled('2017-05-26_145449_blank.npy') # Connect to a web client running serve_microscope.py pc = PlatformCoordinates(theta=0, phi=0, height=cfg.sample_height, cfg=cfg) pc.generate_model(cfg.plat_model) iterator = set_iterator(cfg) # reconstruction dx = cfg.patch_size[0] x_range = range(0, cfg.video_size[1], dx)[:-1] y_range = range(0, cfg.video_size[0], dx)[:-1] # for i, point in enumerate(it.product(x_range, y_range)): # init_point = [point[0], point[1]] # rec, phase = fpm_reconstruct(samples, background, iterator, init_point, # cfg=cfg, debug=False) # misc.imsave('./misc/ph'+str(i)+'.png', phase) # misc.imsave('./misc/im'+str(i)+'.png', rec) rec, phase = fpm_reconstruct(samples, background, iterator, [90, 80], cfg=comp_cfg, debug=True) plt.show()