Esempio n. 1
0
def mesh2Image(vertices,
               faces,
               batch,
               path,
               name,
               height,
               width,
               vertices_num=6890):
    # Create OpenDR renderer
    rn = ColoredRenderer()

    rt_1 = np.zeros(3)

    rn.camera = ProjectPoints(
        v=vertices,  # vertices
        # v=m,
        rt=rt_1,
        # x, y, z translation of the camera, z>=0    0 0 2
        t=np.array([0, 0, 0]),
        # f=np.array([w,w])/2, # focus length? just scaling the picture
        # c=np.array([w,h])/2, #  just move the picture along top-left axis? not sure
        f=np.array([1, 1]),
        c=np.array([0, 0]),
        k=np.zeros(5))
    rn.frustum = {'near': 1, 'far': 15, 'width': width, 'height': height}
    rn.set(v=vertices, f=faces, bgcolor=np.zeros(3))

    # Construct point light source
    rn.vc = LambertianPointLight(
        f=faces,  # face
        v=vertices,
        # v=rn.v, #vertex?
        num_verts=len(vertices),
        light_pos=np.array([-1000, -1000, -2000]),  # point light position
        vc=np.ones_like(vertices) * .9,  # albedo per vertex
        light_color=np.array([1., 1.,
                              1.]))  # Blue, Green, Red; light intensity

    # make the image binary(black and white); these are actually magic steps
    rn.change_col(np.ones((vertices_num, 3)))
    #mask = rn.r.copy()  # takes lots of time

    mask = rn.r * 255
    import cv2
    if batch == 1:
        cv2.imwrite('%s/%s.png' % (path, name), mask)
    else:
        cv2.imwrite('%s/%s_%d.png' % (path, name, i), mask)
    '''
                              c=np.array([0, 0]),
                              k=np.zeros(5))

    rn.frustum = {'near': 1., 'far': 20., 'width': w, 'height': h}
    rn.set(v=mesh, f=m.f, bgcolor=np.zeros(3))
    rn.vc = LambertianPointLight(f=m.f,
                                 v=mesh,
                                 num_verts=len(m),
                                 light_pos=np.array([0, 0, 0]),
                                 vc=np.ones_like(m) * .9,
                                 light_color=np.array([1., 1., 1.]))

    mod = i % bg_number
    bg = misc.imread(os.path.join(bg_pth, '%d.png' % (mod)))

    rn.change_col(np.ones((778, 3)))

    mask = rn.r.copy()
    mask = mask[:, :, 0].astype(np.uint8)

    rn.change_col(colors[random.randint(0, 26)])

    hand = rn.r.copy() * 255.
    image = (1 - np.expand_dims(mask, 2)) * bg + np.expand_dims(mask, 2) * hand

    # image
    misc.imsave(os.path.join(output_path, '%d.png' % i), image)
    # segmentation
    #misc.imsave(os.path.join(output_path, 'mask_%d.png'%i), mask*255)

    # joint locations