Example #1
0
    def apply_colormap(r1, r2):
        if mem.slicer_rgb:
            return

        if mem.slicer_colormap == 'disting':
            # use distinguishable colors
            rgb = colormap.distinguishable_colormap(nb_colors=256)
            rgb = np.asarray(rgb)
        else:
            # use matplotlib colormaps
            rgb = colormap.create_colormap(np.linspace(r1, r2, 256),
                                           name=mem.slicer_colormap,
                                           auto=True)
        N = rgb.shape[0]

        lut = colormap.LookupTable()
        lut.SetNumberOfTableValues(N)
        lut.SetRange(r1, r2)
        for i in range(N):
            r, g, b = rgb[i]
            lut.SetTableValue(i, r, g, b)
        lut.SetRampToLinear()
        lut.Build()

        mem.slicer_curr_actor_z.output.SetLookupTable(lut)
        mem.slicer_curr_actor_z.output.Update()
Example #2
0
def color_tree2(tree, bg=(1, 1, 1), min_level=0):
    import colorsys
    from dipy.viz.colormap import distinguishable_colormap
    global colormap
    colormap = iter(distinguishable_colormap(bg=bg, exclude=[(1., 1., 0.93103448)]))

    def _color_subtree(node, color=None, level=0,  max_lum=0.9, min_lum=0.1):
        global colormap

        node.color = color

        #max_lum = 1
        if color is not None:
            hls = np.asarray(colorsys.rgb_to_hls(*color))
            max_lum = min(max_lum, hls[1] + 0.2)
            min_lum = max(min_lum, hls[1] - 0.2)

        children_sizes = map(len, node.children)
        indices = np.argsort(children_sizes)[::-1]
        luminosities = np.linspace(max_lum, min_lum, len(node.children)+1)
        #for child, luminosity, offset in zip(node.children, luminosities, offsets):
        for i, idx in enumerate(indices):
            child = node.children[idx]
            if level <= min_level:
                color = next(colormap)
                _color_subtree(child, color, level+1)
            else:
                hls = np.asarray(colorsys.rgb_to_hls(*color))
                #rbg = colorsys.hls_to_rgb(hls[0], (luminosities[i]+luminosities[i+1])/2, hls[2])
                rbg = colorsys.hls_to_rgb(hls[0], luminosities[i+1], hls[2])
                _color_subtree(child, np.asarray(rbg), level+1, luminosities[i], luminosities[i+1])

    _color_subtree(tree.root)
Example #3
0
    def _cluster(self, threshold):
        qb = QuickBundles(metric=metric, threshold=threshold)
        self.last_threshold = threshold

        self.clusters = qb.cluster(self.streamlines)
        self.clusters_colors = [
            color for c, color in zip(
                self.clusters,
                distinguishable_colormap(bg=(0, 0, 0), exclude=darkcolors))
        ]

        if threshold < np.inf:
            print("{} clusters with QB threshold of {}mm".format(
                len(self.clusters), threshold))

        # Keep a flag telling there have been changes.
        self.has_changed = True

        if len(self.clusters) == 1:
            # Keep initial color
            self.clusters_colors = [self.color
                                    ] if self.color is not None else [(0, 0,
                                                                       1)]
            vtk_colors = utils.numpy_to_vtk_colors(255 * self.original_colors)
            vtk_colors.SetName("Colors")
            self.actor.GetMapper().GetInput().GetPointData().SetScalars(
                vtk_colors)
            return

        for cluster, color in zip(self.clusters, self.clusters_colors):
            self.streamlines_colors[cluster.indices] = color

        self.colors = []
        for color, streamline in zip(self.streamlines_colors,
                                     self.streamlines):
            self.colors += [color] * len(streamline)

        vtk_colors = utils.numpy_to_vtk_colors(255 * np.array(self.colors))
        vtk_colors.SetName("Colors")
        self.actor.GetMapper().GetInput().GetPointData().SetScalars(vtk_colors)
Example #4
0
def color_tree(tree, bg=(1, 1, 1), min_level=0):
    import colorsys
    from dipy.viz.colormap import distinguishable_colormap
    global colormap
    colormap = iter(distinguishable_colormap(bg=bg, exclude=[(1., 1., 0.93103448)]))

    def _color_subtree(node, color=None, level=0):
        global colormap

        node.color = color

        max_luminosity = 0
        if color is not None:
            hls = np.asarray(colorsys.rgb_to_hls(*color))
            max_luminosity = hls[1]

        #luminosities = np.linspace(0.3, 0.8, len(node.children))
        children_sizes = map(len, node.children)
        indices = np.argsort(children_sizes)[::-1]
        luminosities = np.linspace(max_luminosity, 0.2, len(node.children))
        #for child, luminosity, offset in zip(node.children, luminosities, offsets):
        for idx, luminosity in zip(indices, luminosities):
            child = node.children[idx]
            if level <= min_level:
                color = next(colormap)
                _color_subtree(child, color, level+1)
            else:
                hls = np.asarray(colorsys.rgb_to_hls(*color))
                #if hls[1] > 0.8:
                #    hls[1] -= 0.3
                #elif hls[1] < 0.3:
                #    hls[1] += 0.3

                rbg = colorsys.hls_to_rgb(hls[0], luminosity, hls[2])
                _color_subtree(child, np.asarray(rbg), level+1)

    _color_subtree(tree.root)