diff --git a/scripts/Depth2Disparity/abs2disparity.py b/scripts/Depth2Disparity/abs2disparity.py deleted file mode 100644 index 0f9a47040b00fbde97e57de078d5fd7ca97df30f..0000000000000000000000000000000000000000 --- a/scripts/Depth2Disparity/abs2disparity.py +++ /dev/null @@ -1,2 +0,0 @@ -# takes absolute depth map and converts to disparity map -# uses disparity and depth relationship \ No newline at end of file diff --git a/scripts/Depth2Disparity/depth2disparity.py b/scripts/Depth2Disparity/depth2disparity.py new file mode 100644 index 0000000000000000000000000000000000000000..95285fb13eaa4d8990c92b80931d4a1683bdf7fd --- /dev/null +++ b/scripts/Depth2Disparity/depth2disparity.py @@ -0,0 +1,72 @@ +# takes 2 pixel coordinates and corresponding distance values - and a relative depth map +# caculates relative to absolute function - if linear +# applies function to relative depth map to get absolute depth map +# then convert to disparity map using depth disparity formula + + +import argparse +import cv2 + +def relative2abs(rel_depth_map, coord1, dist1, coord2, dist2): + # Get the relative depth values at the two points + rel_value1 = rel_depth_map[coord1[1], coord1[0]] # (y, x) + rel_value2 = rel_depth_map[coord2[1], coord2[0]] + + # Calculate the linear transformation: depth = a * rel_depth + b + a = (dist2 - dist1) / (rel_value2 - rel_value1) + b = dist1 - a * rel_value1 + + # Apply the transformation to the entire relative depth map + abs_depth_map = a * rel_depth_map + b + + # this should not be normalised, the values in the array should equate to literal distances + return abs_depth_map + +def depth2disparity(_abs_depth_map): + # Calculate disparity map from depth map + + # random values + baseline = 0.54 # Baseline distance in meters + focal_length = 0.05 # Focal length in meters + + # Calculate disparity map + disparity_map = baseline * focal_length / _abs_depth_map + + # normalise disparity map between 0 and 255 + disparity_map = (disparity_map - disparity_map.min()) / (disparity_map.max() - disparity_map.min()) * 255 + + return disparity_map + + +if __name__ == "__main__": + # Set up argument parser + parser = argparse.ArgumentParser(description="Convert relative depth map to absolute depth map.") + parser.add_argument("rel_depth_map", type=str, help="Path to the relative depth map (image file).") + parser.add_argument("coord1", type=int, nargs=2, help="Pixel coordinates of the first point (x y).") + parser.add_argument("dist1", type=float, help="Absolute depth value at the first point.") + parser.add_argument("coord2", type=int, nargs=2, help="Pixel coordinates of the second point (x y).") + parser.add_argument("dist2", type=float, help="Absolute depth value at the second point.") + + # Parse arguments + args = parser.parse_args() + + # Load the relative depth map (dummy placeholder for now) + print(f"Loading relative depth map from: {args.rel_depth_map}") + rel_depth_map = cv2.imread(args.rel_depth_map, cv2.IMREAD_GRAYSCALE) # Load as grayscale + if rel_depth_map is None: + raise FileNotFoundError(f"Unable to load file: {args.rel_depth_map}") + + # Normalise depth map + rel_depth_map = rel_depth_map / 255.0 + + # Convert relative to absolute depth + abs_depth_map = relative2abs( + rel_depth_map, tuple(args.coord1), args.dist1, tuple(args.coord2), args.dist2 + ) + + # Convert depth map to disparity map + disparity_map = depth2disparity(abs_depth_map) + + # save disparity map + #cv2.imwrite(f"{args.rel_depth_map}-disparity.png", disparity_map) + \ No newline at end of file diff --git a/scripts/Depth2Disparity/relative2abs.py b/scripts/Depth2Disparity/relative2abs.py deleted file mode 100644 index a12d749f425c41810addd1eba28254bb536ab755..0000000000000000000000000000000000000000 --- a/scripts/Depth2Disparity/relative2abs.py +++ /dev/null @@ -1,4 +0,0 @@ -# takes 2 pixel coordinates and corresponding distance values - and a relative depth map -# caculates relative to absolute function - if linear -# applies function to relative depth map to get absolute depth map -# then convert to disparity map \ No newline at end of file