nileshkulkarni / csm

Code release for "Canonical Surface Mapping via Geometric Cycle Consistency"
https://nileshkulkarni.github.io/csm/
186 stars 31 forks source link

how can we convert uv values to hsv and then to rgb? #11

Closed linpeisensh closed 4 years ago

linpeisensh commented 4 years ago

Thank you for your excellent work!

We get (Batchsize, 256,256,2) as prediction now.

Could you tell me how can we convert this to RGB (to get the colorful image just like your results)?

Thanks!

nileshkulkarni commented 4 years ago

Hi @linpeisensh,

We created a texture map (which is the colorful image) and then we use the predicted UV values to pick colors from the texture map and overlay them on the input image. You can have any texture map you would like, here are the color maps I used to create the colorful images. Below is my snippet

'''
Args
    img : torch.FloatTensor  C X H x W  -- this is the image
    uv_map: torch.FloatTensor H x W x 2 -- uv predictions
    uv_img: torch.FloatTensor C x H x W  -- this the texture map
    mask: torch.FloatTensor  1 x  H x W -- mask for the object
Returns
    uv_rendering: torch.FloatTensor  C x H x W
'''
def sample_UV_contour(img, uv_map, uv_img, mask, real_img=True):
    img = img.unsqueeze(0)
    uv_map = uv_map.unsqueeze(0)
    uv_img = uv_img.unsqueeze(0)

    uv_sample = torch.nn.functional.grid_sample(uv_img, 2*uv_map-1).squeeze(0)
    uv_sample = uv_sample*mask + (1-mask)

    # alphas = contour_alphas(uv_img.shape[2], uv_img.shape[3], real_img).unsqueeze(0)
    alphas = contour_alphas(uv_img.shape[2], uv_img.shape[3], real_img).unsqueeze(0)* 0 + 1
    # pdb.set_trace()

    alpha_sample = torch.nn.functional.grid_sample(alphas, 2*uv_map-1).squeeze(0)
    # alpha_sample = alpha_sample*0.95 + 0.05
    alpha_sample = (alpha_sample>0.0).float()*0.7
    # alpha_sample = (alpha_sample > 0.9).float()
    alpha_sample = alpha_sample*mask

    if real_img:
        # uv_rendering = (uv_sample*alpha_sample)*1.0 + img.squeeze(0)*(1-alpha_sample)*0.3 * (mask) + img.squeeze(0)*(1-alpha_sample)* (1-mask)*0.3
        uv_rendering = (uv_sample*alpha_sample)*1.0 + img.squeeze(0)*(1-alpha_sample)
        uv_rendering = (uv_sample*alpha_sample)*1.0 +  img.squeeze(0)*(1-alpha_sample)*0.4 * (mask) + img.squeeze(0)*(1-alpha_sample)* (1-mask)
    else:
        uv_rendering = (uv_sample*alpha_sample) + (img.squeeze(0)*(1-alpha_sample))

    return uv_rendering

Hope this helps! Nilesh

linpeisensh commented 4 years ago

Thank you for your reply!

I will try it.

linpeisensh commented 4 years ago

Hi @nileshkulkarni, I want to know what is contour_alphas in this function. Thanks!

linpeisensh commented 4 years ago

And could you tell me whether I should resize uv_img? If yes, how can I do that? Since the shape of uv_img is 102410243 (HW3) and img is 3256256 (CHW)

nileshkulkarni commented 4 years ago

The contour_alphas are multiplied by 0 so you can just have it as a tensor of size 1 x H x W. Sorry for not including that function as it is not required anymore

Secondly, it is fine if you have the texture image of size 3 x 1024 x 1024 you don't need to resize it as the nn.grid_sample will handle it.

linpeisensh commented 4 years ago

OK, I get the ideal result now.

Thank you very much!

hangg7 commented 4 years ago

Hi @nileshkulkarni, how did you create these texture map?

nileshkulkarni commented 4 years ago

Hi, You can create these texture maps by treating the 3D (x,y,z) locations corresponding to each (u,v) coordinate in the texture image as the RGB value. Different permutations will give different color maps. In our case, we compute a 3D location on the unit sphere corresponding to a (u,v) value and use the (x,y,z) as the color value.

Best,