Esempio n. 1
0
def getScalarMappable(colormap, data=None):
    """Returns matplotlib ScalarMappable corresponding to colormap

    :param :class:`.Colormap` colormap: The colormap to convert
    :param numpy.ndarray data:
        The data on which the colormap is applied.
        If provided, it is used to compute autoscale.
    :return: matplotlib object corresponding to colormap
    :rtype: matplotlib.cm.ScalarMappable
    """
    assert colormap is not None

    if colormap.getName() is not None:
        cmap = getColormap(colormap.getName())

    else:  # No name, use custom colors
        if colormap.getColormapLUT() is None:
            raise ValueError(
                'addImage: colormap no name nor list of colors.')
        colors = colormap.getColormapLUT()
        assert len(colors.shape) == 2
        assert colors.shape[-1] in (3, 4)
        if colors.dtype == numpy.uint8:
            # Convert to float in [0., 1.]
            colors = colors.astype(numpy.float32) / 255.
        cmap = matplotlib.colors.ListedColormap(colors)

    vmin, vmax = colormap.getColormapRange(data)
    if colormap.getNormalization().startswith('log'):
        norm = matplotlib.colors.LogNorm(vmin, vmax)
    else:  # Linear normalization
        norm = matplotlib.colors.Normalize(vmin, vmax)

    return matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)
def getScalarMappable(colormap, data=None):
    """Returns matplotlib ScalarMappable corresponding to colormap

    :param :class:`.Colormap` colormap: The colormap to convert
    :param numpy.ndarray data:
        The data on which the colormap is applied.
        If provided, it is used to compute autoscale.
    :return: matplotlib object corresponding to colormap
    :rtype: matplotlib.cm.ScalarMappable
    """
    assert colormap is not None

    if colormap.getName() is not None:
        cmap = getColormap(colormap.getName())

    else:  # No name, use custom colors
        if colormap.getColormapLUT() is None:
            raise ValueError('addImage: colormap no name nor list of colors.')
        colors = colormap.getColormapLUT()
        assert len(colors.shape) == 2
        assert colors.shape[-1] in (3, 4)
        if colors.dtype == numpy.uint8:
            # Convert to float in [0., 1.]
            colors = colors.astype(numpy.float32) / 255.
        cmap = matplotlib.colors.ListedColormap(colors)

    vmin, vmax = colormap.getColormapRange(data)
    if colormap.getNormalization().startswith('log'):
        norm = matplotlib.colors.LogNorm(vmin, vmax)
    else:  # Linear normalization
        norm = matplotlib.colors.Normalize(vmin, vmax)

    return matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)
Esempio n. 3
0
def plot3DHist(hist, means, depth = (3,3,3), colorout = "BGR", filterfunc = lambda x,y,z,h:  h > 1000):

    bins = (2**depth[0], 2**depth[1], 2**depth[2])
    maxsize = (bins[0]*bins[1]*bins[2])
    xs = np.zeros(maxsize)
    ys = np.zeros(maxsize)
    zs = np.zeros(maxsize)
    filter = np.zeros(maxsize, dtype = bool)
    colors = np.zeros((maxsize,3),dtype= np.float32)
    hist = hist.flatten()

    # Generate points for indices that exceed a particular threshold
    for i in range(maxsize):
        z = i % bins[2]
        y = (i >> depth[2])%bins[1]
        x = (i >> (depth[1]+depth[2])) % bins[0]
        colors[i] = np.array([x*256//bins[0],y*256//bins[1],z*256//bins[2]],dtype = np.uint8)
        zs[i] = z
        ys[i] = y
        xs[i] = x

        filter[i] = filterfunc(x,y,z,hist[i])
    xs = xs[filter]
    ys = ys[filter]
    zs = zs[filter]
    colors = colors[filter]
    hist = hist[filter]
    if(cvtCode(colorout,"RGB")):
        colors = cv.cvtColor(np.array([colors],dtype = np.uint8),cvtCode(colorout,"RGB"))[0]
        print(colors)
    colors = np.divide(colors,256)
    print(colors)
    ax1 = plotScatter3D(xs, ys, zs, getColorMap(np.log(1+hist)),bins)
    ax2 = plotScatter3D(xs, ys, zs, colors.astype(np.float32),bins)
    ax1.scatter(means[:, 0], means[:, 1], means[:, 2], "k", marker = "^")
    ax2.scatter(means[:, 0], means[:, 1], means[:, 2], "k", marker = "^")
Esempio n. 4
0
def getScalarMappable(colormap, data=None):
    """Returns matplotlib ScalarMappable corresponding to colormap

    :param :class:`.Colormap` colormap: The colormap to convert
    :param numpy.ndarray data:
        The data on which the colormap is applied.
        If provided, it is used to compute autoscale.
    :return: matplotlib object corresponding to colormap
    :rtype: matplotlib.cm.ScalarMappable
    """
    assert colormap is not None

    if colormap.getName() is not None:
        cmap = getColormap(colormap.getName())

    else:  # No name, use custom colors
        if colormap.getColormapLUT() is None:
            raise ValueError('addImage: colormap no name nor list of colors.')
        colors = colormap.getColormapLUT()
        assert len(colors.shape) == 2
        assert colors.shape[-1] in (3, 4)
        if colors.dtype == numpy.uint8:
            # Convert to float in [0., 1.]
            colors = colors.astype(numpy.float32) / 255.
        cmap = matplotlib.colors.ListedColormap(colors)

    if colormap.getNormalization().startswith('log'):
        vmin, vmax = None, None
        if not colormap.isAutoscale():
            if colormap.getVMin() > 0.:
                vmin = colormap.getVMin()
            if colormap.getVMax() > 0.:
                vmax = colormap.getVMax()

            if vmin is None or vmax is None:
                _logger.warning('Log colormap with negative bounds, ' +
                                'changing bounds to positive ones.')
            elif vmin > vmax:
                _logger.warning('Colormap bounds are inverted.')
                vmin, vmax = vmax, vmin

        # Set unset/negative bounds to positive bounds
        if vmin is None or vmax is None:
            # Convert to numpy array
            data = numpy.array(data if data is not None else [], copy=False)

            if data.size > 0:
                finiteData = data[numpy.isfinite(data)]
                posData = finiteData[finiteData > 0]
                if vmax is None:
                    # 1. as an ultimate fallback
                    vmax = posData.max() if posData.size > 0 else 1.
                if vmin is None:
                    vmin = posData.min() if posData.size > 0 else vmax
                if vmin > vmax:
                    vmin = vmax
            else:
                vmin, vmax = 1., 1.

        norm = matplotlib.colors.LogNorm(vmin, vmax)

    else:  # Linear normalization
        if colormap.isAutoscale():
            # Convert to numpy array
            data = numpy.array(data if data is not None else [], copy=False)

            if data.size == 0:
                vmin, vmax = 1., 1.
            else:
                finiteData = data[numpy.isfinite(data)]
                if finiteData.size > 0:
                    vmin = finiteData.min()
                    vmax = finiteData.max()
                else:
                    vmin, vmax = 1., 1.

        else:
            vmin = colormap.getVMin()
            vmax = colormap.getVMax()
            if vmin > vmax:
                _logger.warning('Colormap bounds are inverted.')
                vmin, vmax = vmax, vmin

        norm = matplotlib.colors.Normalize(vmin, vmax)

    return matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)
Esempio n. 5
0
    def __init__(self, scene_reconstructions, image_files, K, show=True):

        self.images = {}
        self.num_frames = len(image_files)
        frame = 0
        for image in image_files:
            self.images[frame] = mpimg.imread(image)
            frame += 1

        self.K = K
        self.nH = self.images.itervalues().next().shape[0]
        self.nW = self.images.itervalues().next().shape[1]

        # prepare point clouds
        self.point_clouds = {}

        num_objects = len(scene_reconstructions['rotations'])
        cmap = get_colors(num_objects + 2)

        # cmap = np.array([[1.0000, 0.2857, 0, 1.0],
        #                  [1.0000, 0.5714, 0, 1.0],
        #                  [1.0000, 0.8571, 0, 1.0],
        #                  [0.8571, 1.0000, 0, 1.0],
        #                  [0.5714, 1.0000, 0, 1.0],
        #                  [0, 1.0000, 0.8571, 1.0],
        #                  [0, 0.8571, 1.0000, 1.0],
        #                  [0, 0.5714, 1.0000, 1.0],
        #                  [0, 0.2857, 1.0000, 1.0],
        #                  [0, 0, 1.0000, 1.0],
        #                  [0.2857, 0, 1.0000, 1.0],
        #                  [0.8571, 0, 1.0000, 1.0],
        #                  [0.8571, 0, 0.5000, 1.0],
        #                  [0.5571, 0, 0.5000, 1.0]])

        object_index = 0

        for label in range(num_objects):
            vertices = scene_reconstructions['shapes'][label].T
            colors = scene_reconstructions['colors'][label].T
            points = scene_reconstructions['points'][label][0]

            point_cloud = {}
            point_cloud['scale'] = 1.0
            # point_cloud['vertices'] = np.ascontiguousarray(vertices)
            # point_cloud['colors'] = np.ascontiguousarray(colors)
            # point_cloud['vertices'] = vertices.astype(np.float32)
            # point_cloud['colors'] = colors.astype(np.float32)

            point_cloud['vertices'] = np.ascontiguousarray(
                vertices.astype(np.float32))
            point_cloud['colors'] = np.ascontiguousarray(
                colors.astype(np.float32))

            point_cloud['vertex_ids'] = points
            point_cloud['seg_color'] = cmap[object_index,
                                            0:3].astype(np.float32)
            point_cloud['rots'] = scene_reconstructions['rotations'][label]
            point_cloud['trans'] = scene_reconstructions['translations'][label]

            self.point_clouds[label] = point_cloud

            object_index += 1

        # parameters
        self.ref_color = cmap[num_objects, 0:3].astype(np.float32)
        self.selected_color = cmap[num_objects + 1, 0:3].astype(np.float32)

        self.show_ref_color = False
        self.show_sel_color = False

        self.show_color = True
        self.show_gt = False
        # self.fix_camera = True
        # self.fix_object = -1

        self.fix_camera = True
        self.fix_object = -1
        self.sel_object = -1

        self.cur_frame = 0

        self.show_camera = False
        self.show_all_cameras = False

        self.camera_length = 1

        self.show_text = False
        self.text_size = 12

        self.point_size = 5.0

        # create canvas
        #vispy.scene.SceneCanvas.__init__(self, keys='interactive', show=True)
        vispy.scene.SceneCanvas.__init__(self,
                                         keys='interactive',
                                         show=True,
                                         bgcolor='w')

        self.unfreeze()
        self.view = self.central_widget.add_view()

        ## add nodes to view
        self.create_nodes()

        ## transform these nodes
        self.transform_nodes()

        self.view.camera = 'arcball'
        # self.view.camera = 'turntable'

        if (show):
            self.app.run()