Esempio n. 1
0
 def inject_image(self, pixels, name=''):
     '''inject an image into ImageJ for processing'''
     ij_processor = ijiproc.make_image_processor((pixels * 255.0).astype('float32'))
     image_plus = ijip.make_imageplus_from_processor(name, ij_processor)
     if sys.platform == "darwin":
         ijwm.set_temp_current_image(image_plus)
     else:
         ijwm.set_current_image(image_plus)
Esempio n. 2
0
    def run(self, workspace):
        """Run the imageJ command"""
        image_set = workspace.image_set
        d = self.get_dictionary(workspace.image_set_list)
        if self.wants_to_set_current_image:
            input_image_name = self.current_input_image_name.value
            img = image_set.get_image(input_image_name, must_be_grayscale=True)
            if self.show_window:
                workspace.display_data.image_sent_to_ij = img.pixel_data
        else:
            img = None
        display_service = ij2.get_display_service(get_context())
        #
        # Run a command or macro on the first image of the set
        #
        if d.get(D_FIRST_IMAGE_SET) == image_set.image_number:
            self.do_imagej(workspace, D_FIRST_IMAGE_SET)
        ij1_mode = self.command_or_macro == CM_MACRO
        #
        # Install the input image as the current image
        #
        if img is not None:
            ijpixels = img.pixel_data * IMAGEJ_SCALE
            if ij1_mode:
                ij_processor = ijiproc.make_image_processor(ijpixels.astype("float32"))
                image_plus = ijip.make_imageplus_from_processor(input_image_name, ij_processor)
                ijwm.set_current_image(image_plus)
            else:
                dataset = ij2.create_dataset(get_context(), ijpixels, input_image_name)
                display = display_service.createDisplay(input_image_name, dataset)
                display_service.setActiveDisplay(display)

        self.do_imagej(workspace)
        #
        # Get the output image
        #
        if self.wants_to_get_current_image:
            output_image_name = self.current_output_image_name.value
            if ij1_mode:
                image_plus = ijwm.get_current_image()
                ij_processor = image_plus.getProcessor()
                pixels = ijiproc.get_image(ij_processor).astype("float32") / IMAGEJ_SCALE
                image = cpi.Image(pixels)
                workspace.image_set.add(output_image_name, image)
            else:
                for attempt in range(4):
                    display = display_service.getActiveImageDisplay()
                    if display.o is not None:
                        break
                    #
                    # Possible synchronization problem with ImageJ 1.0
                    # Possible error involving user changing window focus
                    #
                    import time

                    time.sleep(0.25)
                else:
                    raise ValueError("Failed to retrieve active display")
                pixels = self.save_display_as_image(workspace, display, output_image_name)
            if self.show_window:
                workspace.display_data.image_acquired_from_ij = pixels
        #
        # Execute the post-group macro or command
        #
        if d.get(D_LAST_IMAGE_SET) == image_set.image_number:
            self.do_imagej(workspace, D_LAST_IMAGE_SET)
            #
            # Save the current ImageJ image after executing the post-group
            # command or macro
            #
            if self.post_group_choice != CM_NOTHING and self.wants_post_group_image:
                output_image_name = self.post_group_output_image.value
                if ij1_mode:
                    image_plus = ijwm.get_current_image()
                    ij_processor = image_plus.getProcessor()
                    pixels = ijiproc.get_image(ij_processor).astype("float32") / IMAGEJ_SCALE
                    image = cpi.Image(pixels, mask=mask)
                    workspace.image_set.add(output_image_name, image)
                else:
                    display = display_service.getActiveImageDisplay()
                    self.save_display_as_image(workspace, display, output_image_name)
Esempio n. 3
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
        '''
        wants_display = workspace.frame is not None
        if wants_display:
            workspace.display_data.input_images = input_images = []
            workspace.display_data.output_images = output_images = []
        key = command.get_unicode_value()
        if bioformats.USE_IJ2:
            node = command.get_selected_leaf()
            module_info = node[2]
            module = IJ2.wrap_module(module_info.createModule())
            context = self.get_context()
            display_service = IJ2.get_display_service(context)
                
            display_dictionary = {}
            for setting, module_item in d[key]:
                field_type = module_item.getType()
                if isinstance(setting, cps.ImageNameProvider):
                    continue
                if field_type == IJ2.FT_BOOL:
                    value = J.make_instance("java/lang/Boolean",
                                            "(Z)V", setting.value)
                elif field_type == IJ2.FT_INTEGER:
                    value = J.make_instance("java/lang/Integer",
                                            "(I)V", setting.value)
                elif field_type == IJ2.FT_FLOAT:
                    value = J.make_instance("java/lang/Double",
                                            "(D)V", setting.value)
                elif field_type == IJ2.FT_STRING:
                    value = setting.value
                elif field_type == IJ2.FT_COLOR:
                    value = IJ2.make_color_rgb_from_html(setting.value)
                elif field_type == IJ2.FT_IMAGE:
                    image_name = setting.value
                    image = workspace.image_set.get_image(image_name)
                    dataset = IJ2.create_dataset(image.pixel_data,
                                                 setting.value)
                    display = display_service.createDisplay(dataset)
                    if image.has_mask:
                        overlay = IJ2.create_overlay(image.mask)
                        display.displayOverlay(overlay)
                    value = display
                    display_dictionary[module_item.getName()] = display
                    if wants_display:
                        input_images.append((image_name, image.pixel_data))
                module.setInput(module_item.getName(), value)
            module_service = IJ2.get_module_service(context)
            module_service.run(module)
            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))
                    ds = display_service.getActiveDataset(display)
                    pixel_data = ds.get_pixel_data()
                    image = cpi.Image(pixel_data)
                    workspace.image_set.add(output_name, image)
                    if wants_display:
                        output_images.append((output_name, pixel_data))
            for display in display_dictionary.values():
                panel = IJ2.wrap_display_panel(display.getDisplayPanel())
                panel.close()
        else:
            from imagej.imageplus import make_imageplus_from_processor
            from imagej.imageplus import get_imageplus_wrapper
            from imagej.imageprocessor import make_image_processor
            from imagej.imageprocessor import get_image
            
            command = command.value
            settings = d[command]
            classname = self.get_cached_commands()[command]
            plugin = M.get_plugin(classname)
            fp_in = P.get_input_fields_and_parameters(plugin)
            result = []
            image_set = workspace.image_set
            assert isinstance(image_set, cpi.ImageSet)
            for (field, parameter), setting in zip(fp_in, settings[:len(fp_in)]):
                field_type = P.get_field_type(field)
                label = parameter.label() or ""
                if field_type == P.FT_IMAGE:
                    image_name = setting.value
                    image = workspace.image_set.get_image(image_name,
                                                          must_be_grayscale = True)
                    pixel_data = (image.pixel_data * 255.0).astype(np.float32)
                    if wants_display:
                        input_images.append((image_name, pixel_data / 255.0))
                    processor = make_image_processor(pixel_data)
                    image_plus = make_imageplus_from_processor(image_name, processor)
                    field.set(plugin, image_plus)
                    del image_plus
                    del processor
                elif field_type == P.FT_INTEGER:
                    field.setInt(plugin, setting.value)
                elif field_type == P.FT_FLOAT:
                    field.setFloat(plugin, setting.value)
                elif field_type == P.FT_BOOL:
                    field.setBoolean(plugin, setting.value)
                else:
                    field.set(plugin, setting.value)
            #
            # There are two ways to run this:
            # * Batch - just call plugin.run()
            # * Interactive - use PlugInFunctions.runInteractively
            #
            if self.pause_before_proceeding:
                J.execute_runnable_in_main_thread(J.run_script(
                    """new java.lang.Runnable() { run:function() {
                        importClass(Packages.imagej.plugin.PlugInFunctions);
                        PlugInFunctions.runInteractively(plugin);
                    }};""", dict(plugin=plugin)), True)
            else:
                J.execute_runnable_in_main_thread(plugin, True)
            setting_idx = len(fp_in)
            fp_out = P.get_output_fields_and_parameters(plugin)
            for field, parameter in fp_out:
                field_type = P.get_field_type(field)
                if field_type == P.FT_IMAGE:
                    image_name = settings[setting_idx].value
                    setting_idx += 1
                    image_plus = get_imageplus_wrapper(field.get(plugin))
                    processor = image_plus.getProcessor()
                    pixel_data = get_image(processor).astype(np.float32) / 255.0
                    if wants_display:
                        output_images.append((image_name, pixel_data))
                    image = cpi.Image(pixel_data)
                    image_set.add(image_name, image)
Esempio n. 4
0
    def run(self, workspace):
        '''Run the imageJ command'''
        image_set = workspace.image_set
        d = self.get_dictionary(workspace.image_set_list)
        if self.wants_to_set_current_image:
            input_image_name = self.current_input_image_name.value
            img = image_set.get_image(input_image_name, must_be_grayscale=True)
            if self.show_window:
                workspace.display_data.image_sent_to_ij = img.pixel_data
        else:
            img = None
        display_service = ij2.get_display_service(get_context())
        #
        # Run a command or macro on the first image of the set
        #
        if d.get(D_FIRST_IMAGE_SET) == image_set.image_number:
            self.do_imagej(workspace, D_FIRST_IMAGE_SET)
        ij1_mode = self.command_or_macro == CM_MACRO
        #
        # Install the input image as the current image
        #
        if img is not None:
            ijpixels = img.pixel_data * IMAGEJ_SCALE
            if ij1_mode:
                ij_processor = ijiproc.make_image_processor(
                    ijpixels.astype('float32'))
                image_plus = ijip.make_imageplus_from_processor(
                    input_image_name, ij_processor)
                ijwm.set_current_image(image_plus)
            else:
                dataset = ij2.create_dataset(get_context(), ijpixels,
                                             input_image_name)
                display = display_service.createDisplay(
                    input_image_name, dataset)
                display_service.setActiveDisplay(display)

        self.do_imagej(workspace)
        #
        # Get the output image
        #
        if self.wants_to_get_current_image:
            output_image_name = self.current_output_image_name.value
            if ij1_mode:
                image_plus = ijwm.get_current_image()
                ij_processor = image_plus.getProcessor()
                pixels = ijiproc.get_image(ij_processor).\
                    astype('float32') / IMAGEJ_SCALE
                image = cpi.Image(pixels)
                workspace.image_set.add(output_image_name, image)
            else:
                for attempt in range(4):
                    display = display_service.getActiveImageDisplay()
                    if display.o is not None:
                        break
                    #
                    # Possible synchronization problem with ImageJ 1.0
                    # Possible error involving user changing window focus
                    #
                    import time
                    time.sleep(.25)
                else:
                    raise ValueError("Failed to retrieve active display")
                pixels = self.save_display_as_image(workspace, display,
                                                    output_image_name)
            if self.show_window:
                workspace.display_data.image_acquired_from_ij = pixels
        #
        # Execute the post-group macro or command
        #
        if d.get(D_LAST_IMAGE_SET) == image_set.image_number:
            self.do_imagej(workspace, D_LAST_IMAGE_SET)
            #
            # Save the current ImageJ image after executing the post-group
            # command or macro
            #
            if (self.post_group_choice != CM_NOTHING
                    and self.wants_post_group_image):
                output_image_name = self.post_group_output_image.value
                if ij1_mode:
                    image_plus = ijwm.get_current_image()
                    ij_processor = image_plus.getProcessor()
                    pixels = ijiproc.get_image(ij_processor).\
                        astype('float32') / IMAGEJ_SCALE
                    image = cpi.Image(pixels, mask=mask)
                    workspace.image_set.add(output_image_name, image)
                else:
                    display = display_service.getActiveImageDisplay()
                    self.save_display_as_image(workspace, display,
                                               output_image_name)
Esempio n. 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
        '''
        from imagej.imageplus import make_imageplus_from_processor
        from imagej.imageplus import get_imageplus_wrapper
        from imagej.imageprocessor import make_image_processor
        from imagej.imageprocessor import get_image
        
        settings = d[command]
        classname = self.get_cached_commands()[command]
        plugin = M.get_plugin(classname)
        fp_in = P.get_input_fields_and_parameters(plugin)
        result = []
        image_set = workspace.image_set
        assert isinstance(image_set, cpi.ImageSet)
        wants_display = workspace.frame is not None
        if wants_display:
            workspace.display_data.input_images = input_images = []
            workspace.display_data.output_images = output_images = []
        for (field, parameter), setting in zip(fp_in, settings[:len(fp_in)]):
            field_type = P.get_field_type(field)
            label = parameter.label() or ""
            if field_type == P.FT_IMAGE:
                image_name = setting.value
                image = workspace.image_set.get_image(image_name,
                                                      must_be_grayscale = True)
                pixel_data = (image.pixel_data * 255.0).astype(np.float32)
                if wants_display:
                    input_images.append((image_name, pixel_data / 255.0))
                processor = make_image_processor(pixel_data)
                image_plus = make_imageplus_from_processor(image_name, processor)
                field.set(plugin, image_plus)
                del image_plus
                del processor
            elif field_type == P.FT_INTEGER:
                field.setInt(plugin, setting.value)
            elif field_type == P.FT_FLOAT:
                field.setFloat(plugin, setting.value)
            elif field_type == P.FT_BOOL:
                field.setBoolean(plugin, setting.value)
            else:
                field.set(plugin, setting.value)
        #
        # There are two ways to run this:
        # * Batch - just call plugin.run()
        # * Interactive - use PlugInFunctions.runInteractively
        #
        if self.pause_before_proceeding:
            J.static_call('imagej/plugin/PlugInFunctions', 'runInteractively',
                   '(Ljava/lang/Runnable)V', plugin)
        else:
            J.call(plugin, 'run', '()V')
        setting_idx = len(fp_in)
        fp_out = P.get_output_fields_and_parameters(plugin)
        for field, parameter in fp_out:
            field_type = P.get_field_type(field)
            if field_type == P.FT_IMAGE:
                image_name = settings[setting_idx].value
                setting_idx += 1
                image_plus = get_imageplus_wrapper(field.get(plugin))
                processor = image_plus.getProcessor()
                pixel_data = get_image(processor).astype(np.float32) / 255.0
                if wants_display:
                    output_images.append((image_name, pixel_data))
                image = cpi.Image(pixel_data)
                image_set.add(image_name, image)
Esempio n. 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
        '''
        wants_display = workspace.frame is not None
        if wants_display:
            workspace.display_data.input_images = input_images = []
            workspace.display_data.output_images = output_images = []
        key = command.get_unicode_value()
        if bioformats.USE_IJ2:
            node = command.get_selected_leaf()
            module_info = node[2]
            module = IJ2.wrap_module(module_info.createModule())
            context = self.get_context()
            display_service = IJ2.get_display_service(context)

            display_dictionary = {}
            for setting, module_item in d[key]:
                field_type = module_item.getType()
                if isinstance(setting, cps.ImageNameProvider):
                    continue
                if field_type == IJ2.FT_BOOL:
                    value = J.make_instance("java/lang/Boolean", "(Z)V",
                                            setting.value)
                elif field_type == IJ2.FT_INTEGER:
                    value = J.make_instance("java/lang/Integer", "(I)V",
                                            setting.value)
                elif field_type == IJ2.FT_FLOAT:
                    value = J.make_instance("java/lang/Double", "(D)V",
                                            setting.value)
                elif field_type == IJ2.FT_STRING:
                    value = setting.value
                elif field_type == IJ2.FT_COLOR:
                    value = IJ2.make_color_rgb_from_html(setting.value)
                elif field_type == IJ2.FT_IMAGE:
                    image_name = setting.value
                    image = workspace.image_set.get_image(image_name)
                    dataset = IJ2.create_dataset(image.pixel_data,
                                                 setting.value)
                    display = display_service.createDisplay(dataset)
                    if image.has_mask:
                        overlay = IJ2.create_overlay(image.mask)
                        display.displayOverlay(overlay)
                    value = display
                    display_dictionary[module_item.getName()] = display
                    if wants_display:
                        input_images.append((image_name, image.pixel_data))
                module.setInput(module_item.getName(), value)
            module_service = IJ2.get_module_service(context)
            module_service.run(module)
            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))
                    ds = display_service.getActiveDataset(display)
                    pixel_data = ds.get_pixel_data()
                    image = cpi.Image(pixel_data)
                    workspace.image_set.add(output_name, image)
                    if wants_display:
                        output_images.append((output_name, pixel_data))
            for display in display_dictionary.values():
                panel = IJ2.wrap_display_panel(display.getDisplayPanel())
                panel.close()
        else:
            from imagej.imageplus import make_imageplus_from_processor
            from imagej.imageplus import get_imageplus_wrapper
            from imagej.imageprocessor import make_image_processor
            from imagej.imageprocessor import get_image

            command = command.value
            settings = d[command]
            classname = self.get_cached_commands()[command]
            plugin = M.get_plugin(classname)
            fp_in = P.get_input_fields_and_parameters(plugin)
            result = []
            image_set = workspace.image_set
            assert isinstance(image_set, cpi.ImageSet)
            for (field, parameter), setting in zip(fp_in,
                                                   settings[:len(fp_in)]):
                field_type = P.get_field_type(field)
                label = parameter.label() or ""
                if field_type == P.FT_IMAGE:
                    image_name = setting.value
                    image = workspace.image_set.get_image(
                        image_name, must_be_grayscale=True)
                    pixel_data = (image.pixel_data * 255.0).astype(np.float32)
                    if wants_display:
                        input_images.append((image_name, pixel_data / 255.0))
                    processor = make_image_processor(pixel_data)
                    image_plus = make_imageplus_from_processor(
                        image_name, processor)
                    field.set(plugin, image_plus)
                    del image_plus
                    del processor
                elif field_type == P.FT_INTEGER:
                    field.setInt(plugin, setting.value)
                elif field_type == P.FT_FLOAT:
                    field.setFloat(plugin, setting.value)
                elif field_type == P.FT_BOOL:
                    field.setBoolean(plugin, setting.value)
                else:
                    field.set(plugin, setting.value)
            #
            # There are two ways to run this:
            # * Batch - just call plugin.run()
            # * Interactive - use PlugInFunctions.runInteractively
            #
            if self.pause_before_proceeding:
                J.static_call('imagej/plugin/PlugInFunctions',
                              'runInteractively', '(Ljava/lang/Runnable)V',
                              plugin)
            else:
                J.call(plugin, 'run', '()V')
            setting_idx = len(fp_in)
            fp_out = P.get_output_fields_and_parameters(plugin)
            for field, parameter in fp_out:
                field_type = P.get_field_type(field)
                if field_type == P.FT_IMAGE:
                    image_name = settings[setting_idx].value
                    setting_idx += 1
                    image_plus = get_imageplus_wrapper(field.get(plugin))
                    processor = image_plus.getProcessor()
                    pixel_data = get_image(processor).astype(
                        np.float32) / 255.0
                    if wants_display:
                        output_images.append((image_name, pixel_data))
                    image = cpi.Image(pixel_data)
                    image_set.add(image_name, image)