Exemple #1
0
    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
Exemple #2
0
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()
Exemple #3
0
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()
Exemple #4
0
    def test_rescale_down(self):
        # Use spline and not zoom or imresize because they don't behave exactly as we define
        n = 18
        square = np.zeros((n, n), dtype=np.float32)
        square[4:-4, 4:-4] = 1

        # Same
        res = ip.rescale(square, (n, n)).get()
        np.testing.assert_almost_equal(res, square)

        # Various odd/even combinations in the x, y directions
        for (ss_y, ss_x) in itertools.product((1.0 / 2, 1.0 / 3), (1.0 / 2, 1.0 / 3)):
            hd_n = int(ss_x * n)
            hd_m = int(ss_y * n)
            res = ip.rescale(square, (hd_m, hd_n)).get()
            gt = rescale_scipy(square, (ss_y, ss_x))
            np.testing.assert_almost_equal(res, gt, decimal=2)
Exemple #5
0
    def test_rescale_up(self):
        # Use spline and not zoom or imresize because they don't behave exactly as we define
        n = 8
        square = np.zeros((n, n), dtype=np.float32)
        square[2:-2, 2:-2] = 1

        # Same
        res = ip.rescale(square, (n, n)).get()
        np.testing.assert_almost_equal(res, square)

        # Various odd/even combinations in the x, y directions
        for (ss_y, ss_x) in itertools.product((2, 3), (2, 3)):
            hd_n = ss_x * n
            hd_m = ss_y * n
            res = ip.rescale(square, (hd_m, hd_n)).get()
            gt = rescale_scipy(square, (ss_y, ss_x)).astype(cfg.PRECISION.np_float)
            np.testing.assert_almost_equal(res, gt, decimal=2)
Exemple #6
0
 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)
Exemple #7
0
 def compare_sampling(gauss, factor):
     shape = (int(factor * self.n), ) * 2
     im = ip.compute_intensity(
         source.transfer(shape,
                         self.ps / factor,
                         self.energies[0],
                         check=False)).get()
     self.assertAlmostEqual(im.sum(), gauss.sum(), places=3)
     hd = ip.rescale(gauss, shape).get() / factor**2
     np.testing.assert_almost_equal(im, hd, decimal=5)
Exemple #8
0
    def test_rescale(self):
        orig_shape = 8, 4
        shape = 4, 8
        image = (np.arange(orig_shape[0] *
                           orig_shape[1]).reshape(orig_shape).astype(
                               cfg.PRECISION.np_float))
        res = ip.rescale(image, shape).get()
        gt = rescale_scipy(image, (0.5, 2))

        np.testing.assert_almost_equal(res, gt)

        cfg.PRECISION.set_precision(True)
        image = image.astype(cfg.PRECISION.np_float)
        self.assertRaises(TypeError, ip.rescale, image, shape)
    def test_rescale(self):
        from scipy.interpolate import RectBivariateSpline
        orig_shape = 8, 4
        shape = 4, 8
        image = np.arange(orig_shape[0] *
                          orig_shape[1]).reshape(orig_shape).astype(cfg.PRECISION.np_float)
        res = ip.rescale(image, shape).get()
        xx = np.linspace(0, orig_shape[1], shape[1], endpoint=False)
        yy = np.linspace(0, orig_shape[0], shape[0], endpoint=False)
        spl = RectBivariateSpline(range(orig_shape[0]), range(orig_shape[1]), image, kx=1, ky=1)

        np.testing.assert_almost_equal(res, spl(yy, xx))

        cfg.PRECISION.set_precision(True)
        image = image.astype(cfg.PRECISION.np_float)
        self.assertRaises(TypeError,ip.rescale, image, shape)
Exemple #10
0
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)