ProjectiveGeometry23 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- projectivegeometry23-0.1.0/LICENSE +201 -0
- projectivegeometry23-0.1.0/MANIFEST.in +3 -0
- projectivegeometry23-0.1.0/PKG-INFO +142 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23/__init__.py +1 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23/central_projection.py +249 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23/estimation.py +71 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23/homography.py +136 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23/pluecker.py +161 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23/source_detector_geometry.py +103 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23/svg_utils.py +141 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23/utils.py +238 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23.egg-info/PKG-INFO +142 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23.egg-info/SOURCES.txt +19 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23.egg-info/dependency_links.txt +1 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23.egg-info/requires.txt +5 -0
- projectivegeometry23-0.1.0/ProjectiveGeometry23.egg-info/top_level.txt +1 -0
- projectivegeometry23-0.1.0/README.md +111 -0
- projectivegeometry23-0.1.0/pyproject.toml +6 -0
- projectivegeometry23-0.1.0/setup.cfg +4 -0
- projectivegeometry23-0.1.0/setup.py +28 -0
- projectivegeometry23-0.1.0/tests/test_basic.py +34 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ProjectiveGeometry23
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Projective geometry in 2D and 3D with homogeneous and Plücker coordinates, projection matrices, and visualization.
|
|
5
|
+
Home-page: https://github.com/aaichert/ProjectiveGeometry23
|
|
6
|
+
Author: Andre Aichert
|
|
7
|
+
Author-email: aaichert@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.7
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: numpy
|
|
16
|
+
Requires-Dist: scipy
|
|
17
|
+
Provides-Extra: svg
|
|
18
|
+
Requires-Dist: svg_snip; extra == "svg"
|
|
19
|
+
Dynamic: author
|
|
20
|
+
Dynamic: author-email
|
|
21
|
+
Dynamic: classifier
|
|
22
|
+
Dynamic: description
|
|
23
|
+
Dynamic: description-content-type
|
|
24
|
+
Dynamic: home-page
|
|
25
|
+
Dynamic: license
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
Dynamic: provides-extra
|
|
28
|
+
Dynamic: requires-dist
|
|
29
|
+
Dynamic: requires-python
|
|
30
|
+
Dynamic: summary
|
|
31
|
+
|
|
32
|
+
# ProjectiveGeometry23
|
|
33
|
+
## Projective Geometry of Two- and Three-space
|
|
34
|
+
|
|
35
|
+
`ProjectiveGeometry23` is a collection of numpy-based utilities for projective geometry of real two- and three-space, including homogeneous coordinates of point, lines and planes, Plücker coordinates and projection matrices.
|
|
36
|
+
|
|
37
|
+
The package has been converted from an existing C++ implementation [LibProjectiveGeometry](https://github.com/aaichert/LibProjectiveGeometry), which is well-tested and obviously faster.
|
|
38
|
+
|
|
39
|
+
The main use of this code is for the visualization or X-Ray source-detector geometries but applied generally to geometric computer vision problems.
|
|
40
|
+
|
|
41
|
+
Features:
|
|
42
|
+
- Computing with points, lines and planes
|
|
43
|
+
- Plücker coordinates, Plücker matrices
|
|
44
|
+
- Projection matrices, intrinsic and extrinsic parameters
|
|
45
|
+
- Decomposition of projection matrices, backprojection
|
|
46
|
+
- Visualization of X-Ray source-detector geometries
|
|
47
|
+
- (WIP) estimation based on direct linear transform, X-ray calibraion
|
|
48
|
+
|
|
49
|
+
General recommendation for better readibility of outputs:
|
|
50
|
+
|
|
51
|
+
```py
|
|
52
|
+
from rich import print
|
|
53
|
+
np.set_printoptions(suppress=True)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Example of an interactive visualization, using the optional `svg_snip` package:
|
|
57
|
+
|
|
58
|
+
```py
|
|
59
|
+
from svg_snip.Jupyter import CanvasWithOverlay
|
|
60
|
+
from svg_snip.Composer import Composer
|
|
61
|
+
import svg_snip.Elements as e2d
|
|
62
|
+
import svg_snip.Elements3D as e3d
|
|
63
|
+
|
|
64
|
+
from ProjectiveGeometry23.homography import rotation_x, rotation_z, scale
|
|
65
|
+
from ProjectiveGeometry23.svg_utils import svg_source_detector, svg_world_geometry
|
|
66
|
+
|
|
67
|
+
vis = CanvasWithOverlay(int(target.image_size[0]), int(target.image_size[1]))
|
|
68
|
+
|
|
69
|
+
# World transformation
|
|
70
|
+
ax, az = 0.5, 0.5
|
|
71
|
+
s = 0.2
|
|
72
|
+
|
|
73
|
+
def handle_draw(vis):
|
|
74
|
+
global ax
|
|
75
|
+
global az
|
|
76
|
+
x,y = vis.mouse_state.pos()
|
|
77
|
+
svg = Composer((vis.w, vis.h))
|
|
78
|
+
|
|
79
|
+
svg.add(svg_world_geometry)
|
|
80
|
+
svg.add(svg_source_detector, projection=target,
|
|
81
|
+
draw_on_detector=svg_world_geometry,
|
|
82
|
+
label_source='C0', label_detector='I0(u,v)')
|
|
83
|
+
|
|
84
|
+
svg.add(e2d.star, x=x, y=y, size=8,
|
|
85
|
+
fill="red" if vis.mouse_state.clicked else "blue")
|
|
86
|
+
|
|
87
|
+
svg.add(e2d.text, x=10, y=20, content=f'ax={ax:.3} az={az:.3}')
|
|
88
|
+
|
|
89
|
+
T = scale(s) @ rotation_x(ax) @ rotation_z(az)
|
|
90
|
+
raw_svg_code = svg.render(P=target.P@T)
|
|
91
|
+
vis.html_overlay.value = raw_svg_code
|
|
92
|
+
|
|
93
|
+
if vis.mouse_state.clicked:
|
|
94
|
+
az += vis.mouse_state.dx * 0.01
|
|
95
|
+
ax += vis.mouse_state.dy * 0.01
|
|
96
|
+
|
|
97
|
+
vis.handle_draw = handle_draw
|
|
98
|
+
|
|
99
|
+
vis.display()
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+

|
|
104
|
+
|
|
105
|
+
## Installation
|
|
106
|
+
|
|
107
|
+
### Using pip
|
|
108
|
+
You can install `ProjectiveGeometry23` using pip:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
pip install ProjectiveGeometry23
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
### Using `setup.py`
|
|
116
|
+
|
|
117
|
+
```sh
|
|
118
|
+
git clone https://github.com/aaichert/ProjectiveGeometry23
|
|
119
|
+
cd ProjectiveGeometry23
|
|
120
|
+
python setup.py install
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Testing and Publication on PyPy
|
|
124
|
+
|
|
125
|
+
Two useful code snippets
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
python -m unittest discover tests
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
python setup.py sdist bdist_wheel
|
|
133
|
+
pip install twine
|
|
134
|
+
twine upload dist/*
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### References
|
|
138
|
+
|
|
139
|
+
1. LibProjectiveGeometry (c++). GitHub https://github.com/aaichert/EpipolarConsistency/tree/master/code/LibProjectiveGeometry
|
|
140
|
+
2. Hartley, Richard, and Andrew Zisserman. Multiple view geometry in computer vision. Cambridge university press, 2003. https://www.robots.ox.ac.uk/~vgg/hzbook/
|
|
141
|
+
3. Coxeter, Harold Scott Macdonald. Projective geometry. Springer Science & Business Media, 2003.
|
|
142
|
+
4. Stolfi, Jorge. "Oriented projective geometry." Proceedings of the third annual symposium on Computational geometry. 1987.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# This file makes ProjectiveGeometry23 a Python package.
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Linear projection from real projective three-space to real projective two-space.
|
|
3
|
+
Representations of a pinhole camera as Projection matrix optionally with additional information of image size and pixelation.
|
|
4
|
+
|
|
5
|
+
Author: André Aichert
|
|
6
|
+
Date: June 22, 2023
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
import scipy
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# recommended:
|
|
14
|
+
# from rich import print
|
|
15
|
+
# np.set_printoptions(suppress=True)
|
|
16
|
+
|
|
17
|
+
class ProjectionMatrix:
|
|
18
|
+
def __init__(self, P, image_size=(400, 300), pixel_spacing=1.0):
|
|
19
|
+
"""Pinhole projection model as 3x4 projection matrix with optional
|
|
20
|
+
information of detector size and pixel spacing [mm per px]."""
|
|
21
|
+
self.P = np.array(P).reshape((3, 4))
|
|
22
|
+
self.image_size = np.array(image_size)
|
|
23
|
+
self.pixel_spacing = pixel_spacing
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@classmethod
|
|
27
|
+
def perspective_look_at(cls, eye, center=np.array([0, 0, 0]), image_size=(400, 300), fovy_rad=0.5, pixel_spacing=1.0):
|
|
28
|
+
"""Create a ProjectionMatrix that looks at a center point from and eye point given field-of-view and image size.
|
|
29
|
+
|
|
30
|
+
Extrinsic parameters as rotation matix R and translation vector t:
|
|
31
|
+
- eye: Position [X,Y,Z] of the camera (camera center)
|
|
32
|
+
- center: Point the camera is looking at (target position)
|
|
33
|
+
|
|
34
|
+
Intrinsic parameters of a pinhole camera as a 3x3 matrix K.
|
|
35
|
+
- fovy_rad: Field of view angle in radians (vertical)
|
|
36
|
+
- image_size: Width and height of the image sensor
|
|
37
|
+
|
|
38
|
+
Returns ProjectionMatrix P=K[R t] with given image_size
|
|
39
|
+
|
|
40
|
+
TODO: optional pixel_spacing should probably affect intrinsics
|
|
41
|
+
"""
|
|
42
|
+
# Up vector defining the camera's orientation
|
|
43
|
+
up=np.array([0, 1, 0])
|
|
44
|
+
|
|
45
|
+
# Intrinsics
|
|
46
|
+
w, h = image_size
|
|
47
|
+
tanfov2 = 2 * np.tan(0.5 * fovy_rad)
|
|
48
|
+
fx, fy = h / tanfov2, h / tanfov2
|
|
49
|
+
cx, cy = 0.5 * w, 0.5 * h
|
|
50
|
+
|
|
51
|
+
K = np.array([[fx, 0, cx],
|
|
52
|
+
[0, fy, cy],
|
|
53
|
+
[0, 0, 1]])
|
|
54
|
+
|
|
55
|
+
# Extrinsics
|
|
56
|
+
fwd = (center - eye) / np.linalg.norm(center - eye)
|
|
57
|
+
left = np.cross(up, fwd)
|
|
58
|
+
left /= np.linalg.norm(left)
|
|
59
|
+
new_up = np.cross(fwd, left)
|
|
60
|
+
|
|
61
|
+
R = np.array([left, new_up, -fwd])
|
|
62
|
+
t = -R.dot(eye)
|
|
63
|
+
|
|
64
|
+
# Compose projection matrix
|
|
65
|
+
P = np.zeros((3, 4))
|
|
66
|
+
P[:, :3] = K @ R
|
|
67
|
+
P[:, 3] = K @ t
|
|
68
|
+
|
|
69
|
+
return cls(P, image_size, pixel_spacing)
|
|
70
|
+
|
|
71
|
+
def to_dict(self):
|
|
72
|
+
return {
|
|
73
|
+
"P": self.P.tolist(),
|
|
74
|
+
"image_size": self.image_size.to_dict(),
|
|
75
|
+
"pixel_spacing": self.pixel_spacing
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
def to_ompl(self, with_geometry=True):
|
|
79
|
+
def f2str(f):
|
|
80
|
+
return np.format_float_positional(f, trim='-')
|
|
81
|
+
s = "; ".join([" ".join([f2str(x) for x in row]) for row in self.P])
|
|
82
|
+
if with_geometry:
|
|
83
|
+
return f'#> spacing="{self.pixel_spacing}" detector_size_px="{self.image_size}"\n[{s}]'
|
|
84
|
+
else:
|
|
85
|
+
return f'[{s}]'
|
|
86
|
+
|
|
87
|
+
@classmethod
|
|
88
|
+
def from_dict(cls, data):
|
|
89
|
+
return cls(
|
|
90
|
+
data["P"],
|
|
91
|
+
np.array(data["image_size"]),
|
|
92
|
+
data["pixel_spacing"]
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
def to_json(self):
|
|
96
|
+
return json.dumps(self.to_dict(), indent=2)
|
|
97
|
+
|
|
98
|
+
@classmethod
|
|
99
|
+
def from_json(cls, json_str):
|
|
100
|
+
data = json.loads(json_str)
|
|
101
|
+
return cls.from_dict(data)
|
|
102
|
+
|
|
103
|
+
def __repr__(self):
|
|
104
|
+
return f"ProjectionMatrix:\n{self.P}\nImage Size: {self.image_size}\nPixel Spacing: {self.pixel_spacing}"
|
|
105
|
+
|
|
106
|
+
def principal_ray(self):
|
|
107
|
+
return self.P[2][0:3]
|
|
108
|
+
|
|
109
|
+
def normalize(self):
|
|
110
|
+
norm_m3 = np.linalg.norm(self.principal_ray())
|
|
111
|
+
detM = np.linalg.det(self.P[0:3, 0:3])
|
|
112
|
+
if detM < 0:
|
|
113
|
+
norm_m3 *= -1;
|
|
114
|
+
self.P /= norm_m3
|
|
115
|
+
|
|
116
|
+
def getCenterOfProjection(self):
|
|
117
|
+
_, _, V = np.linalg.svd(self.P)
|
|
118
|
+
C = V[-1, :4]
|
|
119
|
+
if C[3] < -1e-12 or C[3] > 1e-12:
|
|
120
|
+
C = C / C[3] # By definition: Camera centers are always positive points.
|
|
121
|
+
return C.reshape(-1, 1)
|
|
122
|
+
|
|
123
|
+
def getPrincipalPoint(self):
|
|
124
|
+
pp = self.P[:3, :3] @ self.P[2, :3].T
|
|
125
|
+
return pp / pp[2]
|
|
126
|
+
|
|
127
|
+
def getPrincipalRay(self):
|
|
128
|
+
m3 = self.P[2, :3]
|
|
129
|
+
if np.linalg.norm(m3) > 1e-12:
|
|
130
|
+
m3 = m3 / np.linalg.norm(m3)
|
|
131
|
+
return m3.reshape(-1, 1)
|
|
132
|
+
|
|
133
|
+
def getFocalLengthPx(self):
|
|
134
|
+
"""Compute the focal length in pixels (diagonal entries of K in P=K[R t] )."""
|
|
135
|
+
self.normalize()
|
|
136
|
+
|
|
137
|
+
m1 = self.P[0, :3]
|
|
138
|
+
m2 = self.P[1, :3]
|
|
139
|
+
m3 = self.P[2, :3]
|
|
140
|
+
|
|
141
|
+
U = np.cross(m3, m2) / np.linalg.norm(np.cross(m3, m2))
|
|
142
|
+
V = np.cross(m3, m1) / np.linalg.norm(np.cross(m3, m1))
|
|
143
|
+
|
|
144
|
+
focal_length_u_px = np.dot(m1, np.cross(V, m3))
|
|
145
|
+
# focal_length_v_px = np.dot(m2, np.cross(U, m3)) # should be identical
|
|
146
|
+
|
|
147
|
+
return focal_length_u_px
|
|
148
|
+
|
|
149
|
+
def getDetectorAxisDirections(self):
|
|
150
|
+
"""Compute the two three-points where the image u- and v-axes meet infinity. Scaled to world coordinates."""
|
|
151
|
+
self.normalize()
|
|
152
|
+
|
|
153
|
+
m1 = self.P[0, :3]
|
|
154
|
+
m2 = self.P[1, :3]
|
|
155
|
+
m3 = self.P[2, :3]
|
|
156
|
+
|
|
157
|
+
U = np.append(np.cross(m3, m2) / np.linalg.norm(np.cross(m3, m2)), 0)
|
|
158
|
+
V = np.append(np.cross(m3, m1) / np.linalg.norm(np.cross(m3, m1)), 0)
|
|
159
|
+
|
|
160
|
+
return U, V
|
|
161
|
+
|
|
162
|
+
def getDetectorAxisDirectionsPx(self):
|
|
163
|
+
"""Compute the two three-points where the image u- and v-axes
|
|
164
|
+
meet infinity. Scaled to pixels."""
|
|
165
|
+
U, V = getDetectorAxisDirections()
|
|
166
|
+
return U * self.pixel_spacing, V * self.pixel_spacing
|
|
167
|
+
|
|
168
|
+
def getDetectorPlane(self, imageVPointsUp=False):
|
|
169
|
+
""" Decomposes the projection matrix to compute the equation of the
|
|
170
|
+
image/detector plane. Assumes rectangular pixels. This is identical to
|
|
171
|
+
the principal place shifted by the focal length. For left-handed
|
|
172
|
+
coordinate systems, set imageVPointsUp to True.
|
|
173
|
+
Returns the image detector plane in Hessian normal form and a boolean
|
|
174
|
+
indicating whether imageVPointsUp appears to be set correctly. """
|
|
175
|
+
# TODO: alternative (that won't know when handedness is off)
|
|
176
|
+
# focal_length_u_mm = self.getFocalLength() * self.pixel_spacing
|
|
177
|
+
K, R, t, appears_flipped = self.decomposition(imageVPointsUp)
|
|
178
|
+
focal_length_u_mm = K[0, 0] * self.pixel_spacing
|
|
179
|
+
principal_plane = self.P[2, :4].copy()
|
|
180
|
+
principal_plane /= np.linalg.norm(principal_plane[:3])
|
|
181
|
+
principal_plane[3] -= focal_length_u_mm
|
|
182
|
+
return principal_plane, appears_flipped
|
|
183
|
+
|
|
184
|
+
def decomposition(self, imageVPointsUp = False):
|
|
185
|
+
"""Decompose Projection Matrix into K[R|t] using RQ-decomposition.
|
|
186
|
+
Returns K, R, t and a boolean indicating if R is left-handed.
|
|
187
|
+
For right-handed world coordinates implies imageVPointsUp is wrong."""
|
|
188
|
+
# Compute RQ decomposition of leftmost 3x3 sub-matrix of P
|
|
189
|
+
R, Q = scipy.linalg.rq(self.P[:3, :3].copy())
|
|
190
|
+
K = R
|
|
191
|
+
R = Q
|
|
192
|
+
# make diagonal of K positive
|
|
193
|
+
S = np.eye(3)
|
|
194
|
+
if K[0, 0] < 0:
|
|
195
|
+
S[0, 0] = -1
|
|
196
|
+
if K[1, 1] < 0:
|
|
197
|
+
S[1, 1] = -1
|
|
198
|
+
if imageVPointsUp:
|
|
199
|
+
S[1, 1] *= -1
|
|
200
|
+
if K[2, 2] < 0:
|
|
201
|
+
S[2, 2] = -1
|
|
202
|
+
K = K @ S
|
|
203
|
+
R = S @ R
|
|
204
|
+
# Force zero elements in K
|
|
205
|
+
K[1, 0] = K[2, 0] = K[2, 1] = 0
|
|
206
|
+
# Scale
|
|
207
|
+
K *= 1.0 / K[2, 2]
|
|
208
|
+
t = np.linalg.solve(K, self.P[:3, 3])
|
|
209
|
+
# EXPLANATION of appears_flipped:
|
|
210
|
+
# In oriented projective geometry, for a visible point x=P*X the
|
|
211
|
+
# homogeneous coordinate will be positive. A negative homogeneous
|
|
212
|
+
# coordinate implies "behind the camera". You can thus change the
|
|
213
|
+
# direction in which a camera is facing by a multiplication of the
|
|
214
|
+
# projection matrix with -1.
|
|
215
|
+
# This is relevant for the decomposition, because in practice, the
|
|
216
|
+
# coordinate system can be left-handed. In this case, the image
|
|
217
|
+
# "rotation" matrix has negative determinant. This may actually be the
|
|
218
|
+
# case, e.g. for the way pixels are stored in a BMP file, some window
|
|
219
|
+
# coordinates or Siemens 'Leonardo' style raw images. In most cases,
|
|
220
|
+
# though, that means you got the oriantation wrong.
|
|
221
|
+
# You can check using sourceDetectorGeometry. Only if your pixel
|
|
222
|
+
# spacing and imageVPointsUp values are correct, will the image plane
|
|
223
|
+
# and detector origin be on the opposite side of the object w.r.t.
|
|
224
|
+
# source position (as it should be... duh).
|
|
225
|
+
appears_flipped = np.linalg.det(R) > 0
|
|
226
|
+
return K, R, t, appears_flipped
|
|
227
|
+
|
|
228
|
+
def pseudoinverse(self):
|
|
229
|
+
return np.linalg.pinv(self.P)
|
|
230
|
+
|
|
231
|
+
def backproject(self, x):
|
|
232
|
+
"""Compute Plücker Coordinates of a backprojection for a 2D image point x in homogeneous coordinates.
|
|
233
|
+
if x is a list, make sure to convert o a column vector np.array(x).reshape(-1, 1)."""
|
|
234
|
+
Pinv = self.pseudoinverse()
|
|
235
|
+
return Pinv @ x
|
|
236
|
+
|
|
237
|
+
def computeFundamentalMatrix(self, P1):
|
|
238
|
+
"""Compute fundamental matrix from two projection matrices. Pseudoinverse-based implementation"""
|
|
239
|
+
if isinstance(P1, ProjectionMatrix):
|
|
240
|
+
P1 = P1.P
|
|
241
|
+
C0 = self.getCenterOfProjection()
|
|
242
|
+
e1 = P1 @ C0
|
|
243
|
+
e1 = e1.flatten() / np.linalg.norm(e1)
|
|
244
|
+
e1x = np.array([[ 0, +e1[2], -e1[1]],
|
|
245
|
+
[-e1[2], 0, +e1[0]],
|
|
246
|
+
[+e1[1], -e1[0], 0]])
|
|
247
|
+
P0plus = self.pseudoinverse()
|
|
248
|
+
|
|
249
|
+
return np.dot(np.dot(e1x, P1), P0plus)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Direct Linear Transformation (DLT)
|
|
3
|
+
Author: André Aichert
|
|
4
|
+
Date: June 22, 2023
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
def dlt_normalization(p2d, p3d, matches):
|
|
11
|
+
"""Normalization of 2D and 3D point clouds. Input assumed to have homogeneous coordinate 1.
|
|
12
|
+
Purpose: De-mean and scale to +/- sqtr(2), sqrt(3) respectively. """
|
|
13
|
+
# Compute mean
|
|
14
|
+
mean2d = np.array([0, 0])
|
|
15
|
+
mean3d = np.array([0, 0, 0])
|
|
16
|
+
for i, j in matches:
|
|
17
|
+
mean2d += p2d[i][:2]
|
|
18
|
+
mean3d += p3d[j][:3]
|
|
19
|
+
mean2d /= len(match)
|
|
20
|
+
mean3d /= len(match)
|
|
21
|
+
|
|
22
|
+
# Compute size
|
|
23
|
+
s2d = 0
|
|
24
|
+
s3d = 0
|
|
25
|
+
for i, j in matches:
|
|
26
|
+
s2d += np.linalg.norm(p2d[i][:2] - mean2d)
|
|
27
|
+
s3d += np.linalg.norm(p3d[j][:3] - mean3d)
|
|
28
|
+
s2d *= np.sqrt(2) / len(match)
|
|
29
|
+
s3d *= np.sqrt(3) / len(match)
|
|
30
|
+
|
|
31
|
+
# Compose normalization matrices
|
|
32
|
+
normalization_2d = np.matmul(scale(s2d, s2d), translation(-mean2d[0], -mean2d[1]))
|
|
33
|
+
normalization_3d = np.matmul(scale(s3d, s3d, s3d), translation(-mean3d[0], -mean3d[1], -mean3d[2]))
|
|
34
|
+
|
|
35
|
+
return normalization_2d, normalization_3d
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def dlt(x, X, match=None):
|
|
39
|
+
"""Direct Linear Transformation (DLT) for projection matrices."""
|
|
40
|
+
# Optional match parameter
|
|
41
|
+
if not match and len(X) == len(x):
|
|
42
|
+
match = {i: i for i in range(len(X))}
|
|
43
|
+
|
|
44
|
+
# Check for insufficient data
|
|
45
|
+
if len(match) < 6:
|
|
46
|
+
return np.zeros((3, 4))
|
|
47
|
+
|
|
48
|
+
# Normalization of input data
|
|
49
|
+
N_px, N_mm = dlt_normalization(x, X, match)
|
|
50
|
+
|
|
51
|
+
# Build homogeneous system matrix from point matches
|
|
52
|
+
A = np.zeros((2 * len(match), 12))
|
|
53
|
+
for k, (key, value) in enumerate(match.items()):
|
|
54
|
+
# Normalize input points
|
|
55
|
+
x_norm = np.dot(N_px, x[key])
|
|
56
|
+
X_norm = np.dot(N_mm, X[value])
|
|
57
|
+
# Write two rows in A (we get two independent equations from one point match)
|
|
58
|
+
A[2 * k, 4:8] = x_norm[2] * X_norm
|
|
59
|
+
A[2 * k + 1, 0:4] = -x_norm[2] * X_norm
|
|
60
|
+
A[2 * k, 8:12] = -x_norm[1] * X_norm
|
|
61
|
+
A[2 * k + 1, 8:12] = x_norm[0] * X_norm
|
|
62
|
+
|
|
63
|
+
# Solve and reshape
|
|
64
|
+
_, _, V = np.linalg.svd(A)
|
|
65
|
+
p = V[-1] / V[-1, -1]
|
|
66
|
+
P_norm = p.reshape((3, 4))
|
|
67
|
+
|
|
68
|
+
# Denormalize
|
|
69
|
+
P = np.dot(np.dot(np.linalg.inv(N_px), P_norm), N_mm)
|
|
70
|
+
P /= np.linalg.norm(P[:, :3], axis=0)
|
|
71
|
+
return P
|