def main(): args = parse_args() syris.init() n = 1024 d = args.propagation_distance * q.m shape = (n, n) ps = 1 * q.um energy = 20 * q.keV tr = Trajectory([(n / 2, n / 2, 0)] * ps, pixel_size=ps) sample = make_sphere(n, n / 30 * ps, pixel_size=ps, material=get_material('air_5_30_kev.mat')) bm = make_topotomo(pixel_size=ps, trajectory=tr) print 'Source size FWHM (height x width): {}'.format(bm.size.rescale(q.um)) u = bm.transfer(shape, ps, energy, t=0 * q.s) u = sample.transfer(shape, ps, energy) intensity = propagate([sample], shape, [energy], d, ps).get() incoh = bm.apply_blur(intensity, d, ps).get() region = (n / 4, n / 4, n / 2, n / 2) intensity = ip.crop(intensity, region).get() incoh = ip.crop(incoh, region).get() show(intensity, title='Coherent') show(incoh, title='Applied source blur') plt.show()
def main(): args = parse_args() syris.init(device_index=0) m = 20 if args.input == 'grid': image = make_grid(args.n, m * q.m).thickness.get() elif args.input == 'lena': from scipy.misc import lena image = lena().astype(cfg.PRECISION.np_float) if args.n != image.shape[0]: image = gutil.get_host(ip.rescale(image, (args.n, args.n))) n = image.shape[0] crop_n = n - 2 * m - 2 y, x = np.mgrid[-n / 2:n / 2, -n / 2:n / 2] # Compute a such that the disk diameter is exactly the period when distance from the middle is n # / 2 a = m / (2 * (crop_n / 2.)**2) radii = (a * np.sqrt(x**2 + y**2)**2 + 1e-3).astype(cfg.PRECISION.np_float) x_param = radii y_param = radii result = ip.varconvolve_disk(image, (y_param, x_param)).get() result = ip.crop(result, (m - 1, m - 1, crop_n, crop_n)).get() radii = ip.crop(radii, (m - 1, m - 1, crop_n, crop_n)).get() image = ip.crop(image, (m - 1, m - 1, crop_n, crop_n)).get() if args.output: save_image(args.output, result) show(image, title='Original Image') show(2 * radii, title='Blurring Disk Diameters') show(result, title='Blurred Image') plt.show()
def main(): args = parse_args() syris.init(device_index=0) m = 20 if args.input == 'grid': image = make_grid(args.n, m * q.m).thickness.get() elif args.input == 'lena': from scipy.misc import lena image = lena().astype(cfg.PRECISION.np_float) if args.n != image.shape[0]: image = gutil.get_host(ip.rescale(image, (args.n, args.n))) n = image.shape[0] crop_n = n - 2 * m - 2 y, x = np.mgrid[-n / 2:n / 2, -n / 2:n / 2] # Compute a such that the disk diameter is exactly the period when distance from the middle is n # / 2 a = m / (2 * (crop_n / 2.) ** 2) radii = (a * np.sqrt(x ** 2 + y ** 2) ** 2 + 1e-3).astype(cfg.PRECISION.np_float) x_param = radii y_param = radii result = ip.varconvolve_disk(image, (y_param, x_param)).get() result = ip.crop(result, (m - 1, m - 1, crop_n, crop_n)).get() radii = ip.crop(radii, (m - 1, m - 1, crop_n, crop_n)).get() image = ip.crop(image, (m - 1, m - 1, crop_n, crop_n)).get() if args.output: save_image(args.output, result) show(image, title='Original Image') show(2 * radii, title='Blurring Disk Diameters') show(result, title='Blurred Image') plt.show()
def test_simple(): syris.init(device_index=0) n = 8 ps = 1 * q.um thickness = np.arange(n**2).reshape(n, n).astype( cfg.PRECISION.np_float) * q.m go = StaticBody(thickness, ps) # Same projection = go.project((n, n), ps).get() np.testing.assert_almost_equal(thickness.magnitude, projection) # Cropped upsampled shape = (n, n) gt = rescale(thickness.magnitude, shape).get() projection = go.project(shape, ps / 2).get() gt = rescale(crop(thickness.magnitude, (0, 0, n / 2, n / 2)), shape).get() np.testing.assert_almost_equal(gt, projection) # Cropped downsampled shape = (n / 4, n / 4) gt = rescale(thickness.magnitude, shape).get() projection = go.project(shape, 2 * ps).get() gt = rescale(crop(thickness.magnitude, (0, 0, n / 2, n / 2)), shape).get() np.testing.assert_almost_equal(gt, projection) # Padded upsampled shape = (4 * n, 4 * n) projection = go.project(shape, ps / 2, offset=(-n / 2, -n / 2) * ps).get() gt = rescale(pad(thickness.magnitude, (n / 2, n / 2, 2 * n, 2 * n)), shape).get() np.testing.assert_almost_equal(gt, projection) # Padded downsampled shape = (n, n) projection = go.project(shape, 2 * ps, offset=(-n / 2, -n / 2) * ps).get() gt = rescale(pad(thickness.magnitude, (4, 4, 2 * n, 2 * n)), shape).get() np.testing.assert_almost_equal(gt, projection) # Crop and pad and upsample def crop_pad_rescale(ss): shape = (n, n) target_ps = ps / ss fov = shape * ps offset = (2, -2) * q.um shape = (int(n / 2 * ss), int(ss * 3 * n / 2)) projection = go.project(shape, target_ps, offset=offset).get() gt = rescale( pad(thickness[n / 4:3 * n / 4, :], (0, n / 4, n / 2, 3 * n / 2)), shape).get() np.testing.assert_almost_equal(gt, projection) crop_pad_rescale(2) crop_pad_rescale(0.5)
def test_crop(self): shape = 8, 4 image = np.arange(shape[0] * shape[1]).reshape(shape).astype(cfg.PRECISION.np_float) x_0 = 1 y_0 = 2 width = 3 height = 4 res = ip.crop(image, (y_0, x_0, height, width)).get() np.testing.assert_equal(image[y_0 : y_0 + height, x_0 : x_0 + width], res) # Identity np.testing.assert_equal(image, ip.crop(image, (0, 0, shape[0], shape[1])).get())
def test_crop(self): shape = 8, 4 image = np.arange(shape[0] * shape[1]).reshape(shape).astype(cfg.PRECISION.np_float) x_0 = 1 y_0 = 2 width = 3 height = 4 res = ip.crop(image, (y_0, x_0, height, width)).get() np.testing.assert_equal(image[y_0:y_0 + height, x_0:x_0 + width], res) # Identity np.testing.assert_equal(image, ip.crop(image, (0, 0, shape[0], shape[1])).get())
def _project(self, shape, pixel_size, offset, t=None, queue=None, out=None, block=False): """Project thickness.""" orig_shape = self.thickness.shape orig_region = (0, 0) + orig_shape end = ((offset + shape * pixel_size) / self.pixel_size).simplified.magnitude end = np.round(end).astype(np.int) start = np.round((offset / self.pixel_size).simplified.magnitude).astype(np.int) # numpy integers are not understood by pyopencl's rectangle copy end = [int(num) for num in end] start = [int(num) for num in start] cy, cx = (max(0, start[0]), max(0, start[1])) crop_region = (cy, cx, min(end[0], orig_shape[0]) - cy, min(end[1], orig_shape[1]) - cx) py, px = (abs(min(0, start[0])), abs(min(0, start[1]))) pad_region = (py, px, end[0] - start[0], end[1] - start[1]) proj = self.thickness if crop_region != orig_region: proj = crop(self.thickness, crop_region, block=block) if pad_region != (0, 0) + crop_region[2:]: proj = pad(proj, pad_region, block=block) if proj.shape != shape: proj = rescale(proj, shape, block=block) return proj
def crop_to_aperture(image, w, ps): """Crop *image* to 2x aperture width.""" n = image.shape[0] fov = image.shape * ps wp = int(np.round(w / ps).simplified.magnitude) fmt = 'Cropping image with shape {} and FOV {} to {} and FOV {}' LOG.info(fmt.format(image.shape[::-1], fov[::-1], 2 * wp, 2 * w)) # Crop to 4w region = (n / 2 - 2 * wp, n / 2 - 2 * wp, 4 * wp, 4 * wp) return crop(image, region).get()