-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_cloud.py
941 lines (770 loc) · 30.5 KB
/
point_cloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
"""
This module contains functions for processing and transforming point cloud data, \
particularly focusing on the conversion between lidar and camera coordinate \
systems. It is designed to work with point cloud data typically used in \
autonomous vehicle and robotics applications.
"""
from typing import Literal
import scipy.interpolate
import numpy as np
import matplotlib.pyplot as plt
import open3d as o3d
import plotting
def _check_img_size(img_size: tuple[int, int]) -> None:
"""
Check the shape of the input img_size.
Parameters:
img_size (tuple[int, int]): Size of the image as a tuple of two integers.
Returns:
None
"""
if img_size[0] <= 0 or img_size[1] <= 0:
raise ValueError(
f"Invalid value for `img_size`. Both values must be greater than 0 but it is {img_size}."
)
def _check_img(img: np.ndarray) -> None:
"""
Check the shape of the input img.
Parameters:
img (np.ndarray): Image as a Numpy array of shape (H, W) or (H, W, 3) where H is the height and W is the width \
of the image.
Returns:
None
"""
if img.ndim not in (2, 3):
raise ValueError(
f"Invalid shape for `img`. It must be of shape (H, W) or (H, W, 3) but it is of shape {img.shape}."
)
if img.shape[0] <= 10 or img.shape[1] <= 10:
raise ValueError(
f"Invalid shape for `img`. Both dimensions must be greater than 10 but it is {img.shape}."
)
def _check_points_lidar(points_lidar: np.ndarray) -> None:
"""
Check the shape of the input points_lidar.
Parameters:
points_lidar (np.ndarray): Coordinates of the velodyne points in lidar coordinates as a Numpy array of shape \
(N, 3).
Returns:
None
"""
if points_lidar.ndim != 2 or points_lidar.shape[1] != 3:
raise ValueError(
f"Invalid shape for `points_lidar`. It must be of shape (N, 3) but it is of shape {points_lidar.shape}."
)
def _check_points_cam(points_cam: np.ndarray) -> None:
"""
Check the shape of the input points_cam.
Parameters:
points_cam (np.ndarray): Coordinates of the velodyne points in camera coordinates as a Numpy array of shape \
(N, 3).
Returns:
None
"""
if points_cam.ndim != 2 or points_cam.shape[1] != 3:
raise ValueError(
f"Invalid shape for `points_cam`. It must be of shape (N, 3) but it is of shape {points_cam.shape}."
)
def _check_points_img(points_img: np.ndarray) -> None:
"""
Check the shape of the input points_img.
Parameters:
points_img (np.ndarray): Coordinates of the velodyne points in image coordinates as a Numpy array of shape \
(N, 2).
Returns:
None
"""
if points_img.ndim != 2 or points_img.shape[1] != 2:
raise ValueError(
f"Invalid shape for `points_img`. It must be of shape (N, 2) but it is of shape {points_img.shape}."
)
def _check_velo_to_cam(velo_to_cam: np.ndarray) -> None:
"""
Check the shape of the input velo_to_cam.
Parameters:
velo_to_cam (np.ndarray): Transformation matrix from lidar to camera coordinates as a Numpy array of shape \
(4, 4).
Returns:
None
"""
if velo_to_cam.shape != (4, 4):
raise ValueError(
f"Invalid shape for `velo_to_cam`. It must be of shape (4, 4) but it is of shape {velo_to_cam.shape}."
)
if not np.allclose(velo_to_cam[3, :], np.array([0, 0, 0, 1])):
raise ValueError(
f"Invalid value for `velo_to_cam`. The last row must be [0, 0, 0, 1] but it is {velo_to_cam[3, :]}."
)
def _check_r_rect(r_rect: np.ndarray) -> None:
"""
Check the shape of the input r_rect.
Parameters:
r_rect (np.ndarray): Rectification matrix as a Numpy array of shape (4, 4).
Returns:
None
"""
if r_rect.shape != (4, 4):
raise ValueError(
f"Invalid shape for `r_rect`. It must be of shape (4, 4) but it is of shape {r_rect.shape}."
)
def _check_p_rect(p_rect: np.ndarray) -> None:
"""
Check the shape of the input p_rect.
Parameters:
p_rect (np.ndarray): Projection matrix as a Numpy array of shape (3, 4).
Returns:
None
"""
if p_rect.shape != (3, 4):
raise ValueError(
f"Invalid shape for `p_rect`. It must be of shape (3, 4) but it is of shape {p_rect.shape}."
)
def lidar_to_cam(points_lidar: np.ndarray, *, velo_to_cam: np.ndarray) -> np.ndarray:
"""
Convert lidar coordinates to camera coordinates.
Parameters:
points_lidar (np.ndarray): Array of lidar coordinates with shape (N, 3).
velo_to_cam (np.ndarray): Transformation matrix from lidar to camera \
coordinates with shape (4, 4).
Returns:
np.ndarray: Array of camera coordinates with shape (N, 3).
"""
# check the input
_check_points_lidar(points_lidar)
_check_velo_to_cam(velo_to_cam)
points_lidar = np.column_stack((points_lidar, np.ones(points_lidar.shape[0])))
points_cam_coord = points_lidar @ velo_to_cam.T
points_cam_coord = points_cam_coord[:, :3]
return points_cam_coord
def cam_to_img(
points_cam: np.ndarray,
*,
r_rect: np.ndarray,
p_rect: np.ndarray,
img_height: int | None = None,
img_width: int | None = None,
) -> tuple[np.ndarray, np.ndarray]:
"""
Converts camera coordinates to image coordinates.
Parameters:
points_cam (np.ndarray): Array of camera coordinates with shape (N, 3).
r_rect (np.ndarray): Rectification matrix with shape (4, 4).
p_rect (np.ndarray): Projection matrix with shape (3, 4).
img_height (int, optional): Height of the image. Defaults to None.
img_width (int, optional): Width of the image. Defaults to None.
Returns:
points_img (np.ndarray): Array of image coordinates with shape (N, 2).
idx (np.ndarray): Boolean mask indicating which points are within the image boundaries.
"""
# check the input
_check_points_cam(points_cam)
_check_r_rect(r_rect)
_check_p_rect(p_rect)
if img_height is not None:
if img_height <= 0:
raise ValueError(
f"Invalid value for `img_height`. It must be greater than 0 but it is {img_height}."
)
if img_width is not None:
if img_width <= 0:
raise ValueError(
f"Invalid value for `img_width`. It must be greater than 0 but it is {img_width}."
)
points_cam = np.column_stack((points_cam, np.ones(points_cam.shape[0])))
points_img = (points_cam @ r_rect.T) @ p_rect.T
points_img = points_img / points_img[:, 2, np.newaxis]
points_img = points_img[:, :2]
points_img = points_img.round()
points_img = points_img.astype(int)
idx = points_cam[:, 2] >= 0
idx = np.logical_and(idx, points_img[:, 0] >= 0)
idx = np.logical_and(idx, points_img[:, 1] >= 0)
if img_width is not None:
idx = np.logical_and(idx, points_img[:, 0] < img_width)
if img_height is not None:
idx = np.logical_and(idx, points_img[:, 1] < img_height)
return points_img, idx
def lidar_to_img(
points_lidar: np.ndarray,
*,
velo_to_cam: np.ndarray,
r_rect: np.ndarray,
p_rect: np.ndarray,
img_height: int | None = None,
img_width: int | None = None,
) -> tuple[np.ndarray, np.ndarray]:
"""
Converts lidar coordinates to image coordinates.
Parameters:
points_lidar (np.ndarray): Velodyne points in lidar coordinates as a Numpy array of shape (N, 3).
velo_to_cam (np.ndarray): Transformation matrix from lidar to camera \
coordinates with shape (4, 4).
r_rect (np.ndarray): Rectification matrix with shape (4, 4).
p_rect (np.ndarray): Projection matrix with shape (3, 4).
img_height (int, optional): Height of the image. Defaults to None.
img_width (int, optional): Width of the image. Defaults to None.
Returns:
points_img (np.ndarray): Array of image coordinates with shape (N, 2).
idx (np.ndarray): Boolean mask indicating which points are within the image boundaries.
"""
# check the input
_check_points_lidar(points_lidar)
_check_velo_to_cam(velo_to_cam)
_check_r_rect(r_rect)
_check_p_rect(p_rect)
if img_height is not None:
if img_height <= 0:
raise ValueError(
f"Invalid value for `img_height`. It must be greater than 0 but it is {img_height}."
)
if img_width is not None:
if img_width <= 0:
raise ValueError(
f"Invalid value for `img_width`. It must be greater than 0 but it is {img_width}."
)
points_cam = lidar_to_cam(points_lidar, velo_to_cam=velo_to_cam)
points_img, idx = cam_to_img(
points_cam,
r_rect=r_rect,
p_rect=p_rect,
img_height=img_height,
img_width=img_width,
)
return points_img, idx
def to_field_on_img(
field_on_lidar: np.ndarray,
points_lidar: np.ndarray,
*,
mask: np.ndarray | None = None,
interpolation: Literal["linear", "nearest", "cubic"] | None = None,
img_size: tuple[int, int],
velo_to_cam: np.ndarray,
r_rect: np.ndarray,
p_rect: np.ndarray,
) -> np.ndarray:
"""
Convert a field defined on lidar coordinates to a field defined on image coordinates.
Args:
field_on_lidar (np.ndarray): Field defined on lidar coordinates as a Numpy array \
of shape (N,)
points_lidar (np.ndarray): lidar points as a Numpy array of shape (N, 3)
mask (np.ndarray, optional): Mask to ignore points outside the mask as a Numpy array \
of shape `img_size`. Defaults to None.
interpolation (None | linear | nearest | cubic): Interpolation method. If None, no \
interpolation occurs. Defaults to None.
img_size (tuple[int, int]): Size of the image.
velo_to_cam (np.ndarray): Transformation matrix from lidar to camera coordinates.
r_rect (np.ndarray): Rectification matrix.
p_rect (np.ndarray): Projection matrix.
Returns:
np.ndarray: Field defined on image coordinates as a Numpy array of shape `img_size'
"""
# check the input
_check_img_size(img_size)
_check_points_lidar(points_lidar)
_check_velo_to_cam(velo_to_cam)
_check_r_rect(r_rect)
_check_p_rect(p_rect)
if mask is not None:
if mask.shape != img_size:
raise ValueError(
f"Invalid shape for `mask`. It must be of shape {img_size} but it is of shape {mask.shape}."
)
# convert from lidar coordinates to camera coordinates
points_img, idx = lidar_to_img(
points_lidar,
velo_to_cam=velo_to_cam,
r_rect=r_rect,
p_rect=p_rect,
img_height=img_size[0],
img_width=img_size[1],
)
# interpolate
if interpolation is None:
field_on_img = np.full((img_size[0], img_size[1]), np.nan)
field_on_img[points_img[idx][:, 1], points_img[idx][:, 0]] = field_on_lidar[idx]
else:
grid_x, grid_y = np.meshgrid(np.arange(img_size[1]), np.arange(img_size[0]))
field_on_img = scipy.interpolate.griddata(
points_img[idx], field_on_lidar[idx], (grid_x, grid_y), method=interpolation
)
interp = scipy.interpolate.NearestNDInterpolator(
points_img[idx], field_on_lidar[idx]
)
field_on_img = interp(grid_x, grid_y)
# ignore the outside of the mask
if mask is not None:
field_on_img = np.where(mask, field_on_img, np.nan)
return field_on_img
def scale_field_on_lidar(
points_lidar: np.ndarray,
*,
img_size: tuple[int, int],
velo_to_cam: np.ndarray,
r_rect: np.ndarray,
p_rect: np.ndarray,
scaling: float = 0.3,
neighbors: int = 50,
radius: float | None = 10,
) -> np.ndarray:
"""
Calculates the scale field for height estimation.
Parameters:
points_lidar (np.ndarray): Velodyne points in lidar coordinates as a Numpy array of shape (N, 3).
img_size (tuple[int, int]): Size of the image.
velo_to_cam (np.ndarray): Transformation matrix from lidar to camera \
coordinates with shape (4, 4).
r_rect (np.ndarray): Rectification matrix with shape (4, 4).
p_rect (np.ndarray): Projection matrix with shape (3, 4).
scaling (float, optional): Scaling factor for the normal vectors. Defaults to 0.3.
neighbors (int, optional): Number of neighbors to consider for normal \
vector calculation. Defaults to 50.
radius (float | None, optional): Radius for neighborhood search. Defaults to 10.
Returns:
np.ndarray: Scale field for height estimation.
"""
# check the input
_check_img_size(img_size)
_check_points_lidar(points_lidar)
_check_velo_to_cam(velo_to_cam)
_check_r_rect(r_rect)
_check_p_rect(p_rect)
if scaling == 0:
raise ValueError(
f"Invalid value for `scaling`. It must be non-zero but it is {scaling}."
)
if neighbors <= 0:
raise ValueError(
f"Invalid value for `neighbors`. It must be greater than 0 but it is {neighbors}."
)
if radius is not None:
if radius <= 0:
raise ValueError(
f"Invalid value for `radius`. It must be positive but it is {radius}."
)
# change from lidar coordinates to camera coordinates and project points on to image plane
points_img, _ = lidar_to_img(
points_lidar,
velo_to_cam=velo_to_cam,
r_rect=r_rect,
p_rect=p_rect,
img_height=img_size[0],
img_width=img_size[1],
)
# calculate the normal vectors
normal_vectors = normals_on_lidar(points_lidar, neighbors=neighbors, radius=radius)
normal_vectors *= scaling
# change from lidar coordinates to camera coordinates and project the displaced points on to image plane
displaced_points_lidar = points_lidar + normal_vectors
displaced_points_img, _ = lidar_to_img(
displaced_points_lidar,
velo_to_cam=velo_to_cam,
r_rect=r_rect,
p_rect=p_rect,
img_height=img_size[0],
img_width=img_size[1],
)
# calculate the scale field on lidar
field_on_lidar = np.linalg.norm(displaced_points_img - points_img, axis=1) / scaling
return field_on_lidar
def scale_field_on_img(
points_lidar: np.ndarray,
*,
mask: np.ndarray | None = None,
interpolation: Literal["linear", "nearest", "cubic"] | None = None,
img_size: tuple[int, int],
velo_to_cam: np.ndarray,
r_rect: np.ndarray,
p_rect: np.ndarray,
scaling: float = 0.3,
neighbors: int = 50,
radius: float | None = 10,
) -> np.ndarray:
"""
Calculates the scale field for height estimation on the image.
Parameters:
points_lidar (np.ndarray): Velodyne points in lidar coordinates as a Numpy array of shape \
(N, 3).
img_height (int): Height of the image.
img_width (int): Width of the image.
velo_to_cam (np.ndarray): Transformation matrix from lidar to camera coordinates with shape \
(4, 4).
r_rect (np.ndarray): Rectification matrix with shape (4, 4).
p_rect (np.ndarray): Projection matrix with shape (3, 4).
scaling (float, optional): Scaling factor for the normal vectors. Defaults to 0.3.
neighbors (int, optional): Number of neighbors to consider for normal \
vector calculation. Defaults to 50.
radius (float | None, optional): Radius for neighborhood search. Defaults to 10.
Returns:
np.ndarray: Scale field for height estimation on the image.
"""
# check the input
_check_img_size(img_size)
_check_points_lidar(points_lidar)
_check_velo_to_cam(velo_to_cam)
_check_r_rect(r_rect)
_check_p_rect(p_rect)
if scaling == 0:
raise ValueError(
f"Invalid value for `scaling`. It must be non-zero but it is {scaling}."
)
if neighbors <= 0:
raise ValueError(
f"Invalid value for `neighbors`. It must be greater than 0 but it is {neighbors}."
)
if radius is not None:
if radius <= 0:
raise ValueError(
f"Invalid value for `radius`. It must be positive but it is {radius}."
)
field_on_lidar = scale_field_on_lidar(
points_lidar,
img_size=img_size,
velo_to_cam=velo_to_cam,
r_rect=r_rect,
p_rect=p_rect,
scaling=scaling,
neighbors=neighbors,
radius=radius,
)
field_on_img = to_field_on_img(
field_on_lidar,
points_lidar,
mask=mask,
interpolation=interpolation,
img_size=img_size,
velo_to_cam=velo_to_cam,
r_rect=r_rect,
p_rect=p_rect,
)
return field_on_img
def to_vector_on_img(
vector_on_lidar: np.ndarray,
points_lidar: np.ndarray,
*,
mask: np.ndarray | None = None,
interpolation: Literal["linear", "nearest", "cubic"] | None = None,
img_size: tuple[int, int],
velo_to_cam: np.ndarray,
r_rect: np.ndarray,
p_rect: np.ndarray,
) -> np.ndarray:
"""
Convert a vector field defined on lidar coordinates to a vector field defined on image coordinates.
Args:
vector_on_lidar (np.ndarray): Vector field defined on lidar coordinates as a Numpy array \
of shape (N, D)
points_lidar (np.ndarray): lidar points as a Numpy array of shape (N, 3)
mask (np.ndarray, optional): Mask to ignore points outside the mask as a Numpy array \
of shape `img_size`. Defaults to None.
interpolation (None | linear | nearest | cubic): Interpolation method. If None, no \
interpolation occurs. Defaults to None.
img_size (tuple[int, int]): Size of the image.
velo_to_cam (np.ndarray): Transformation matrix from lidar to camera coordinates.
r_rect (np.ndarray): Rectification matrix.
p_rect (np.ndarray): Projection matrix.
Returns:
np.ndarray: Vector field defined on image coordinates as a Numpy array of shape (H, W, D) where (H, W) is `img_size'.
"""
# check the input
_check_img_size(img_size)
_check_points_lidar(points_lidar)
_check_velo_to_cam(velo_to_cam)
_check_r_rect(r_rect)
_check_p_rect(p_rect)
if mask is not None:
if mask.shape != img_size:
raise ValueError(
f"Invalid shape for `mask`. It must be of shape {img_size} but it is of shape {mask.shape}."
)
# treat each dimension as a field
D = vector_on_lidar.shape[1]
vector_on_img = np.full((*img_size, D), np.nan)
for i in range(D):
vector_on_img[:, :, i] = to_field_on_img(
vector_on_lidar[:, i],
points_lidar,
mask=mask,
interpolation=interpolation,
img_size=img_size,
velo_to_cam=velo_to_cam,
r_rect=r_rect,
p_rect=p_rect,
)
return vector_on_img
def normals_on_lidar(
points_lidar: np.ndarray, *, neighbors: int = 50, radius: float | None = 10
) -> np.ndarray:
"""
Compute the normal vectors of a point cloud.
Parameters:
points_lidar (np.ndarray): The input point cloud as a numpy array of shape \
(N, 3), where N is the number of points and each point is \
represented by its (x, y, z) coordinates.
neighbors (int, optional): The number of nearest neighbors to consider \
when estimating normals. Defaults to 50.
radius (float | None, optional): The radius within which to search for \
neighbors when estimating normals. If set to None, the search is \
performed based on the number of neighbors. Defaults to 10.
Returns:
np.ndarray: The computed normal vectors as a numpy array of shape \
(N, 3), where N is the number of points and each normal vector is \
represented by its (nx, ny, nz) components.
"""
# check the input
_check_points_lidar(points_lidar)
if neighbors <= 0:
raise ValueError(
f"Invalid value for `neighbors`. It must be greater than 0 but it is {neighbors}."
)
if radius is not None:
if radius <= 0:
raise ValueError(
f"Invalid value for `radius`. It must be positive but it is {radius}."
)
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points_lidar)
if radius is None:
search_param = o3d.geometry.KDTreeSearchParamKNN(knn=neighbors)
else:
search_param = o3d.geometry.KDTreeSearchParamHybrid(
radius=radius, max_nn=neighbors
)
pcd.estimate_normals(search_param=search_param)
pcd.orient_normals_consistent_tangent_plane(100)
return np.asarray(pcd.normals)
def normals_on_img(
points_lidar: np.ndarray,
*,
mask: np.ndarray | None = None,
interpolation: Literal["linear", "nearest", "cubic"] | None = None,
img_size: tuple[int, int],
velo_to_cam: np.ndarray,
r_rect: np.ndarray,
p_rect: np.ndarray,
neighbors: int = 50,
radius: float | None = 10,
) -> np.ndarray:
"""
Compute the normal vectors of a point cloud on the image coordinates.
Parameters:
points_lidar (np.ndarray): The input point cloud as a numpy array of shape \
(N, 3), where N is the number of points and each point is \
represented by its (x, y, z) coordinates.
mask (np.ndarray, optional): Mask to ignore points outside the mask as a Numpy array \
of shape `img_size`. Defaults to None.
interpolation (None | linear | nearest | cubic): Interpolation method. If None, no \
interpolation occurs. Defaults to None.
img_size (tuple[int, int]): Size of the image.
velo_to_cam (np.ndarray): Transformation matrix from lidar to camera coordinates.
r_rect (np.ndarray): Rectification matrix.
p_rect (np.ndarray): Projection matrix.
neighbors (int, optional): The number of nearest neighbors to consider \
when estimating normals. Defaults to 50.
radius (float | None, optional): The radius within which to search for \
neighbors when estimating normals. If set to None, the search is \
performed based on the number of neighbors. Defaults to 10.
Returns:
np.ndarray: The computed normal vectors as a numpy array of shape \
(H, W, 3), where (H, W) is `img_size`.
"""
# check the input
_check_img_size(img_size)
_check_points_lidar(points_lidar)
_check_velo_to_cam(velo_to_cam)
_check_r_rect(r_rect)
_check_p_rect(p_rect)
if mask is not None:
if mask.shape != img_size:
raise ValueError(
f"Invalid shape for `mask`. It must be of shape {img_size} but it is of shape {mask.shape}."
)
if neighbors <= 0:
raise ValueError(
f"Invalid value for `neighbors`. It must be greater than 0 but it is {neighbors}."
)
if radius is not None:
if radius <= 0:
raise ValueError(
f"Invalid value for `radius`. It must be positive but it is {radius}."
)
# calculate the normal vectors on lidar
vector_on_lidar = normals_on_lidar(points_lidar, neighbors=neighbors, radius=radius)
vector_on_img = to_vector_on_img(
vector_on_lidar,
points_lidar,
mask=mask,
interpolation=interpolation,
img_size=img_size,
velo_to_cam=velo_to_cam,
r_rect=r_rect,
p_rect=p_rect,
)
# normalization
vector_on_img /= np.linalg.norm(vector_on_img, axis=2, keepdims=True)
return vector_on_img
# plotting functions
def draw_velodyne_on_image(
img: np.ndarray,
points_lidar: np.ndarray,
*,
velo_to_cam: np.ndarray,
r_rect: np.ndarray,
p_rect: np.ndarray,
**kwargs,
) -> tuple[np.ndarray, np.ndarray]:
"""
Project velodyne points on to image plane.
Parameters:
img (np.ndarray): Image as a Numpy array of shape (H, W) \
or (H, W, 3) where H is the height and W is the width of the image.
points_lidar (np.ndarray): The coordinates of the velodyne points with respect \
to lidar as a Numpy array of shape (N, 3) where N is the number of points.
velo_to_cam (np.ndarray): The transformation matrix from lidar to camera coordinates \
as a Numpy array of shape (4, 4).
r_rect (np.ndarray): The rotation matrix for rectifying the camera coordinates \
as a Numpy array of shape (4, 4).
p_rect (np.ndarray): The projection matrix for rectifying the camera coordinates \
as a Numpy array of shape (3, 4).
**kwargs: Additional keyword arguments for scatter_data_on_image function.
Returns:
np.ndarray: Coordinates of the points with respect to image plane coordinates.
np.ndarray: The indices of the points that were successfully projected on \
to the image plane.
"""
# check the input
_check_img(img)
_check_points_lidar(points_lidar)
_check_velo_to_cam(velo_to_cam)
_check_r_rect(r_rect)
_check_p_rect(p_rect)
img_height, img_width = img.shape[:2]
# convert from lidar coordinates to camera coordinates
points_cam = lidar_to_cam(points_lidar, velo_to_cam=velo_to_cam)
# project the points on to the image plane
points_img, idx = cam_to_img(
points_cam,
r_rect=r_rect,
p_rect=p_rect,
img_height=img_height,
img_width=img_width,
)
plotting.scatter_on_image(img, points_img[idx], c=points_cam[idx, 2], **kwargs)
return points_img, idx
# data exploration functions
def in_radius(points: np.ndarray, center: np.ndarray, radius: float) -> np.ndarray:
"""
Returns a boolean array indicating whether each point in the given array
is inside the specified radius from the center point.
Parameters:
points (np.ndarray): Array of points with shape (N, D), where N is the
number of points and D is the number of dimensions.
center (np.ndarray): Center point with shape (D,) specifying the
coordinates of the center.
radius (float): Radius value specifying the maximum distance allowed
from the center point.
Returns:
np.ndarray: Boolean array with shape (N,) indicating whether each point
is inside the specified radius.
"""
displacements = points - center
distances = np.linalg.norm(displacements, axis=1)
points_inside = distances <= radius
return points_inside
def approximate_diameter(points: np.ndarray, sample_size: int | None = 100) -> float:
"""
Calculate the diameter of a point cloud by random sampling.
Parameters:
points (numpy.ndarray): The array of points in the point cloud.
sample_size (int | None): Number of points to sample for approximation. \
If set to None, all of the points are considered.
Returns:
float: The approximate maximum distance between any two points in the \
point cloud.
"""
assert points.ndim == 2
assert points.shape[1] == 3
if sample_size is not None:
assert sample_size > 0
if sample_size is None:
sampled_points = points
else:
sample_size = min(sample_size, points.shape[0])
idx = np.random.choice(points.shape[0], size=sample_size, replace=False)
sampled_points = points[idx, :]
distances = np.linalg.norm(sampled_points[:, np.newaxis] - sampled_points, axis=2)
return distances.max()
def plot_number_of_points_in_radius(
points: np.ndarray,
radius: np.ndarray,
*,
sample_size: int | None = 100,
title: str = "Average Number of Points in Radius vs Radius",
xlabel: str = "Radius",
ylabel: str = "Average Number of Points in Radius",
output_name: str = "average_number_of_points_in_radius.png",
) -> None:
"""
Plots the average number of points in a given radius.
Parameters:
points (np.ndarray): The array of points with shape (N, 3).
radius (np.ndarray): The array of radii.
sample_size (int | None, optional): The number of points to sample. \
Defaults to 100. If set to None, all points are considered.
title (str, optional): The title of the plot. Defaults to 'Average \
Number of Points in Radius vs Radius'.
xlabel (str, optional): The label for the x-axis. Defaults to 'Radius'.
ylabel (str, optional): The label for the y-axis. Defaults to 'Average \
Number of Points in Radius'.
output_name (str, optional): The name of the output file. Defaults to \
'average_number_of_points_in_radius.png'.
Returns:
None
"""
assert points.ndim == 2
assert points.shape[1] == 3
if sample_size is not None:
assert sample_size > 0
if sample_size is None:
sampled_points = points
else:
sample_size = min(sample_size, points.shape[0])
idx = np.random.choice(points.shape[0], size=sample_size, replace=False)
sampled_points = points[idx, :]
distances = np.linalg.norm(
sampled_points[:, np.newaxis, :] - sampled_points, axis=2
)
idx = distances[:, :, np.newaxis] <= radius
num_points_in_radius = idx.sum(axis=1).mean(axis=0)
plt.plot(radius, num_points_in_radius)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
plt.savefig(output_name, dpi=300, bbox_inches="tight")
plt.close()
return num_points_in_radius
def test_scatter_data_on_image(
img: np.ndarray,
data: np.ndarray | str = "random",
colors: np.ndarray | str | None = "random",
) -> None:
"""
Scatter data points on an image.
Parameters:
img (np.ndarray): The input image.
data (np.ndarray | str): The data points to scatter. If 'random', \
random data points will be generated.
colors (np.ndarray | str | None): The colors of the data points. If \
'random', random colors will be generated. If None, default colors \
will be used.
Returns:
None
"""
img_width = img.shape[1]
img_height = img.shape[0]
if isinstance(data, str):
num_points = 50
data = np.random.rand(num_points, 2) * np.array([img_width, img_height])
if isinstance(colors, str):
colors = np.random.rand(data.shape[0]) # type: ignore
plotting.scatter_on_image(img, data, c=colors)