Пример #1
0
    def display(self, workspace, figure):
        '''Create an informative display for the module'''
        import matplotlib
        from cellprofiler.gui.tools import renumber_labels_for_display
        original_labels = workspace.display_data.original_labels
        final_labels = workspace.display_data.final_labels
        mask = workspace.display_data.mask
        #
        # Create a composition of the final labels and mask
        #
        final_labels = renumber_labels_for_display(final_labels)
        outlines = outline(original_labels) > 0

        cm = matplotlib.cm.get_cmap(cpprefs.get_default_colormap())
        sm = matplotlib.cm.ScalarMappable(cmap=cm)
        #
        # Paint the labels in color
        #
        image = sm.to_rgba(final_labels)[:, :, :3]
        image[final_labels == 0, :] = 0
        #
        # Make the mask a dark gray
        #
        image[(final_labels == 0) & mask, :] = .25
        #
        # Make the outlines of the kept objects the primary color
        # and the outlines of removed objects red.
        #
        final_outlines = outline(final_labels) > 0
        original_color = np.array(cpprefs.get_secondary_outline_color(),
                                  float) / 255
        final_color = np.array(cpprefs.get_primary_outline_color(),
                               float) / 255
        image[outlines, :] = original_color[np.newaxis, :]
        image[final_outlines, :] = final_color[np.newaxis, :]

        figure.set_subplots((2, 1))
        figure.subplot_imshow_labels(0,
                                     0,
                                     original_labels,
                                     title=self.object_name.value)
        figure.subplot_imshow_color(1,
                                    0,
                                    image,
                                    title=self.remaining_objects.value,
                                    sharexy=figure.subplot(0, 0))
Пример #2
0
    def display(self, workspace, figure):
        '''Create an informative display for the module'''
        import matplotlib
        from cellprofiler.gui.tools import renumber_labels_for_display
        original_labels = workspace.display_data.original_labels
        final_labels = workspace.display_data.final_labels
        mask = workspace.display_data.mask
        #
        # Create a composition of the final labels and mask
        #
        final_labels = renumber_labels_for_display(final_labels)
        outlines = outline(original_labels) > 0

        cm = matplotlib.cm.get_cmap(cpprefs.get_default_colormap())
        sm = matplotlib.cm.ScalarMappable(cmap=cm)
        #
        # Paint the labels in color
        #
        image = sm.to_rgba(final_labels)[:, :, :3]
        image[final_labels == 0, :] = 0
        #
        # Make the mask a dark gray
        #
        image[(final_labels == 0) & mask, :] = .25
        #
        # Make the outlines of the kept objects the primary color
        # and the outlines of removed objects red.
        #
        final_outlines = outline(final_labels) > 0
        original_color = np.array(cpprefs.get_secondary_outline_color(), float) / 255
        final_color = np.array(cpprefs.get_primary_outline_color(), float) / 255
        image[outlines, :] = original_color[np.newaxis, :]
        image[final_outlines, :] = final_color[np.newaxis, :]

        figure.set_subplots((2, 1))
        figure.subplot_imshow_labels(0, 0, original_labels,
                                     title=self.object_name.value)
        figure.subplot_imshow_color(1, 0, image,
                                    title=self.remaining_objects.value,
                                    sharexy=figure.subplot(0, 0))
    def display(self, workspace, figure):
        if not self.show_window:
            return
        from cellprofiler.gui.tools import renumber_labels_for_display
        figure.set_subplots((2, 2))
        renumbered_parent_labels = renumber_labels_for_display(
            workspace.display_data.parent_labels)
        child_labels = workspace.display_data.child_labels
        parents_of = workspace.display_data.parents_of
        #
        # discover the mapping so that we can apply it to the children
        #
        mapping = np.arange(workspace.display_data.parent_count + 1)
        mapping[workspace.display_data.parent_labels.flatten()] = \
            renumbered_parent_labels.flatten()
        parent_labeled_children = np.zeros(child_labels.shape, int)
        mask = child_labels > 0
        parent_labeled_children[mask] = \
            mapping[parents_of[child_labels[mask] - 1]]

        figure.subplot_imshow_labels(0,
                                     0,
                                     renumbered_parent_labels,
                                     title=self.parent_name.value,
                                     renumber=False)
        figure.subplot_imshow_labels(1,
                                     0,
                                     child_labels,
                                     title=self.sub_object_name.value,
                                     sharex=figure.subplot(0, 0),
                                     sharey=figure.subplot(0, 0))
        figure.subplot_imshow_labels(
            0,
            1,
            parent_labeled_children,
            "%s labeled by %s" %
            (self.sub_object_name.value, self.parent_name.value),
            renumber=False,
            sharex=figure.subplot(0, 0),
            sharey=figure.subplot(0, 0))
Пример #4
0
    def display(self, workspace, figure):
        if not self.show_window:
            return
        from cellprofiler.gui.tools import renumber_labels_for_display
        figure.set_subplots((2, 2))
        renumbered_parent_labels = renumber_labels_for_display(
                workspace.display_data.parent_labels)
        child_labels = workspace.display_data.child_labels
        parents_of = workspace.display_data.parents_of
        #
        # discover the mapping so that we can apply it to the children
        #
        mapping = np.arange(workspace.display_data.parent_count + 1)
        mapping[workspace.display_data.parent_labels.flatten()] = \
            renumbered_parent_labels.flatten()
        parent_labeled_children = np.zeros(child_labels.shape, int)
        mask = child_labels > 0
        parent_labeled_children[mask] = \
            mapping[parents_of[child_labels[mask] - 1]]

        figure.subplot_imshow_labels(
                0, 0, renumbered_parent_labels,
                title=self.parent_name.value,
                renumber=False)
        figure.subplot_imshow_labels(
                1, 0, child_labels,
                title=self.sub_object_name.value,
                sharex=figure.subplot(0, 0),
                sharey=figure.subplot(0, 0))
        figure.subplot_imshow_labels(
                0, 1, parent_labeled_children,
                "%s labeled by %s" %
                (self.sub_object_name.value,
                 self.parent_name.value),
                renumber=False,
                sharex=figure.subplot(0, 0),
                sharey=figure.subplot(0, 0))
    def draw_outlines(self, pixel_data, labels):
        '''Draw a color image that shows the objects

        pixel_data - image, either b & w or color
        labels - labels for image

        returns - color image of same size as pixel_data
        '''
        from cellprofiler.gui.tools import renumber_labels_for_display
        import matplotlib

        labels = renumber_labels_for_display(labels)
        outlines = outline(labels)

        if pixel_data.ndim == 3:
            image = pixel_data.copy()
        else:
            image = np.dstack([pixel_data] * 3)
        #
        # make labeled pixels a grayscale times the label color
        #
        cm = matplotlib.cm.get_cmap(cpprefs.get_default_colormap())
        sm = matplotlib.cm.ScalarMappable(cmap=cm)
        labels_image = sm.to_rgba(labels)[:, :, :3]

        lmask = labels > 0
        gray = (image[lmask, 0] + image[lmask, 1] + image[lmask, 2]) / 3

        for i in range(3):
            image[lmask, i] = gray * labels_image[lmask, i]
        #
        # Make the outline pixels a solid color
        #
        outlines_image = sm.to_rgba(outlines)[:, :, :3]
        image[outlines > 0, :] = outlines_image[outlines > 0, :]
        return image
    def draw_outlines(self, pixel_data, labels):
        '''Draw a color image that shows the objects

        pixel_data - image, either b & w or color
        labels - labels for image

        returns - color image of same size as pixel_data
        '''
        from cellprofiler.gui.tools import renumber_labels_for_display
        import matplotlib

        labels = renumber_labels_for_display(labels)
        outlines = outline(labels)

        if pixel_data.ndim == 3:
            image = pixel_data.copy()
        else:
            image = np.dstack([pixel_data] * 3)
        #
        # make labeled pixels a grayscale times the label color
        #
        cm = matplotlib.cm.get_cmap(cpprefs.get_default_colormap())
        sm = matplotlib.cm.ScalarMappable(cmap=cm)
        labels_image = sm.to_rgba(labels)[:, :, :3]

        lmask = labels > 0
        gray = (image[lmask, 0] + image[lmask, 1] + image[lmask, 2]) / 3

        for i in range(3):
            image[lmask, i] = gray * labels_image[lmask, i]
        #
        # Make the outline pixels a solid color
        #
        outlines_image = sm.to_rgba(outlines)[:, :, :3]
        image[outlines > 0, :] = outlines_image[outlines > 0, :]
        return image
 def run(self, workspace):
     objects = workspace.object_set.get_objects(self.object_name.value)
     alpha = np.zeros(objects.shape)
     if self.image_mode == IM_BINARY:
         pixel_data = np.zeros(objects.shape, bool)
     elif self.image_mode == IM_GRAYSCALE:
         pixel_data = np.zeros(objects.shape)
     elif self.image_mode == IM_UINT16:
         pixel_data = np.zeros(objects.shape, np.int32)
     else:
         pixel_data = np.zeros((objects.shape[0], objects.shape[1], 3))
     convert = True
     for labels, indices in objects.get_labels():
         mask = labels != 0
         if np.all(~ mask):
             continue
         if self.image_mode == IM_BINARY:
             pixel_data[mask] = True
             alpha[mask] = 1
         elif self.image_mode == IM_GRAYSCALE:
             pixel_data[mask] = labels[mask].astype(float) / np.max(labels)
             alpha[mask] = 1
         elif self.image_mode == IM_COLOR:
             import matplotlib.cm
             from cellprofiler.gui.tools import renumber_labels_for_display
             if self.colormap.value == DEFAULT_COLORMAP:
                 cm_name = cpprefs.get_default_colormap()
             elif self.colormap.value == COLORCUBE:
                 # Colorcube missing from matplotlib
                 cm_name = "gist_rainbow"
             elif self.colormap.value == LINES:
                 # Lines missing from matplotlib and not much like it,
                 # Pretty boring palette anyway, hence
                 cm_name = "Pastel1"
             elif self.colormap.value == WHITE:
                 # White missing from matplotlib, it's just a colormap
                 # of all completely white... not even different kinds of
                 # white. And, isn't white just a uniform sampling of
                 # frequencies from the spectrum?
                 cm_name = "Spectral"
             else:
                 cm_name = self.colormap.value
             cm = matplotlib.cm.get_cmap(cm_name)
             mapper = matplotlib.cm.ScalarMappable(cmap=cm)
             pixel_data[mask, :] += \
                 mapper.to_rgba(renumber_labels_for_display(labels))[mask, :3]
             alpha[mask] += 1
         elif self.image_mode == IM_UINT16:
             pixel_data[mask] = labels[mask]
             alpha[mask] = 1
             convert = False
     mask = alpha > 0
     if self.image_mode == IM_BINARY:
         pass
     elif self.image_mode == IM_COLOR:
         pixel_data[mask, :] = pixel_data[mask, :] / alpha[mask][:, np.newaxis]
     else:
         pixel_data[mask] = pixel_data[mask] / alpha[mask]
     image = cpi.Image(pixel_data, parent_image=objects.parent_image,
                       convert=convert)
     workspace.image_set.add(self.image_name.value, image)
     if self.show_window:
         workspace.display_data.ijv = objects.ijv
         workspace.display_data.pixel_data = pixel_data
Пример #8
0
 def run(self, workspace):
     objects = workspace.object_set.get_objects(self.object_name.value)
     alpha = np.zeros(objects.shape)
     if self.image_mode == IM_BINARY:
         pixel_data = np.zeros(objects.shape, bool)
     elif self.image_mode == IM_GRAYSCALE:
         pixel_data = np.zeros(objects.shape)
     elif self.image_mode == IM_UINT16:
         pixel_data = np.zeros(objects.shape, np.int32)
     else:
         pixel_data = np.zeros((objects.shape[0], objects.shape[1], 3))
     convert = True
     for labels, indices in objects.get_labels():
         mask = labels != 0
         if np.all(~mask):
             continue
         if self.image_mode == IM_BINARY:
             pixel_data[mask] = True
             alpha[mask] = 1
         elif self.image_mode == IM_GRAYSCALE:
             pixel_data[mask] = labels[mask].astype(float) / np.max(labels)
             alpha[mask] = 1
         elif self.image_mode == IM_COLOR:
             import matplotlib.cm
             from cellprofiler.gui.tools import renumber_labels_for_display
             if self.colormap.value == DEFAULT_COLORMAP:
                 cm_name = cpprefs.get_default_colormap()
             elif self.colormap.value == COLORCUBE:
                 # Colorcube missing from matplotlib
                 cm_name = "gist_rainbow"
             elif self.colormap.value == LINES:
                 # Lines missing from matplotlib and not much like it,
                 # Pretty boring palette anyway, hence
                 cm_name = "Pastel1"
             elif self.colormap.value == WHITE:
                 # White missing from matplotlib, it's just a colormap
                 # of all completely white... not even different kinds of
                 # white. And, isn't white just a uniform sampling of
                 # frequencies from the spectrum?
                 cm_name = "Spectral"
             else:
                 cm_name = self.colormap.value
             cm = matplotlib.cm.get_cmap(cm_name)
             mapper = matplotlib.cm.ScalarMappable(cmap=cm)
             pixel_data[mask, :] += \
                 mapper.to_rgba(renumber_labels_for_display(labels))[mask, :3]
             alpha[mask] += 1
         elif self.image_mode == IM_UINT16:
             pixel_data[mask] = labels[mask]
             alpha[mask] = 1
             convert = False
     mask = alpha > 0
     if self.image_mode == IM_BINARY:
         pass
     elif self.image_mode == IM_COLOR:
         pixel_data[mask, :] = pixel_data[mask, :] / alpha[mask][:,
                                                                 np.newaxis]
     else:
         pixel_data[mask] = pixel_data[mask] / alpha[mask]
     image = cpi.Image(pixel_data,
                       parent_image=objects.parent_image,
                       convert=convert)
     workspace.image_set.add(self.image_name.value, image)
     if self.show_window:
         workspace.display_data.ijv = objects.ijv
         workspace.display_data.pixel_data = pixel_data