def run(self, workspace):
        import matplotlib
        import matplotlib.cm
        import matplotlib.backends.backend_wxagg
        import matplotlib.transforms
        from cellprofiler.gui.cpfigure import figure_to_image, only_display_image
        #
        # Get the image
        #
        image = workspace.image_set.get_image(self.image_name.value)
        workspace.display_data.pixel_data = image.pixel_data
        #
        # Get the measurements and positions
        #
        measurements = workspace.measurements
        if self.objects_or_image == OI_IMAGE:
            value = measurements.get_current_image_measurement(
                self.measurement.value)
            values = [value]
            x = [image.pixel_data.shape[1] / 2]
            y = [image.pixel_data.shape[0] / 2]
        else:
            values = measurements.get_current_measurement(
                self.objects_name.value,
                self.measurement.value)
            x = measurements.get_current_measurement(
                self.objects_name.value, M_LOCATION_CENTER_X)
            y = measurements.get_current_measurement(
                self.objects_name.value, M_LOCATION_CENTER_Y)
            mask = ~(np.isnan(values) | np.isnan(x) | np.isnan(y))
            values = values[mask]
            x = x[mask]
            y = y[mask]
        workspace.display_data.values = values
        workspace.display_data.x = x
        workspace.display_data.y = y
        fig = matplotlib.figure.Figure()
        axes = fig.add_subplot(1,1,1)
        def imshow_fn(pixel_data):
            # Note: requires typecast to avoid failure during
            #       figure_to_image (IMG-764)
            img = pixel_data * 255
            img[img < 0] = 0
            img[img > 255] = 255
            img = img.astype(np.uint8)
            axes.imshow(img, cmap = matplotlib.cm.Greys_r)
        self.display_on_figure(workspace, axes, imshow_fn)

        canvas = matplotlib.backends.backend_wxagg.FigureCanvasAgg(fig)
        if self.saved_image_contents == E_AXES:
            fig.set_frameon(False)
            fig.subplots_adjust(0.1,.1,.9,.9,0,0)
            shape = workspace.display_data.pixel_data.shape
            width = float(shape[1]) / fig.dpi
            height = float(shape[0]) / fig.dpi
            fig.set_figheight(height)
            fig.set_figwidth(width)
        elif self.saved_image_contents == E_IMAGE:
            only_display_image(fig, workspace.display_data.pixel_data.shape)
        else:
            fig.subplots_adjust(.1,.1,.9,.9,0,0)
            
        pixel_data = figure_to_image(fig, dpi=fig.dpi)
        image = cpi.Image(pixel_data)
        workspace.image_set.add(self.display_image.value, image)
Beispiel #2
0
    def run(self, workspace):
        import matplotlib
        import matplotlib.cm
        import matplotlib.backends.backend_wxagg
        import matplotlib.transforms
        from cellprofiler.gui.cpfigure import figure_to_image, only_display_image
        #
        # Get the image
        #
        image = workspace.image_set.get_image(self.image_name.value)
        workspace.display_data.pixel_data = image.pixel_data
        #
        # Get the measurements and positions
        #
        measurements = workspace.measurements
        if self.objects_or_image == OI_IMAGE:
            value = measurements.get_current_image_measurement(
                self.measurement.value)
            values = [value]
            x = [image.pixel_data.shape[1] / 2]
            x_offset = np.random.uniform(high=1.0, low=-1.0)
            x[0] += x_offset
            y = [image.pixel_data.shape[0] / 2]
            y_offset = np.sqrt(1 - x_offset**2)
            y[0] += y_offset
        else:
            values = measurements.get_current_measurement(
                self.objects_name.value, self.measurement.value)
            x = measurements.get_current_measurement(self.objects_name.value,
                                                     M_LOCATION_CENTER_X)
            x_offset = np.random.uniform(high=1.0, low=-1.0, size=x.shape)
            y_offset = np.sqrt(1 - x_offset**2)
            x += self.offset.value * x_offset
            y = measurements.get_current_measurement(self.objects_name.value,
                                                     M_LOCATION_CENTER_Y)
            y += self.offset.value * y_offset
            mask = ~(np.isnan(values) | np.isnan(x) | np.isnan(y))
            values = values[mask]
            x = x[mask]
            y = y[mask]
        workspace.display_data.values = values
        workspace.display_data.x = x
        workspace.display_data.y = y
        fig = matplotlib.figure.Figure()
        axes = fig.add_subplot(1, 1, 1)

        def imshow_fn(pixel_data):
            # Note: requires typecast to avoid failure during
            #       figure_to_image (IMG-764)
            img = pixel_data * 255
            img[img < 0] = 0
            img[img > 255] = 255
            img = img.astype(np.uint8)
            axes.imshow(img, cmap=matplotlib.cm.Greys_r)

        self.display_on_figure(workspace, axes, imshow_fn)

        canvas = matplotlib.backends.backend_wxagg.FigureCanvasAgg(fig)
        if self.saved_image_contents == E_AXES:
            fig.set_frameon(False)
            fig.subplots_adjust(0.1, .1, .9, .9, 0, 0)
            shape = workspace.display_data.pixel_data.shape
            width = float(shape[1]) / fig.dpi
            height = float(shape[0]) / fig.dpi
            fig.set_figheight(height)
            fig.set_figwidth(width)
        elif self.saved_image_contents == E_IMAGE:
            only_display_image(fig, workspace.display_data.pixel_data.shape)
        else:
            fig.subplots_adjust(.1, .1, .9, .9, 0, 0)

        pixel_data = figure_to_image(fig, dpi=fig.dpi)
        image = cpi.Image(pixel_data)
        workspace.image_set.add(self.display_image.value, image)