Skip to content

Commit

Permalink
Add an inverse function to construct intrinsic matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenvivek committed Feb 19, 2024
1 parent 45d3a23 commit 949428d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions diffdrr/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
'diffdrr.renderers._get_voxel': ('api/renderers.html#_get_voxel', 'diffdrr/renderers.py')},
'diffdrr.utils': { 'diffdrr.utils.get_focal_length': ('api/utils.html#get_focal_length', 'diffdrr/utils.py'),
'diffdrr.utils.get_principal_point': ('api/utils.html#get_principal_point', 'diffdrr/utils.py'),
'diffdrr.utils.make_intrinsic_matrix': ('api/utils.html#make_intrinsic_matrix', 'diffdrr/utils.py'),
'diffdrr.utils.parse_intrinsic_matrix': ('api/utils.html#parse_intrinsic_matrix', 'diffdrr/utils.py')},
'diffdrr.visualization': { 'diffdrr.visualization._make_camera_frustum_mesh': ( 'api/visualization.html#_make_camera_frustum_mesh',
'diffdrr/visualization.py'),
Expand Down
28 changes: 23 additions & 5 deletions diffdrr/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../notebooks/api/07_utils.ipynb.

# %% auto 0
__all__ = ['get_focal_length', 'get_principal_point', 'parse_intrinsic_matrix']
__all__ = ['get_focal_length', 'get_principal_point', 'parse_intrinsic_matrix', 'make_intrinsic_matrix']

# %% ../notebooks/api/07_utils.ipynb 4
def get_focal_length(
Expand All @@ -16,8 +16,8 @@ def get_focal_length(
# %% ../notebooks/api/07_utils.ipynb 5
def get_principal_point(
intrinsic, # Intrinsic matrix (3 x 3 tensor)
height: int, # Y-direction length
width: int, # X-direction length
height: int, # Y-direction length (in units pixels)
width: int, # X-direction length (in units pixels)
delx: float, # X-direction spacing (in units length)
dely: float, # Y-direction spacing (in units length)
):
Expand All @@ -28,11 +28,29 @@ def get_principal_point(
# %% ../notebooks/api/07_utils.ipynb 6
def parse_intrinsic_matrix(
intrinsic, # Intrinsic matrix (3 x 3 tensor)
height: int, # Y-direction length
width: int, # X-direction length
height: int, # Y-direction length (in units pixels)
width: int, # X-direction length (in units pixels)
delx: float, # X-direction spacing (in units length)
dely: float, # Y-direction spacing (in units length)
):
focal_length = get_focal_length(intrinsic, delx, dely)
x0, y0 = get_principal_point(intrinsic, height, width, delx, dely)
return focal_length, x0, y0

# %% ../notebooks/api/07_utils.ipynb 7
def make_intrinsic_matrix(
sdr: float, # Source-to-detector radius (in units length)
height: int, # Y-direction length (in units pixels)
width: int, # X-direction length (in units pixels)
delx: float, # X-direction spacing (in units length)
dely: float, # Y-direction spacing (in units length)
x0: float = 0.0, # Principal point x-coordinate (in units length)
y0: float = 0.0, # Principal point y-coordinate (in units length)
):
return torch.tensor(
[
[-2 * sdr / delx, 0.0, height / 2 - x0 / delx],
[0.0, -2 * sdr / dely, width / 2 - y0 / dely],
[0.0, 0.0, 1.0],
]
)
34 changes: 30 additions & 4 deletions notebooks/api/07_utils.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
"#| export\n",
"def get_principal_point(\n",
" intrinsic, # Intrinsic matrix (3 x 3 tensor)\n",
" height: int, # Y-direction length\n",
" width: int, # X-direction length\n",
" height: int, # Y-direction length (in units pixels)\n",
" width: int, # X-direction length (in units pixels)\n",
" delx: float, # X-direction spacing (in units length)\n",
" dely: float, # Y-direction spacing (in units length)\n",
"):\n",
Expand All @@ -93,8 +93,8 @@
"#| export\n",
"def parse_intrinsic_matrix(\n",
" intrinsic, # Intrinsic matrix (3 x 3 tensor)\n",
" height: int, # Y-direction length\n",
" width: int, # X-direction length\n",
" height: int, # Y-direction length (in units pixels)\n",
" width: int, # X-direction length (in units pixels)\n",
" delx: float, # X-direction spacing (in units length)\n",
" dely: float, # Y-direction spacing (in units length)\n",
"):\n",
Expand All @@ -103,6 +103,32 @@
" return focal_length, x0, y0"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9119195d-bedf-4076-a7c0-87dd359c2b35",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def make_intrinsic_matrix(\n",
" sdr: float, # Source-to-detector radius (in units length)\n",
" height: int, # Y-direction length (in units pixels)\n",
" width: int, # X-direction length (in units pixels)\n",
" delx: float, # X-direction spacing (in units length)\n",
" dely: float, # Y-direction spacing (in units length)\n",
" x0: float = 0.0, # Principal point x-coordinate (in units length)\n",
" y0: float = 0.0, # Principal point y-coordinate (in units length)\n",
"):\n",
" return torch.tensor(\n",
" [\n",
" [-2 * sdr / delx, 0.0, height / 2 - x0 / delx],\n",
" [0.0, -2 * sdr / dely, width / 2 - y0 / dely],\n",
" [0.0, 0.0, 1.0],\n",
" ]\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 949428d

Please sign in to comment.