示例#1
0
def only_display_image(figure, shape):
    """Set up a figure so that the image occupies the entire figure

    figure - a matplotlib figure
    shape - i/j size of the image being displayed
    """
    assert isinstance(figure, matplotlib.figure.Figure)
    figure.set_frameon(False)
    ax = figure.axes[0]
    ax.set_axis_off()
    figure.subplots_adjust(0, 0, 1, 1, 0, 0)
    dpi = figure.dpi
    width = float(shape[1]) / dpi
    height = float(shape[0]) / dpi
    figure.set_figheight(height)
    figure.set_figwidth(width)
    bbox = matplotlib.transforms.Bbox(numpy.array([[0.0, 0.0], [width, height]]))
    transform = matplotlib.transforms.Affine2D(
        numpy.array([[dpi, 0, 0], [0, dpi, 0], [0, 0, 1]])
    )
    figure.bbox = matplotlib.transforms.TransformedBbox(bbox, transform)
示例#2
0
    def run(self, workspace):
        """Run the module

        workspace    - The workspace contains
            pipeline     - instance of cpp for this run
            image_set    - the images in the image set being processed
            object_set   - the objects (labeled masks) in this image set
            measurements - the measurements for this run
            frame        - the parent frame to whatever frame is created. None means don't draw.
        """
        background_image = self.get_background_image(workspace, None)

        if (self.each_or_once == EO_ONCE
                and self.get_good_gridding(workspace) is not None):
            gridding = self.get_good_gridding(workspace)
        if self.auto_or_manual == AM_AUTOMATIC:
            gridding = self.run_automatic(workspace)
        elif self.manual_choice == MAN_COORDINATES:
            gridding = self.run_coordinates(workspace)
        elif self.manual_choice == MAN_MOUSE:
            gridding = workspace.interaction_request(
                self, background_image,
                workspace.measurements.image_set_number)
        self.set_good_gridding(workspace, gridding)
        workspace.set_grid(self.grid_image.value, gridding)
        #
        # Save measurements
        #
        self.add_measurement(
            workspace,
            F_X_LOCATION_OF_LOWEST_X_SPOT,
            gridding.x_location_of_lowest_x_spot,
        )
        self.add_measurement(
            workspace,
            F_Y_LOCATION_OF_LOWEST_Y_SPOT,
            gridding.y_location_of_lowest_y_spot,
        )
        self.add_measurement(workspace, F_ROWS, gridding.rows)
        self.add_measurement(workspace, F_COLUMNS, gridding.columns)
        self.add_measurement(workspace, F_X_SPACING, gridding.x_spacing)
        self.add_measurement(workspace, F_Y_SPACING, gridding.y_spacing)

        # update background image
        background_image = self.get_background_image(workspace, gridding)

        workspace.display_data.gridding = gridding.serialize()
        workspace.display_data.background_image = background_image
        workspace.display_data.image_set_number = (
            workspace.measurements.image_set_number)

        if self.wants_image:
            import matplotlib.transforms
            import matplotlib.figure
            import matplotlib.backends.backend_agg
            from cellprofiler.gui.tools import figure_to_image

            figure = matplotlib.figure.Figure()
            canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(figure)
            ax = figure.add_subplot(1, 1, 1)
            self.display_grid(background_image, gridding,
                              workspace.measurements.image_set_number, ax)
            #
            # This is the recipe for just showing the axis
            #
            figure.set_frameon(False)
            ax.set_axis_off()
            figure.subplots_adjust(0, 0, 1, 1, 0, 0)
            ai = ax.images[0]
            shape = ai.get_size()
            dpi = figure.dpi
            width = float(shape[1]) / dpi
            height = float(shape[0]) / dpi
            figure.set_figheight(height)
            figure.set_figwidth(width)
            bbox = matplotlib.transforms.Bbox(
                np.array([[0.0, 0.0], [width, height]]))
            transform = matplotlib.transforms.Affine2D(
                np.array([[dpi, 0, 0], [0, dpi, 0], [0, 0, 1]]))
            figure.bbox = matplotlib.transforms.TransformedBbox(
                bbox, transform)
            image_pixels = figure_to_image(figure, dpi=dpi)
            image = cpi.Image(image_pixels)

            workspace.image_set.add(self.save_image_name.value, image)
示例#3
0
    def run(self, workspace):
        """Run the module

        workspace    - The workspace contains
            pipeline     - instance of cpp for this run
            image_set    - the images in the image set being processed
            object_set   - the objects (labeled masks) in this image set
            measurements - the measurements for this run
            frame        - the parent frame to whatever frame is created. None means don't draw.
        """
        background_image = self.get_background_image(workspace, None)

        if (self.each_or_once == EO_ONCE and
                    self.get_good_gridding(workspace) is not None):
            gridding = self.get_good_gridding(workspace)
        if self.auto_or_manual == AM_AUTOMATIC:
            gridding = self.run_automatic(workspace)
        elif self.manual_choice == MAN_COORDINATES:
            gridding = self.run_coordinates(workspace)
        elif self.manual_choice == MAN_MOUSE:
            gridding = workspace.interaction_request(self, background_image,
                                                     workspace.measurements.image_set_number)
        self.set_good_gridding(workspace, gridding)
        workspace.set_grid(self.grid_image.value, gridding)
        #
        # Save measurements
        #
        self.add_measurement(workspace, F_X_LOCATION_OF_LOWEST_X_SPOT,
                             gridding.x_location_of_lowest_x_spot)
        self.add_measurement(workspace, F_Y_LOCATION_OF_LOWEST_Y_SPOT,
                             gridding.y_location_of_lowest_y_spot)
        self.add_measurement(workspace, F_ROWS, gridding.rows)
        self.add_measurement(workspace, F_COLUMNS, gridding.columns)
        self.add_measurement(workspace, F_X_SPACING, gridding.x_spacing)
        self.add_measurement(workspace, F_Y_SPACING, gridding.y_spacing)

        # update background image
        background_image = self.get_background_image(workspace, gridding)

        workspace.display_data.gridding = gridding.serialize()
        workspace.display_data.background_image = background_image
        workspace.display_data.image_set_number = workspace.measurements.image_set_number

        if self.wants_image:
            import matplotlib.transforms
            import matplotlib.figure
            import matplotlib.backends.backend_agg
            from cellprofiler.gui.tools import figure_to_image
            figure = matplotlib.figure.Figure()
            canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(figure)
            ax = figure.add_subplot(1, 1, 1)
            self.display_grid(background_image, gridding,
                              workspace.measurements.image_set_number, ax)
            #
            # This is the recipe for just showing the axis
            #
            figure.set_frameon(False)
            ax.set_axis_off()
            figure.subplots_adjust(0, 0, 1, 1, 0, 0)
            ai = ax.images[0]
            shape = ai.get_size()
            dpi = figure.dpi
            width = float(shape[1]) / dpi
            height = float(shape[0]) / dpi
            figure.set_figheight(height)
            figure.set_figwidth(width)
            bbox = matplotlib.transforms.Bbox(
                    np.array([[0.0, 0.0], [width, height]]))
            transform = matplotlib.transforms.Affine2D(
                    np.array([[dpi, 0, 0],
                              [0, dpi, 0],
                              [0, 0, 1]]))
            figure.bbox = matplotlib.transforms.TransformedBbox(bbox, transform)
            image_pixels = figure_to_image(figure, dpi=dpi)
            image = cpi.Image(image_pixels)

            workspace.image_set.add(self.save_image_name.value, image)