|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | +import argparse |
| 4 | + |
| 5 | + |
| 6 | +class DepthToNormalMap: |
| 7 | + """A class for converting a depth map image to a normal map image. |
| 8 | +
|
| 9 | +
|
| 10 | + Attributes: |
| 11 | + depth_map (ndarray): A numpy array representing the depth map image. |
| 12 | + max_depth (int): The maximum depth value in the depth map image. |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, depth_map_path: str, max_depth: int = 255) -> None: |
| 16 | + """Constructs a DepthToNormalMap object. |
| 17 | +
|
| 18 | + Args: |
| 19 | + depth_map_path (str): The path to the depth map image file. |
| 20 | + max_depth (int, optional): The maximum depth value in the depth map image. |
| 21 | + Defaults to 255. |
| 22 | +
|
| 23 | + Raises: |
| 24 | + ValueError: If the depth map image file cannot be read. |
| 25 | +
|
| 26 | + """ |
| 27 | + self.depth_map = cv2.imread(depth_map_path, cv2.IMREAD_UNCHANGED) |
| 28 | + |
| 29 | + if self.depth_map is None: |
| 30 | + raise ValueError( |
| 31 | + f"Could not read the depth map image file at {depth_map_path}" |
| 32 | + ) |
| 33 | + self.max_depth = max_depth |
| 34 | + |
| 35 | + def convert(self, output_path: str) -> None: |
| 36 | + """Converts the depth map image to a normal map image. |
| 37 | +
|
| 38 | + Args: |
| 39 | + output_path (str): The path to save the normal map image file. |
| 40 | +
|
| 41 | + """ |
| 42 | + rows, cols = self.depth_map.shape |
| 43 | + |
| 44 | + x, y = np.meshgrid(np.arange(cols), np.arange(rows)) |
| 45 | + x = x.astype(np.float32) |
| 46 | + y = y.astype(np.float32) |
| 47 | + |
| 48 | + # Calculate the partial derivatives of depth with respect to x and y |
| 49 | + dx = cv2.Sobel(self.depth_map, cv2.CV_32F, 1, 0) |
| 50 | + dy = cv2.Sobel(self.depth_map, cv2.CV_32F, 0, 1) |
| 51 | + |
| 52 | + # Compute the normal vector for each pixel |
| 53 | + normal = np.dstack((-dx, -dy, np.ones((rows, cols)))) |
| 54 | + norm = np.sqrt(np.sum(normal**2, axis=2, keepdims=True)) |
| 55 | + normal = np.divide(normal, norm, out=np.zeros_like(normal), where=norm != 0) |
| 56 | + |
| 57 | + # Map the normal vectors to the [0, 255] range and convert to uint8 |
| 58 | + normal = (normal + 1) * 127.5 |
| 59 | + normal = normal.clip(0, 255).astype(np.uint8) |
| 60 | + |
| 61 | + # Save the normal map to a file |
| 62 | + normal_bgr = cv2.cvtColor(normal, cv2.COLOR_RGB2BGR) |
| 63 | + cv2.imwrite(output_path, normal_bgr) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + parser = argparse.ArgumentParser(description="Convert depth map to normal map") |
| 68 | + parser.add_argument("--input", type=str, help="Path to depth map image") |
| 69 | + parser.add_argument( |
| 70 | + "--max_depth", type=int, default=255, help="Maximum depth value (default: 255)" |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + "--output_path", |
| 74 | + type=str, |
| 75 | + default="normal_map.png", |
| 76 | + help="Output path for normal map image (default: normal_map.png)", |
| 77 | + ) |
| 78 | + args = parser.parse_args() |
| 79 | + |
| 80 | + converter = DepthToNormalMap(args.input, max_depth=args.max_depth) |
| 81 | + converter.convert(args.output_path) |
0 commit comments