Esempio n. 1
0
def main():
    plt.grid_generic()
    plt.show(persistent=False)
    plt.scatter_generic(**generate_real_size_points())

    generate_animation()
    plt.close_app()
Esempio n. 2
0
def simple_visualize(input_arr,
                     tracking_result_history,
                     template_edges,
                     key_func,
                     filter_point_cloud=False,
                     target_fps=30):

    offset = -tracking_result_history[0].mean(axis=0)

    glplt.clear()
    glplt.show()
    glplt.grid_generic()
    tracking_result_edges_item = glplt.edge_set(tracking_result_history[0] +
                                                offset,
                                                template_edges,
                                                color=(1, 1, 1, 1),
                                                width=2.0)
    tracking_result_points_item = glplt.scatter_generic(
        tracking_result_history[0] + offset,
        color=(1, 0, 0, 1),
        size=0.01,
        pxMode=False)

    point_cloud_item = None
    desired_frame_time = 1 / target_fps
    last_time = time.time()
    for i in cycle(range(len(tracking_result_history))):
        print(i)
        point_cloud_img, color_img = input_arr[i]
        tracking_result = tracking_result_history[i]

        # generate mask
        mask_img = key_func(point_cloud_img, color_img)
        filtered_point_cloud = point_cloud_img
        if filter_point_cloud:
            filtered_point_cloud = point_cloud_img[mask_img]

        # update point cloud
        if point_cloud_item is None:
            point_cloud_item = glplt.point_cloud(filtered_point_cloud + offset,
                                                 color_img,
                                                 size=0.9,
                                                 pxMode=True)
        else:
            glplt.update_point_cloud(point_cloud_item,
                                     filtered_point_cloud + offset, color_img)

        glplt.update_edge_set(tracking_result_edges_item,
                              tracking_result + offset, template_edges)
        tracking_result_points_item.setData(pos=tracking_result + offset)

        curr_time = time.time()
        curr_frame_time = curr_time - last_time
        last_time = curr_time
        if curr_frame_time < desired_frame_time:
            time.sleep(desired_frame_time - curr_frame_time)
Esempio n. 3
0
def generate_animation():
    pos = np.random.random(size=(100000, 3))
    pos *= [10, -10, 10]
    pos[0] = (0, 0, 0)
    color = np.ones((pos.shape[0], 4))
    d2 = (pos**2).sum(axis=1)**0.5
    size = np.random.random(size=pos.shape[0]) * 10
    phase = 0.

    pos3 = np.zeros((100, 100, 3))
    pos3[:, :, :2] = np.mgrid[:100, :100].transpose(1, 2, 0) * [-0.1, 0.1]
    pos3 = pos3.reshape(10000, 3)
    d3 = (pos3**2).sum(axis=1)**0.5

    sp2 = plt.scatter_generic(pos=pos, color=(1, 1, 1, 1), size=size)
    sp3 = plt.scatter_generic(pos=pos3,
                              color=(1, 1, 1, .3),
                              size=0.1,
                              pxMode=False)

    while plt.is_alive():
        ## update volume colors
        s = -np.cos(d2 * 2 + phase)
        color = np.empty((len(d2), 4), dtype=np.float32)
        color[:, 3] = np.clip(s * 0.1, 0, 1)
        color[:, 0] = np.clip(s * 3.0, 0, 1)
        color[:, 1] = np.clip(s * 1.0, 0, 1)
        color[:, 2] = np.clip(s**3, 0, 1)
        sp2.setData(color=color)
        phase -= 0.1

        ## update surface positions and colors
        z = -np.cos(d3 * 2 + phase)
        pos3[:, 2] = z
        color = np.empty((len(d3), 4), dtype=np.float32)
        color[:, 3] = 0.3
        color[:, 0] = np.clip(z * 3.0, 0, 1)
        color[:, 1] = np.clip(z * 1.0, 0, 1)
        color[:, 2] = np.clip(z**3, 0, 1)
        sp3.setData(pos=pos3, color=color)

        time.sleep(1.0 / 30)
Esempio n. 4
0
 def __init__(self, offset, template, grid=None, num_correspondence=5):
     self.offset = offset
     self.template_item = glplt.scatter_generic(pos=template + self.offset,
                                                color=(1, 0, 1, 1),
                                                size=0.005,
                                                pxMode=False)
     self.source_item = None
     self.target_item = None
     self.correspondence_item = None
     self.grid_item = None
     self.grid_verts = None
     self.grid_edges = None
     self.grid_kernel = None
     if grid is not None:
         self.grid_verts, self.grid_edges, beta = grid
         self.grid_kernel = gaussian_kernel_any(self.grid_verts, template,
                                                beta)
         self.grid_item = glplt.edge_set(self.grid_verts + self.offset,
                                         self.grid_edges,
                                         width=0.1,
                                         color=(1, 1, 1, 1))
     self.num_correspondence = num_correspondence
     self.coord_item = glplt.grid_generic()
     glplt.show()
Esempio n. 5
0
    def callback(self, iteration, error, X, Y, W=None, P=None):
        # self.offset = -X.mean(axis=0)
        if self.source_item is None:
            self.source_item = glplt.scatter_generic(pos=Y + self.offset,
                                                     color=(1, 0, 0, 1),
                                                     size=0.005,
                                                     pxMode=False)
        else:
            self.source_item.setData(pos=Y + self.offset)

        if self.target_item is None:
            self.target_item = glplt.scatter_generic(X + self.offset,
                                                     color=(0, 1, 1, 1),
                                                     size=0.002,
                                                     pxMode=False)
        else:
            self.target_item.setData(pos=X + self.offset)

        if W is not None and self.grid_kernel is not None:
            new_grid_verts = self.grid_verts + np.dot(self.grid_kernel,
                                                      W) + self.offset
            glplt.update_edge_set(self.grid_item,
                                  new_grid_verts,
                                  self.grid_edges,
                                  width=0.1)

        if P is not None and self.num_correspondence > 0:
            # P = np.copy(P)
            # row_sums = P.max(axis=1)
            # P /= row_sums[:, np.newaxis]
            # p_mask = P > self.p_threshold
            k_largest = P.shape[1] - self.num_correspondence
            mask_idxs = np.argpartition(P, k_largest, axis=1)[:, k_largest:]
            p_mask = np.zeros(P.shape, dtype=np.bool)
            for i in range(mask_idxs.shape[0]):
                p_mask[i, mask_idxs[i]] = True
            idx_pairs = np.transpose(np.mgrid[0:P.shape[0], 0:P.shape[1]],
                                     axes=[1, 2, 0])
            edge_idxs = idx_pairs[p_mask]
            p_values = P[p_mask]
            cmap = cm.get_cmap('viridis')
            corr_colors = cmap(np.sqrt(p_values))
            corr_lines = np.empty((p_values.shape[0] * 2, Y.shape[-1]))
            corr_lines[0::2] = Y[edge_idxs[:, 0]]
            corr_lines[1::2] = X[edge_idxs[:, 1]]
            corr_line_colors = np.empty(
                (corr_colors.shape[0] * 2, corr_colors.shape[1]))
            corr_line_colors[0::2] = corr_colors
            corr_line_colors[1::2] = corr_colors
            if self.correspondence_item is None:
                self.correspondence_item = glplt.line_generic(
                    pos=corr_lines + self.offset,
                    color=corr_line_colors,
                    width=1.0,
                    mode='lines')
                self.correspondence_item.setGLOptions('opaque')
            else:
                self.correspondence_item.setData(pos=corr_lines + self.offset,
                                                 color=corr_line_colors)

        time.sleep(1.0)