diff --git a/scripts/Depth2Disparity/depth_to_disparity.py b/scripts/Depth2Disparity/depth_to_disparity.py index f2ca50be08c25357901ac5b115a627696a966148..a0e3abef1f3339b7700595452986e0b1ba0e9694 100644 --- a/scripts/Depth2Disparity/depth_to_disparity.py +++ b/scripts/Depth2Disparity/depth_to_disparity.py @@ -8,6 +8,12 @@ import argparse import numpy as np import cv2 import os +import matplotlib.pyplot as plt +import matplotlib +matplotlib.use("TkAgg") # Use TkAgg for GUI rendering +import warnings +warnings.filterwarnings( "ignore", module = "matplotlib\..*" ) + class Depth2Disparity: @@ -34,33 +40,40 @@ class Depth2Disparity: if rel_depth_map is None: raise FileNotFoundError(f"Unable to load file: {rel_depth_path}") - # Normalize depth map - print("Normalizing depth map...") - rel_depth_map = rel_depth_map / 255.0 - # Convert relative to absolute depth print("Converting relative to absolute ...") abs_depth_map = self._relative2abs(rel_depth_map, coord1, dist1, coord2, dist2) - # Convert depth map to disparity map - print("Converting abs depth to disparity ...") - disparity_map = self._depth2disparity(abs_depth_map) + # Normalize depth map for saving + abs_depth_map_normalized = (abs_depth_map - abs_depth_map.min()) / (abs_depth_map.max() - abs_depth_map.min()) + abs_depth_map_normalized *= 255 + abs_depth_map_normalized = abs_depth_map_normalized.astype(np.uint8) - # Determine the output path for the disparity map - disparity_dir = os.path.dirname(rel_depth_path) - disparity_base = "depth_e.png" - disparity_path = os.path.join(disparity_dir, disparity_base) + # store absolute dpet map for debugging + abs_depth_path = os.path.join(os.path.dirname(rel_depth_path), "abs_depth.png") + cv2.imwrite(abs_depth_path, abs_depth_map_normalized) + print(f"Absolute depth map saved to: {abs_depth_path}\n") - # Check if an existing disparity map needs to be renamed - print(f"Checking for old depth map: {disparity_path}") - if os.path.exists(disparity_path): - old_depth_path = os.path.join(disparity_dir, "depth_e_old.png") - print(f"Renaming monodepth map to: {old_depth_path}") - os.rename(disparity_path, old_depth_path) - - # Save the new disparity map - print(f"Saving new disparity map to: {disparity_path}") - cv2.imwrite(disparity_path, (disparity_map * 255).astype(np.uint8)) + # Convert depth map to disparity map + #print("Converting abs depth to disparity ...") + #disparity_map = self._depth2disparity(abs_depth_map) + + ## Determine the output path for the disparity map + #disparity_dir = os.path.dirname(rel_depth_path) + #disparity_base = "disp_map.png"#"depth_e.png" + #disparity_path = os.path.join(disparity_dir, disparity_base) + + ## Check if an existing disparity map needs to be renamed + ##if os.path.exists(disparity_path): + ## old_depth_path = os.path.join(disparity_dir, "depth_e_old.png") + ## print(f"Renaming monodepth map to: {old_depth_path}") + ## # if pre-existing 'old depth map' exists, remove it + ## if os.path.exists(old_depth_path): + ## os.remove(old_depth_path) + ## os.rename(disparity_path, old_depth_path) + ## Save the new disparity map + ##print(f"Saving new disparity map to: {disparity_path}") + ##cv2.imwrite(disparity_path, (disparity_map).astype(np.uint8)) # Debug message for GUI or logs print("Complete") @@ -68,19 +81,43 @@ class Depth2Disparity: # add typing to this method defintion def _relative2abs(self, rel_depth_map, coord1: tuple, dist1: float, coord2: tuple, dist2: float): - print("\tStoring relative depth value at coordinates") + # Normalize depth map + print("Normalizing depth map...") + #rel_depth_map = rel_depth_map.astype(np.float32) / 255.0 + + plt.imshow(rel_depth_map, cmap='gray') + plt.colorbar() + plt.show() + + #print("\tStoring relative depth value at coordinates") # 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]] + rel_value1 = float(rel_depth_map[coord1[1], coord1[0]]) # (y, x) + rel_value2 = float(rel_depth_map[coord2[1], coord2[0]]) + print(f"rel1: {rel_value1}, rel2: {rel_value2}, dist1: {dist1}, dist2: {dist2}") + + #if rel_value1 > rel_value2: + # coord1, coord2 = coord2, coord1 + # dist1, dist2 = dist2, dist1 - print("\tPerforming linear transformation") + + #print("\tPerforming linear transformation") # Calculate the linear transformation: depth = a * rel_depth + b - a = (dist2 - dist1) / (rel_value2 - rel_value1) + numerator = dist2 - dist1 + denominator = rel_value2 - rel_value1 + a = numerator / denominator + print("Calculated a: ", a) b = dist1 - a * rel_value1 - - print("\tApplying transformation to entire depth map") + print("Calculated b: ", b) + + #print("\tApplying transformation to entire depth map") # Apply the transformation to the entire relative depth map abs_depth_map = a * rel_depth_map + b + print("Applied transformation to entire depth map") + + plt.imshow(abs_depth_map, cmap='gray') + plt.colorbar() + plt.show() + # this should not be normalised, the values in the array should equate to literal distances return abs_depth_map @@ -131,6 +168,10 @@ class Depth2Disparity: except ZeroDivisionError: disparity_map[i, j] = 0 + plt.imshow(disparity_map, cmap='gray') + plt.colorbar() + plt.show() + return disparity_map diff --git a/scripts/debug_tool/utils/image_handlers.py b/scripts/debug_tool/utils/image_handlers.py index e1cdcdd8a8d34721cff7464c7f4c975c00869a9f..7ecd65c534ce861284f9c07e77e13265b9579f02 100644 --- a/scripts/debug_tool/utils/image_handlers.py +++ b/scripts/debug_tool/utils/image_handlers.py @@ -49,7 +49,7 @@ def load_and_resize_image(image_path, max_size=800): new_size = (int(width * ratio), int(height * ratio)) img = cv2.resize(img, new_size, interpolation=cv2.INTER_AREA) - return img + return img, ratio except Exception as e: raise Exception(f"Error loading image: {str(e)}") @@ -65,7 +65,7 @@ def update_preview(preview_label, image_path, max_size=300, error_callback=None) """ if image_path and os.path.exists(image_path): try: - img = load_and_resize_image(image_path, max_size) + img, ratio = load_and_resize_image(image_path, max_size) pixmap = convert_cv_to_pixmap(img) preview_label.setPixmap(pixmap) except Exception as e: diff --git a/scripts/simple_tab.py b/scripts/simple_tab.py index ff0c9123eb027ec0dbe0a7454e8c356e8652c846..ab01fa76197df07c7f668cb11ab21dfd54b9477b 100644 --- a/scripts/simple_tab.py +++ b/scripts/simple_tab.py @@ -28,7 +28,8 @@ class PipelineWorker(QThread): super().__init__() self.tab = tab_instance self.distance_points = self.tab.distance_points # added distance points to class for disparity calculation - + self.ratio = self.tab.ratio # image scale ratio for reversal + def run(self): try: self.run_pipeline() @@ -96,17 +97,17 @@ class PipelineWorker(QThread): # execute function to convert depth to disparity real_depth_path = self.tab.depth.depth_output_path # depthmap path - coord1 = int(self.distance_points[0][0]), int(self.distance_points[0][1]) # x,y coordinates of point 1 + coord1 = int(self.distance_points[0][0] / self.ratio), int(self.distance_points[0][1] / self.ratio) # x,y coordinates of point 1 dist1 = float(self.distance_points[0][2]) # distance from camera to point 1 - coord2 = int(self.distance_points[1][0]), int(self.distance_points[1][1]) # x,y coordinates of point 2 + coord2 = int(self.distance_points[1][0] / self.ratio), int(self.distance_points[1][1] / self.ratio) # x,y coordinates of point 2 dist2 = float(self.distance_points[1][2]) # distance from camera to point 2 - print(f"{coord1}, {dist1}, {coord2}, {dist2}") + print(f"{coord1}, {dist1}, {coord2}, {dist2}. Ratio: {self.ratio}") convert_d2d = Depth2Disparity() print("Executing...") convert_d2d.execute(real_depth_path, coord1, dist1, coord2, dist2) - print("Saved disparity map to output directory: " + real_depth_path) + #print("Saved disparity map to output directory: " + real_depth_path) self.progress.emit("Completed Disparity Map Conversion") @@ -573,7 +574,7 @@ class SimpleTab(QWidget): update_preview(self.input_preview, file_path, error_callback=self.update_status) update_preview(self.distance_preview,file_path,max_size=1500) - pixmap = load_and_resize_image(file_path, 1500) + pixmap, self.ratio = load_and_resize_image(file_path, 1500) pixmap = convert_cv_to_pixmap(pixmap) self.distance_preview.setFixedSize(pixmap.size()) self.image_distance_group.show()