Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
AVVR-Pipeline-GDP4
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GDP Project 4
AVVR-Pipeline-GDP4
Commits
cd34cfa3
Commit
cd34cfa3
authored
4 months ago
by
tee1g21
Browse files
Options
Downloads
Patches
Plain Diff
Real depth working, disparity not currently feasible
parent
c1cc588d
No related branches found
No related tags found
1 merge request
!19
Gdp 4.5.1 implement disparity
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
scripts/Depth2Disparity/depth_to_disparity.py
+101
-61
101 additions, 61 deletions
scripts/Depth2Disparity/depth_to_disparity.py
with
101 additions
and
61 deletions
scripts/Depth2Disparity/depth_to_disparity.py
+
101
−
61
View file @
cd34cfa3
...
@@ -109,73 +109,113 @@ class Depth2Disparity:
...
@@ -109,73 +109,113 @@ class Depth2Disparity:
abs_depth_map
=
a
*
rel_depth_map
+
b
abs_depth_map
=
a
*
rel_depth_map
+
b
print
(
"
Applied transformation to entire depth map
"
)
print
(
"
Applied transformation to entire depth map
"
)
abs_depth_map
=
np
.
maximum
(
abs_depth_map
,
0.01
)
#
abs_depth_map = np.maximum(abs_depth_map, 0.01)
#
plt.imshow(abs_depth_map, cmap='gray')
plt
.
imshow
(
abs_depth_map
,
cmap
=
'
gray
'
)
#
plt.colorbar()
plt
.
colorbar
()
#
plt.show()
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
def
_depth2disparity
(
self
,
abs_depth_map
):
def
_depth2disparity
(
self
,
abs_depth_map
):
"""
# Get image dimensions
Convert absolute depth map to disparity map for 360-degree images.
# Get image dimensions
height
,
width
=
abs_depth_map
.
shape
Parameters:
abs_depth_map (numpy.ndarray): Absolute depth map with pixel values as distances in meters.
# Calculate step sizes
unit_w
=
2.0
/
width
# Longitude step size
Returns:
unit_h
=
1.0
/
height
# Latitude step size
disparity_map (numpy.ndarray): Disparity map with values representing disparity.
disparity_map_normalized (numpy.ndarray): Normalized disparity map for visualization.
# Initialize disparity map
"""
disparity_map
=
np
.
zeros_like
(
abs_depth_map
,
dtype
=
np
.
float32
)
# Define parameters for the conversion
height
,
width
=
abs_depth_map
.
shape
# Iterate over each pixel
for
i
in
range
(
height
):
# Latitude grid (θ_l)
# Calculate latitude angle for the row
epsilon
=
1e-6
theta_t
=
i
*
unit_h
*
np
.
pi
latitudes
=
np
.
linspace
(
-
np
.
pi
/
2
,
np
.
pi
/
2
,
height
)
latitudes_grid
=
np
.
tile
(
latitudes
[:,
np
.
newaxis
],
(
1
,
width
))
for
j
in
range
(
width
):
# Retrieve the absolute depth (r_t) for this pixel
# Clamp extreme latitudes to avoid instability
r_t
=
abs_depth_map
[
i
,
j
]
latitudes_grid
=
np
.
clip
(
latitudes_grid
,
-
np
.
pi
/
2
+
epsilon
,
np
.
pi
/
2
-
epsilon
)
# Avoid invalid or infinite depth values
plt
.
imshow
(
latitudes_grid
,
cmap
=
"
jet
"
)
if
r_t
<=
0
:
plt
.
colorbar
()
disparity_map
[
i
,
j
]
=
0
plt
.
title
(
"
Latitude Grid
"
)
continue
plt
.
show
()
try
:
self
.
baseline
=
0.176
# Compute denominator for the formula
epsilon
=
1e-8
# Small value to prevent instability
tan_theta_t
=
np
.
tan
(
theta_t
)
if
np
.
abs
(
np
.
cos
(
theta_t
))
>
epsilon
else
np
.
sign
(
np
.
sin
(
theta_t
))
*
np
.
inf
# Calculate angular disparity Δθ(x, y)
denominator
=
r_t
*
np
.
sin
(
theta_t
)
+
r_t
*
np
.
cos
(
theta_t
)
*
tan_theta_t
# Δθ(x, y) = arctan(B / (r_t * sin(θ_l) + r_t * cos(θ_l) * tan(θ_l))) - θ_l
epsilon
=
1e-6
# Small value to prevent instability
# Avoid invalid denominator values
denominator
=
(
if
denominator
<=
0
:
abs_depth_map
*
np
.
sin
(
latitudes_grid
)
+
disparity_map
[
i
,
j
]
=
0
abs_depth_map
*
np
.
cos
(
latitudes_grid
)
*
np
.
tan
(
latitudes_grid
)
+
continue
epsilon
)
# Calculate angular disparity
angular_disparity
=
np
.
arctan
(
self
.
baseline
/
denominator
)
-
latitudes_grid
angle_disp
=
np
.
arctan
(
self
.
baseline
/
denominator
)
-
theta_t
self
.
disp_scale
=
10
# Convert angular disparity to pixel disparity
self
.
disp_offset
=
np
.
median
(
angular_disparity
)
pixel_disp
=
(
angle_disp
/
(
unit_h
*
np
.
pi
)
-
self
.
disp_offset
)
*
self
.
disp_scale
disparity_map
[
i
,
j
]
=
pixel_disp
# Normalize the disparity map for saving
pixel_angle_scale
=
1
# Assume scale factor is 1 if not provided (can be adjusted if needed)
except
ZeroDivisionError
:
disparity_map
=
(
angular_disparity
/
pixel_angle_scale
-
self
.
disp_offset
)
*
self
.
disp_scale
disparity_map
[
i
,
j
]
=
0
# Normalize for visualization
plt
.
imshow
(
disparity_map
,
cmap
=
'
gray
'
)
disparity_map_normalized
=
(
disparity_map
-
disparity_map
.
min
())
/
(
disparity_map
.
max
()
-
disparity_map
.
min
())
plt
.
colorbar
()
disparity_map_normalized
*=
255
plt
.
show
()
disparity_map_normalized
=
disparity_map_normalized
.
astype
(
np
.
uint8
)
# Normalize disparity map for visualization
# Debugging visualization
disparity_map_normalized
=
(
disparity_map
-
disparity_map
.
min
())
/
(
disparity_map
.
max
()
-
disparity_map
.
min
())
plt
.
imshow
(
disparity_map
,
cmap
=
"
jet
"
)
disparity_map_normalized
*=
255
plt
.
colorbar
()
disparity_map_normalized
=
disparity_map_normalized
.
astype
(
np
.
uint8
)
plt
.
title
(
"
Disparity Map (Debugging)
"
)
plt
.
show
()
return
disparity_map
,
disparity_map_normalized
return
disparity_map
,
disparity_map_normalized
def
__depth2disparity
(
self
,
abs_depth_map
):
height
,
width
=
abs_depth_map
.
shape
unit_w
=
2.0
/
width
# Horizontal unit angle
unit_h
=
1.0
/
height
# Vertical unit angle
epsilon
=
1e-8
# Small value to prevent instability
# Latitude angles (theta_t) for each row
theta_t_grid
=
np
.
linspace
(
0
,
np
.
pi
,
height
).
reshape
(
height
,
1
)
# Ensure no instability in tangent calculation
tan_theta_t
=
np
.
tan
(
np
.
clip
(
theta_t_grid
,
-
np
.
pi
/
2
+
epsilon
,
np
.
pi
/
2
-
epsilon
))
# Broadcast depth map and latitude grid
r_t
=
abs_depth_map
# Absolute depth values (meters)
sin_theta_t
=
np
.
sin
(
theta_t_grid
)
cos_theta_t
=
np
.
cos
(
theta_t_grid
)
# Compute denominator
denominator
=
r_t
*
sin_theta_t
+
r_t
*
cos_theta_t
*
tan_theta_t
+
epsilon
# Avoid invalid denominator values
denominator
[
denominator
<=
0
]
=
epsilon
# Calculate angular disparity
angle_disp
=
np
.
arctan
(
self
.
baseline
/
denominator
)
-
theta_t_grid
# Convert angular disparity to pixel disparity
disparity_map
=
(
angle_disp
/
(
unit_h
*
np
.
pi
)
-
self
.
disp_offset
)
*
self
.
disp_scale
plt
.
imshow
(
disparity_map
,
cmap
=
'
gray
'
)
plt
.
colorbar
()
plt
.
show
()
# Normalize disparity map for visualization
disparity_map_normalized
=
(
disparity_map
-
disparity_map
.
min
())
/
(
disparity_map
.
max
()
-
disparity_map
.
min
())
disparity_map_normalized
*=
255
disparity_map_normalized
=
disparity_map_normalized
.
astype
(
np
.
uint8
)
return
disparity_map
,
disparity_map_normalized
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment