Пример #1
0
 def test_median_stack(self):
     stack = Stack('median', 3)
     self.assertEqual(stack.mode, 'median')
     self.assertEqual(stack.num, 3)
     self.assertEqual(stack._num, 0)
     stack._update_stack(self.img1)
     self.assertEqual(stack._num, 1)
     stack._update_stack(self.img2)
     self.assertEqual(stack._num, 2)
     stack._update_stack(self.img3)
     self.assertEqual(stack._num, 3)
     self.assertItemsEqual(stack.calculate().img, np.ones((3, 3, 3),
                                                          dtype=np.uint8))
     result = stack.calculate().img
     self.assertItemsEqual(result, np.ones((3, 3, 3), dtype=np.uint16))
Пример #2
0
    def setUp(self):
        self.median = Stack('median', 3)

        self.img1 = Image(img=np.zeros((3, 3, 3), dtype=np.float))
        self.img2 = Image(img=np.zeros((3, 3, 3), dtype=np.float)+1)
        self.img3 = Image(img=np.zeros((3, 3, 3), dtype=np.float)+2)
        self.img4 = Image(img=np.zeros((3, 3, 3), dtype=np.float)+7)
Пример #3
0
    def test_min_stack(self):
        stack = Stack('min', 2)
        self.assertEqual(stack.mode, 'min')
        self.assertEqual(stack.num, 2)
        self.assertEqual(stack._num, 0)

        stack._update_stack(self.img1)
        correct_result = np.zeros((3, 3, 3), dtype=np.uint8)
        self.assertItemsEqual(stack.stack.img, correct_result)

        stack._update_stack(self.img2)
        self.assertItemsEqual(stack.calculate().img, correct_result)
Пример #4
0
    def test_sigma_stack(self):
        stack = Stack('sigma', 9, kwargs={'kappa': 2, 'max_iters': 1})
        self.assertEqual(stack.mode, 'sigma')
        self.assertEqual(stack.num, 9)
        self.assertEqual(stack._num, 0)
        for i in range(3):
            stack._update_stack(self.img2)
            stack._update_stack(self.img3)
            stack._update_stack(self.img4)

        result = stack.calculate().img
        self.assertItemsEqual(result, 1./3. + 3 * np.ones((3, 3, 3),
                                                          dtype=np.uint16))
Пример #5
0
 def test_mean_stack(self):
     stack = Stack('mean', 3)
     self.assertEqual(stack.mode, 'mean')
     self.assertEqual(stack.num, 3)
     self.assertEqual(stack._num, 0)
     stack._update_stack(self.img1)
     self.assertEqual(stack._num, 1)
     stack._update_stack(self.img2)
     self.assertEqual(stack._num, 2)
     stack._update_stack(self.img3)
     self.assertEqual(stack._num, 3)
     self.assertItemsEqual(stack.stack.img, 3*np.ones((3, 3, 3),
                                                      dtype=np.float))
     stack.stack.img[1, 1, 1] = 13
     stack.stack.img[2, 2, 1] = 23
     stack.stack.img = _scale(stack.stack.img, 16)
     result = stack.stack.img
     self.assertTrue(result.min() == 0)
     self.assertTrue(result[1, 1, 1] == 32767)
     self.assertTrue(result.max() == 65535)
Пример #6
0
def halostack_cli(args):
    '''Commandline interface.'''

    images = args['fname_in']

    stacks = []
    for stack in args['stacks']:
        stacks.append(Stack(stack, len(images), nprocs=args['nprocs']))

    base_img_fname = images[0]
    base_img = Image(fname=base_img_fname, nprocs=args['nprocs'])
    LOGGER.debug("Using %s as base image.", base_img.fname)
    images.remove(images[0])

    if not args['no_alignment'] and len(images) > 1:
        view_img = base_img.luminance()
        if isinstance(args['view_gamma'], float):
            view_img.enhance({'gamma': args['view_gamma']})
        print "\nClick tight area (two opposite corners) for "\
            "reference location.\n"
        args['focus_reference'] = get_two_points(view_img)
        LOGGER.debug("Reference area: (%d, %d) with radius %d.",
                     args['focus_reference'][0], args['focus_reference'][1],
                     args['focus_reference'][2])
        print "Click two corner points for the area where alignment "\
            "reference will be in every image.\n"
        args['focus_area'] = get_two_points(view_img)

        LOGGER.debug("User-selected search area: (%d, %d) with radius %d.",
                     args['focus_area'][0], args['focus_area'][1],
                     args['focus_area'][2])
        del view_img

    if not args['no_alignment'] and len(images) > 1:
        LOGGER.debug("Initializing alignment.")
        aligner = Align(base_img,
                        cor_th=args['correlation_threshold'],
                        nprocs=args['nprocs'])
        aligner.set_reference(args['focus_reference'])
        aligner.set_search_area(args['focus_area'])
        LOGGER.debug("Alignment initialized.")

    if args['save_prefix'] is not None:
        fname = intermediate_fname(args['save_prefix'], base_img_fname)
        base_img.save(fname)

    if len(args['enhance_images']) > 0:
        LOGGER.info("Preprocessing image.")
        base_img.enhance(args['enhance_images'])

    for stack in stacks:
        stack.add_image(base_img)

    # memory management
    del base_img

    for img_fname in images:
        # Read image
        img = Image(fname=img_fname, nprocs=args['nprocs'])

        if not args['no_alignment'] and len(images) > 1:
            # align image
            img = aligner.align(img)

        if img is None:
            LOGGER.warning("Skipping image.")
            continue

        if args['save_prefix'] is not None:
            fname = intermediate_fname(args['save_prefix'], img_fname)
            img.save(fname)

        if len(args['enhance_images']) > 0:
            LOGGER.info("Preprocessing image.")
            img.enhance(args['enhance_images'])

        for stack in stacks:
            stack.add_image(img)

    for i in range(len(stacks)):
        img = stacks[i].calculate()
        img.save(args['stack_fnames'][i], enhancements=args['enhance_stacks'])