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

Fixed: overflow errors in depth values #53

Merged
merged 6 commits into from
Oct 9, 2020
Merged

Conversation

alters-mit
Copy link
Member

Closes #52

Steps to reproduce

Run this controller:

from PIL import Image
import numpy as np
from tdw.controller import Controller
from tdw.tdw_utils import TDWUtils
from tdw.output_data import OutputData, Images

class DepthValues(Controller):
    def run(self):
        # Create the scene.
        # Set image encoding globals.
        self.start()
        commands = [TDWUtils.create_empty_room(12, 12)]
        # Create the avatar.
        commands.extend(TDWUtils.create_avatar(position={"x": 2.478, "y": 1.602, "z": 1.412},
                                               look_at=TDWUtils.VECTOR3_ZERO,
                                               avatar_id="a"))
        # Enable all pass masks. Request an image for this frame only.
        commands.extend([{"$type": "set_pass_masks",
                          "pass_masks": ["_depth"],
                          "avatar_id": "a"},
                         {"$type": "send_images",
                          "ids": ["a"],
                          "frequency": "once"}])
        # Add objects.
        commands.append(self.get_add_object("small_table_green_marble",
                                            position=TDWUtils.VECTOR3_ZERO,
                                            rotation=TDWUtils.VECTOR3_ZERO,
                                            object_id=0))
        commands.append(self.get_add_object("rh10",
                                            position={"x": 0.7, "y": 0, "z": 0.4},
                                            rotation={"x": 0, "y": 30, "z": 0},
                                            object_id=1))
        # Send the commands.
        resp = self.communicate(commands)
        # Save the images.
        for r in resp[:-1]:
            r_id = OutputData.get_data_type_id(r)
            if r_id == "imag":
                images = Images(r)
                for i in range(images.get_num_passes()):
                    if images.get_pass_mask(i) == "_depth":
                        depth_values = TDWUtils.get_depth_values(images.get_image(i))
                        depth_values /= (depth_values.max() / 255)
                        q = np.uint8(depth_values)
                        qq = np.uint8(np.empty((q.shape[0], q.shape[1], 3)))
                        qq[:, :, 0] = q
                        qq[:, :, 1] = q
                        qq[:, :, 2] = q
                        image = Image.fromarray(qq)
                        image.show()
        # Stop the build.
        self.communicate({"$type": "terminate"})


if __name__ == "__main__":
    DepthValues().run()

Result: Converting the depth values back to grayscale produces a reasonable-looking image (implying that the calculation is correct).

Changes

tdw module

TDWUtils

  • Fixed: Overflow of values in get_depth_values()

@@ -379,7 +379,7 @@ def get_depth_values(image: np.array) -> np.array:
"""

# Convert the image to a 2D image array.
image = np.array(Image.open(io.BytesIO(image)))
image = np.array(Image.open(io.BytesIO(image)), dtype=np.uint32)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make more sense to use np.float? Since depth is a real value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cffan There's no difference.

img_0 = np.array(Image.open(io.BytesIO(image)), dtype=np.float)
depth_0 = np.array((img_0[:, :, 0] * 256 * 256 + img_0[:, :, 1] * 256 + img_0[:, :, 2]) / (256 * 256 * 256) * 100.1)

img_1 = np.array(Image.open(io.BytesIO(image)), dtype=np.uint32)
depth_1 = np.array((img_1[:, :, 0] * 256 * 256 + img_1[:, :, 1] * 256 + img_1[:, :, 2]) / (256 * 256 * 256) * 100.1)

print(np.linalg.norm(depth_0 - depth_1))

Result: 0.0

@alters-mit alters-mit merged commit 5e5cc74 into master Oct 9, 2020
@alters-mit alters-mit deleted the depth_values_uint32 branch October 9, 2020 20:25
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

Successfully merging this pull request may close these issues.

get_depth_values causes overflow
2 participants