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
Merge requests
!15
GDP_4.4.6-Depth2Disparity
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
GDP_4.4.6-Depth2Disparity
GDP_4.4.6-Depth2Disparity
into
master
Overview
0
Commits
4
Changes
1
Merged
Tom Evans
requested to merge
GDP_4.4.6-Depth2Disparity
into
master
5 months ago
Overview
0
Commits
4
Changes
1
Expand
0
0
Merge request reports
Compare
master
master (base)
and
latest version
latest version
b3b028a8
4 commits,
5 months ago
1 file
+
105
−
0
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
scripts/Depth2Disparity/depth2disparity.py
0 → 100644
+
105
−
0
Options
# 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
numpy
as
np
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
,
baseline
=
0.176
,
disp_scale
=
2.0
,
disp_offset
=-
120
):
# Get image dimensions
height
,
width
=
abs_depth_map
.
shape
# Calculate angular coordinates for each pixel
unit_w
=
2.0
/
width
# Longitude step size
unit_h
=
1.0
/
height
# Latitude step size
# Initialize disparity map
disparity_map
=
np
.
zeros_like
(
abs_depth_map
,
dtype
=
np
.
float32
)
for
i
in
range
(
height
):
theta_t
=
i
*
unit_h
*
np
.
pi
# Latitude angle for the row
for
j
in
range
(
width
):
# Longitude angle (not strictly needed for disparity calculation)
phi
=
j
*
unit_w
*
np
.
pi
# Retrieve the absolute depth (r_t) for this pixel
r_t
=
abs_depth_map
[
i
,
j
]
# Avoid invalid or infinite depth values
if
r_t
<=
0
:
disparity_map
[
i
,
j
]
=
0
continue
try
:
# Compute denominator with stability checks
epsilon
=
1e-8
# Small value to prevent division 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
denominator
=
r_t
*
np
.
sin
(
theta_t
)
+
r_t
*
np
.
cos
(
theta_t
)
*
tan_theta_t
if
denominator
<=
0
:
disparity_map
[
i
,
j
]
=
0
continue
# Calculate angular disparity (d)
angle_disp
=
np
.
arctan
(
baseline
/
denominator
)
-
theta_t
# Convert angular disparity to pixel disparity
disparity_map
[
i
,
j
]
=
(
angle_disp
/
(
unit_h
*
np
.
pi
)
-
disp_offset
)
*
disp_scale
except
ZeroDivisionError
:
disparity_map
[
i
,
j
]
=
0
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
Loading