예제 #1
0
 def test_11_06_select_overlay(self):
     display_svc = ij2.get_display_service(self.context)
     overlay_svc = ij2.get_overlay_service(self.context)
     i, j = np.mgrid[0:15, 0:1900:100]
     image = i+j
     ds = ij2.create_dataset(self.context, image, "Foo")
     display = display_svc.createDisplay("Foo", ds)
     d2 = display_svc.createDisplay("Bar", ij2.create_dataset(self.context, image, "Bar"))
     mask = np.zeros(i.shape, bool)
     islice = slice(2,-3)
     jslice = slice(3,-4)
     mask[islice, jslice] = True
     overlay = ij2.create_overlay(self.context, mask)
     overlay_svc.addOverlays(display, J.make_list([overlay]))
     ij2.select_overlay(display.o, overlay)
예제 #2
0
 def test_11_07_get_selection_bounds(self):
     display_svc = ij2.get_display_service(self.context)
     overlay_svc = ij2.get_overlay_service(self.context)
     i, j = np.mgrid[0:15, 0:1900:100]
     image = i+j
     ds = ij2.create_dataset(self.context, image, "Foo")
     display = display_svc.createDisplay("Foo", ds)
     mask = np.zeros(i.shape, bool)
     islice = slice(2,-3)
     jslice = slice(3,-4)
     mask[islice, jslice] = True
     overlay = ij2.create_overlay(self.context, mask)
     overlay_svc.addOverlays(display, J.make_list([overlay]))
     ij2.select_overlay(display.o, overlay)
     rect = overlay_svc.getSelectionBounds(display)
     self.assertEqual(J.get_field(rect, "x", "D"), 3)
     self.assertEqual(J.get_field(rect, "y", "D"), 2)
     self.assertEqual(J.get_field(rect, "width", "D"), 11)
     self.assertEqual(J.get_field(rect, "height", "D"), 9)
예제 #3
0
 def test_09_09_check_overlay(self):
     svc = ij2.get_display_service(self.context)
     r = np.random.RandomState()
     i, j = np.mgrid[0:11, 0:1300:100]
     image = i+j
     ds = ij2.create_dataset(self.context, image, "Foo")
     display = svc.createDisplay("Foo", ds)
     mask = np.zeros(image.shape, bool)
     mask[2:-1, 3:-4] = 1
     overlay = ij2.create_overlay(self.context, mask)
     ij2.get_overlay_service(self.context).addOverlays(
         display.o, J.make_list([overlay]))
     ij2.select_overlay(display.o, overlay)
     self.run_command("imagej.core.commands.imglib.CropImage",
                      dict(display=display), {})
     dataset = ij2.wrap_dataset(display.getActiveView().getData())
     image_out = dataset.get_pixel_data()
     self.assertSequenceEqual(image_out.shape, [7, 5])
     np.testing.assert_array_equal(image[2:-2, 3:-5], image_out)
예제 #4
0
 def test_06_01_get_mask_data(self):
     # Get the overlay data from a display
     #
     display_svc = ij2.get_display_service(self.context)
     overlay_svc = ij2.get_overlay_service(self.context)
     image = np.zeros((30, 30))
     ds = ij2.create_dataset(self.context, image, "Foo")
     display = display_svc.createDisplay("Foo", ds)
     d2 = display_svc.createDisplay("Bar", ij2.create_dataset(self.context, image, "Bar"))
     overlay = J.run_script(
         """var o = new Packages.imagej.data.overlay.RectangleOverlay(context.getContext());
            o.setOrigin(5, 0);
            o.setOrigin(3, 1);
            o.setExtent(6, 0);
            o.setExtent(7, 1);
            o;""", dict(context=self.context))
     overlay_svc.addOverlays(display, J.make_list([overlay]))
     ij2.select_overlay(display.o, overlay)
     mask = ij2.create_mask(display)
     i, j = np.mgrid[0:mask.shape[0], 0:mask.shape[1]]
     np.testing.assert_equal(mask, (j >= 5) & (j < 11) & (i >= 3) & (i < 10))
예제 #5
0
    def execute_advanced_command(self, workspace, command, d):
        '''Execute an advanced command

        command - name of the command
        d - dictionary to be used to find settings
        '''
        context = get_context()
        self.get_command_settings(command, d)
        wants_display = self.show_window
        if wants_display:
            workspace.display_data.input_images = input_images = []
            workspace.display_data.output_images = output_images = []
        key = command.get_unicode_value()
        node = command.get_selected_leaf()
        module_info = node[2]
        
        input_dictionary = J.get_map_wrapper(
            J.make_instance('java/util/HashMap', "()V"))
        display_dictionary = {}
        display_service = ij2.get_display_service(context)
        for setting, module_item in d[key]:
            if isinstance(setting, cps.ImageNameProvider):
                continue
            field_name = module_item.getName()
            field_type = module_item.getType()
            raw_type = J.call(module_item.o, "getType", "()Ljava/lang/Class;")
            if field_type in (ij2.FT_BOOL, ij2.FT_INTEGER, ij2.FT_FLOAT,
                              ij2.FT_STRING):
                input_dictionary.put(field_name, J.box(setting.value, raw_type))
            elif field_type == ij2.FT_COLOR:
                assert isinstance(setting, cps.Color)
                red, green, blue = setting.to_rgb()
                jobject = J.make_instance(
                    "imagej/util/ColorRGB", "(III)V", red, green, blue)
                input_dictionary.put(field_name, jobject)
            elif field_type == ij2.FT_IMAGE:
                image_name = setting.value
                image = workspace.image_set.get_image(image_name)
                pixel_data = image.pixel_data * IMAGEJ_SCALE
                
                dataset = ij2.create_dataset(
                    context, pixel_data, image_name)
                display = display_service.createDisplay(image_name, dataset)
                display_dictionary[module_item.getName()] = display 
                if image.has_mask:
                    #overlay_name = "X" + uuid.uuid4().get_hex()
                    #image_dictionary[overlay_name] = image.mask
                    overlay = ij2.create_overlay(context, image.mask)
                    overlay_service = ij2.get_overlay_service(context)
                    overlay_service.addOverlays(
                        display.o, J.make_list([overlay]))
                    ij2.select_overlay(display.o, overlay)
                input_dictionary.put(field_name, display.o)
                if wants_display:
                    input_images.append((image_name, image.pixel_data))
            elif field_type == ij2.FT_TABLE:
                table_name = setting.value
                table = workspace.object_set.get_type_instance(
                    IJ_TABLE_TYPE, table_name)
                input_dictionary.put(field_name, table)
            elif field_type == ij2.FT_FILE:
                jfile = J.make_instance(
                    "java/io/File", "(Ljava/lang/String;)V", setting.value)
                input_dictionary.put(field_name, jfile)
        command_service = ij2.get_command_service(get_context())
        future = command_service.run(module_info.o, input_dictionary.o)
        module = future.get()
        for setting, module_item in d[key]:
            if isinstance(setting, cps.ImageNameProvider):
                name = module_item.getName()
                output_name = setting.value
                if display_dictionary.has_key(name):
                    display = display_dictionary[name]
                else:
                    display = IJ2.wrap_display(module.getOutput(name))
                pixel_data = self.save_display_as_image(
                    workspace, display, output_name)
                
                if wants_display:
                    output_images.append((output_name, pixel_data))
        # Close any displays that we created.
        for display in display_dictionary.values():
            display.close()
예제 #6
0
    def execute_advanced_command(self, workspace, command, d):
        '''Execute an advanced command

        command - name of the command
        d - dictionary to be used to find settings
        '''
        context = get_context()
        self.get_command_settings(command, d)
        wants_display = self.show_window
        if wants_display:
            workspace.display_data.input_images = input_images = []
            workspace.display_data.output_images = output_images = []
        key = command.get_unicode_value()
        node = command.get_selected_leaf()
        module_info = node[2]

        input_dictionary = J.get_map_wrapper(
            J.make_instance('java/util/HashMap', "()V"))
        display_dictionary = {}
        display_service = ij2.get_display_service(context)
        for setting, module_item in d[key]:
            if isinstance(setting, cps.ImageNameProvider):
                continue
            field_name = module_item.getName()
            field_type = module_item.getType()
            raw_type = J.call(module_item.o, "getType", "()Ljava/lang/Class;")
            if field_type in (ij2.FT_BOOL, ij2.FT_INTEGER, ij2.FT_FLOAT,
                              ij2.FT_STRING):
                input_dictionary.put(field_name, J.box(setting.value,
                                                       raw_type))
            elif field_type == ij2.FT_COLOR:
                assert isinstance(setting, cps.Color)
                red, green, blue = setting.to_rgb()
                jobject = J.make_instance("imagej/util/ColorRGB", "(III)V",
                                          red, green, blue)
                input_dictionary.put(field_name, jobject)
            elif field_type == ij2.FT_IMAGE:
                image_name = setting.value
                image = workspace.image_set.get_image(image_name)
                pixel_data = image.pixel_data * IMAGEJ_SCALE

                dataset = ij2.create_dataset(context, pixel_data, image_name)
                display = display_service.createDisplay(image_name, dataset)
                display_dictionary[module_item.getName()] = display
                if image.has_mask:
                    #overlay_name = "X" + uuid.uuid4().get_hex()
                    #image_dictionary[overlay_name] = image.mask
                    overlay = ij2.create_overlay(context, image.mask)
                    overlay_service = ij2.get_overlay_service(context)
                    overlay_service.addOverlays(display.o,
                                                J.make_list([overlay]))
                    ij2.select_overlay(display.o, overlay)
                input_dictionary.put(field_name, display.o)
                if wants_display:
                    input_images.append((image_name, image.pixel_data))
            elif field_type == ij2.FT_TABLE:
                table_name = setting.value
                table = workspace.object_set.get_type_instance(
                    IJ_TABLE_TYPE, table_name)
                input_dictionary.put(field_name, table)
            elif field_type == ij2.FT_FILE:
                jfile = J.make_instance("java/io/File",
                                        "(Ljava/lang/String;)V", setting.value)
                input_dictionary.put(field_name, jfile)
        command_service = ij2.get_command_service(get_context())
        future = command_service.run(module_info.o, input_dictionary.o)
        module = future.get()
        for setting, module_item in d[key]:
            if isinstance(setting, cps.ImageNameProvider):
                name = module_item.getName()
                output_name = setting.value
                if display_dictionary.has_key(name):
                    display = display_dictionary[name]
                else:
                    display = IJ2.wrap_display(module.getOutput(name))
                pixel_data = self.save_display_as_image(
                    workspace, display, output_name)

                if wants_display:
                    output_images.append((output_name, pixel_data))
        # Close any displays that we created.
        for display in display_dictionary.values():
            display.close()