Skip to content
Snippets Groups Projects
Commit 263d026e authored by tee1g21's avatar tee1g21
Browse files

Fixed ratio scaling problem, absolute depth maps now generate well but completely rely on monodepth

parent 3eaafdd1
No related branches found
No related tags found
1 merge request!19Gdp 4.5.1 implement disparity
...@@ -8,6 +8,12 @@ import argparse ...@@ -8,6 +8,12 @@ import argparse
import numpy as np import numpy as np
import cv2 import cv2
import os 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: class Depth2Disparity:
...@@ -34,33 +40,40 @@ class Depth2Disparity: ...@@ -34,33 +40,40 @@ class Depth2Disparity:
if rel_depth_map is None: if rel_depth_map is None:
raise FileNotFoundError(f"Unable to load file: {rel_depth_path}") 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 # Convert relative to absolute depth
print("Converting relative to absolute ...") print("Converting relative to absolute ...")
abs_depth_map = self._relative2abs(rel_depth_map, coord1, dist1, coord2, dist2) abs_depth_map = self._relative2abs(rel_depth_map, coord1, dist1, coord2, dist2)
# Convert depth map to disparity map # Normalize depth map for saving
print("Converting abs depth to disparity ...") abs_depth_map_normalized = (abs_depth_map - abs_depth_map.min()) / (abs_depth_map.max() - abs_depth_map.min())
disparity_map = self._depth2disparity(abs_depth_map) abs_depth_map_normalized *= 255
abs_depth_map_normalized = abs_depth_map_normalized.astype(np.uint8)
# Determine the output path for the disparity map # store absolute dpet map for debugging
disparity_dir = os.path.dirname(rel_depth_path) abs_depth_path = os.path.join(os.path.dirname(rel_depth_path), "abs_depth.png")
disparity_base = "depth_e.png" cv2.imwrite(abs_depth_path, abs_depth_map_normalized)
disparity_path = os.path.join(disparity_dir, disparity_base) print(f"Absolute depth map saved to: {abs_depth_path}\n")
# Check if an existing disparity map needs to be renamed # Convert depth map to disparity map
print(f"Checking for old depth map: {disparity_path}") #print("Converting abs depth to disparity ...")
if os.path.exists(disparity_path): #disparity_map = self._depth2disparity(abs_depth_map)
old_depth_path = os.path.join(disparity_dir, "depth_e_old.png")
print(f"Renaming monodepth map to: {old_depth_path}") ## Determine the output path for the disparity map
os.rename(disparity_path, old_depth_path) #disparity_dir = os.path.dirname(rel_depth_path)
#disparity_base = "disp_map.png"#"depth_e.png"
# Save the new disparity map #disparity_path = os.path.join(disparity_dir, disparity_base)
print(f"Saving new disparity map to: {disparity_path}")
cv2.imwrite(disparity_path, (disparity_map * 255).astype(np.uint8)) ## 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 # Debug message for GUI or logs
print("Complete") print("Complete")
...@@ -68,19 +81,43 @@ class Depth2Disparity: ...@@ -68,19 +81,43 @@ class Depth2Disparity:
# add typing to this method defintion # add typing to this method defintion
def _relative2abs(self, rel_depth_map, coord1: tuple, dist1: float, coord2: tuple, dist2: float): 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 # Get the relative depth values at the two points
rel_value1 = rel_depth_map[coord1[1], coord1[0]] # (y, x) rel_value1 = float(rel_depth_map[coord1[1], coord1[0]]) # (y, x)
rel_value2 = rel_depth_map[coord2[1], coord2[0]] 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 # 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 b = dist1 - a * rel_value1
print("Calculated b: ", b)
print("\tApplying transformation to entire depth map")
#print("\tApplying transformation to entire depth map")
# Apply the transformation to the entire relative depth map # Apply the transformation to the entire relative depth map
abs_depth_map = a * rel_depth_map + b 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 # this should not be normalised, the values in the array should equate to literal distances
return abs_depth_map return abs_depth_map
...@@ -131,6 +168,10 @@ class Depth2Disparity: ...@@ -131,6 +168,10 @@ class Depth2Disparity:
except ZeroDivisionError: except ZeroDivisionError:
disparity_map[i, j] = 0 disparity_map[i, j] = 0
plt.imshow(disparity_map, cmap='gray')
plt.colorbar()
plt.show()
return disparity_map return disparity_map
...@@ -49,7 +49,7 @@ def load_and_resize_image(image_path, max_size=800): ...@@ -49,7 +49,7 @@ def load_and_resize_image(image_path, max_size=800):
new_size = (int(width * ratio), int(height * ratio)) new_size = (int(width * ratio), int(height * ratio))
img = cv2.resize(img, new_size, interpolation=cv2.INTER_AREA) img = cv2.resize(img, new_size, interpolation=cv2.INTER_AREA)
return img return img, ratio
except Exception as e: except Exception as e:
raise Exception(f"Error loading image: {str(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) ...@@ -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): if image_path and os.path.exists(image_path):
try: 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) pixmap = convert_cv_to_pixmap(img)
preview_label.setPixmap(pixmap) preview_label.setPixmap(pixmap)
except Exception as e: except Exception as e:
......
...@@ -28,7 +28,8 @@ class PipelineWorker(QThread): ...@@ -28,7 +28,8 @@ class PipelineWorker(QThread):
super().__init__() super().__init__()
self.tab = tab_instance self.tab = tab_instance
self.distance_points = self.tab.distance_points # added distance points to class for disparity calculation 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): def run(self):
try: try:
self.run_pipeline() self.run_pipeline()
...@@ -96,17 +97,17 @@ class PipelineWorker(QThread): ...@@ -96,17 +97,17 @@ class PipelineWorker(QThread):
# execute function to convert depth to disparity # execute function to convert depth to disparity
real_depth_path = self.tab.depth.depth_output_path # depthmap path 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 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 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() convert_d2d = Depth2Disparity()
print("Executing...") print("Executing...")
convert_d2d.execute(real_depth_path, coord1, dist1, coord2, dist2) 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") self.progress.emit("Completed Disparity Map Conversion")
...@@ -573,7 +574,7 @@ class SimpleTab(QWidget): ...@@ -573,7 +574,7 @@ class SimpleTab(QWidget):
update_preview(self.input_preview, file_path, update_preview(self.input_preview, file_path,
error_callback=self.update_status) error_callback=self.update_status)
update_preview(self.distance_preview,file_path,max_size=1500) 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) pixmap = convert_cv_to_pixmap(pixmap)
self.distance_preview.setFixedSize(pixmap.size()) self.distance_preview.setFixedSize(pixmap.size())
self.image_distance_group.show() self.image_distance_group.show()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment