Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
linpeisensh opened this issue May 26, 2020 · 8 comments
Closed

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

linpeisensh opened this issue May 26, 2020 · 8 comments

Comments

@linpeisensh
Copy link

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
Copy link
Owner

nileshkulkarni commented May 26, 2020

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
Copy link
Author

Thank you for your reply!

I will try it.

@linpeisensh
Copy link
Author

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

@linpeisensh
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

OK, I get the ideal result now.

Thank you very much!

@hangg7
Copy link

hangg7 commented Jun 3, 2020

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

@nileshkulkarni
Copy link
Owner

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants