-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: New methods to directly plot a TexturesUV map with its used points, using PIL and matplotlib. Reviewed By: gkioxari Differential Revision: D23782968 fbshipit-source-id: 692970857b5be13a35a3175dc82ac03963a73555
- Loading branch information
1 parent
b149bbf
commit aa4cc0a
Showing
6 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. | ||
|
||
from .plotly_vis import AxisArgs, Lighting, plot_batch_individually, plot_scene | ||
from .texture_vis import texturesuv_image_matplotlib, texturesuv_image_PIL | ||
|
||
|
||
__all__ = [k for k in globals().keys() if not k.startswith("_")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. | ||
from typing import Optional | ||
|
||
import numpy as np | ||
from PIL import Image, ImageDraw | ||
from pytorch3d.renderer.mesh import TexturesUV | ||
|
||
|
||
def texturesuv_image_matplotlib( | ||
texture: TexturesUV, | ||
*, | ||
texture_index: int = 0, | ||
radius: float = 1, | ||
color=(1.0, 0.0, 0.0), | ||
subsample: Optional[int] = 10000, | ||
origin: str = "upper", | ||
): | ||
""" | ||
Plot the texture image for one element of a TexturesUV with | ||
matplotlib together with verts_uvs positions circled. | ||
In particular a value in verts_uvs which is never referenced | ||
in faces_uvs will still be plotted. | ||
This is for debugging purposes, e.g. to align the map with | ||
the uv coordinates. In particular, matplotlib | ||
is used which is not an official dependency of PyTorch3D. | ||
Args: | ||
texture: a TexturesUV object with one mesh | ||
texture_index: index in the batch to plot | ||
radius: plotted circle radius in pixels | ||
color: any matplotlib-understood color for the circles. | ||
subsample: if not None, number of points to plot. | ||
Otherwise all points are plotted. | ||
origin: "upper" or "lower" like matplotlib.imshow | ||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
from matplotlib.patches import Circle | ||
|
||
texture_image = texture.maps_padded() | ||
centers = texture.centers_for_image(index=texture_index).numpy() | ||
|
||
ax = plt.gca() | ||
ax.imshow(texture_image[texture_index].detach().cpu().numpy(), origin=origin) | ||
|
||
n_points = centers.shape[0] | ||
if subsample is None or n_points <= subsample: | ||
indices = range(n_points) | ||
else: | ||
indices = np.random.choice(n_points, subsample, replace=False) | ||
for i in indices: | ||
# setting clip_on=False makes it obvious when | ||
# we have UV coordinates outside the correct range | ||
ax.add_patch(Circle(centers[i], radius, color=color, clip_on=False)) | ||
|
||
|
||
def texturesuv_image_PIL( | ||
texture: TexturesUV, | ||
*, | ||
texture_index: int = 0, | ||
radius: float = 1, | ||
color="red", | ||
subsample: Optional[int] = 10000, | ||
): | ||
""" | ||
Return a PIL image of the texture image of one element of the batch | ||
from a TexturesUV, together with the verts_uvs positions circled. | ||
In particular a value in verts_uvs which is never referenced | ||
in faces_uvs will still be plotted. | ||
This is for debugging purposes, e.g. to align the map with | ||
the uv coordinates. In particular, matplotlib | ||
is used which is not an official dependency of PyTorch3D. | ||
Args: | ||
texture: a TexturesUV object with one mesh | ||
texture_index: index in the batch to plot | ||
radius: plotted circle radius in pixels | ||
color: any PIL-understood color for the circles. | ||
subsample: if not None, number of points to plot. | ||
Otherwise all points are plotted. | ||
Returns: | ||
PIL Image object. | ||
""" | ||
|
||
centers = texture.centers_for_image(index=texture_index).numpy() | ||
texture_image = texture.maps_padded() | ||
texture_array = (texture_image[texture_index] * 255).cpu().numpy().astype(np.uint8) | ||
|
||
image = Image.fromarray(texture_array) | ||
draw = ImageDraw.Draw(image) | ||
|
||
n_points = centers.shape[0] | ||
if subsample is None or n_points <= subsample: | ||
indices = range(n_points) | ||
else: | ||
indices = np.random.choice(n_points, subsample, replace=False) | ||
|
||
for i in indices: | ||
x = centers[i][0] | ||
y = centers[i][1] | ||
draw.ellipse([(x - radius, y - radius), (x + radius, y + radius)], fill=color) | ||
|
||
return image |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters