File size: 29,786 Bytes
bc68240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import equilib
import imageio
import numpy as np
from equilib import equi2pers
from sklearn.preprocessing import normalize

assert equilib.__version__ == "0.3.0"
from typing import Union

import torch
from equilib import grid_sample
from numpy.lib.scimath import sqrt as csqrt
from PIL import Image
from torchvision import transforms


def diskradius(xi, f):  # compute the disk radius when the image is catadioptric
    return np.sqrt(-(f * f) / (1 - xi * xi))


def create_rotation_matrix(
    roll: float,
    pitch: float,
    yaw: float,
) -> np.ndarray:
    r"""Create rotation matrix from extrinsic parameters
    Args:
        roll (float): camera rotation about camera frame z-axis
        pitch (float): camera rotation about camera frame x-axis
        yaw (float): camera rotation about camera frame y-axis

    Returns:
        np.ndarray: rotation R_z @ R_x @ R_y
    """
    # calculate rotation about the x-axis
    R_x = np.array(
        [
            [1.0, 0.0, 0.0],
            [0.0, np.cos(pitch), np.sin(pitch)],
            [0.0, -np.sin(pitch), np.cos(pitch)],
        ]
    )
    # calculate rotation about the y-axis
    R_y = np.array(
        [
            [np.cos(yaw), 0.0, -np.sin(yaw)],
            [0.0, 1.0, 0.0],
            [np.sin(yaw), 0.0, np.cos(yaw)],
        ]
    )
    # calculate rotation about the z-axis
    R_z = np.array(
        [
            [np.cos(roll), np.sin(roll), 0.0],
            [-np.sin(roll), np.cos(roll), 0.0],
            [0.0, 0.0, 1.0],
        ]
    )

    return R_z @ R_x @ R_y


def minfocal(u0, v0, xi, xref=1, yref=1):
    """compute the minimum focal for the image to be catadioptric given xi"""
    fmin = np.sqrt(
        -(1 - xi * xi) * ((xref - u0) * (xref - u0) + (yref - v0) * (yref - v0))
    )

    return fmin * 1.0001


def deg2rad(deg):
    """convert degrees to radians"""
    return deg * np.pi / 180


def preprocess(
    img: Union[np.ndarray, Image.Image],
    is_cv2: bool = False,
) -> torch.Tensor:
    """Convert img to tensor"""
    if isinstance(img, np.ndarray) and is_cv2:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    if isinstance(img, Image.Image):
        # Sometimes images are RGBA
        img = img.convert("RGB")

    to_tensor = transforms.Compose(
        [
            transforms.ToTensor(),
        ]
    )
    img = to_tensor(img)
    assert len(img.shape) == 3, "input must be dim=3"
    assert img.shape[0] == 3, "input must be HWC"
    return img


def postprocess(
    img: torch.Tensor,
    to_cv2: bool = False,
) -> Union[np.ndarray, Image.Image]:
    """Convert img from tensor to image format"""
    if to_cv2:
        img = np.asarray(img.to("cpu").numpy() * 255, dtype=np.uint8)
        img = np.transpose(img, (1, 2, 0))
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        return img
    else:
        to_PIL = transforms.Compose(
            [
                transforms.ToPILImage(),
            ]
        )
        img = img.to("cpu")
        img = to_PIL(img)
        return img


class PanoCam:
    def __init__(self, pano_path, device="cpu"):
        """initialize PanoCam model

        Args:
            pano_path (str): path to panorama image
            device (str, optional): type of device used to execute instructions. Defaults to "cpu".
        """
        self.pano_path = pano_path
        self.device = device

    def get_image(
        self,
        vfov=85,
        im_w=640,
        im_h=480,
        azimuth=0,
        elevation=30,
        roll=0,
        ar=4.0 / 3.0,
        img_format="RGB",
    ):
        """
        Crop a perspective image from an equirectangular image with specified camera parameters
        camera frame: x right, y down, z out.
        image frame: u right, v down, origin at top left

        Args:
            vfov (float): vertical field of view of cropped image (degrees)
            im_w (int): width of cropped image
            im_h (int): height of cropped image
            azimuth (float): camera rotation about camera frame y-axis of cropped image (degrees)
            elevation (float): camera rotation about camera frame x-axis of cropped image (degrees)
            roll (float): camera rotation about camera frame z-axis of cropped image (degrees)
            ar (float): aspect ratio of cropped image
            img_format (str): format to return image

        Returns:
            crop (np.ndarray): Cropped perspective image
            horizon (float, float): fraction of image left/right border intersection with respect to image height
            vvp (float, float, float): Vertical Vanishing Point, which is the vanishing point for the vertical lines. absvvp is in pixels, vvp is normalized by the image size.
        """
        equi_img = Image.open(self.pano_path)
        equi_img = preprocess(equi_img).to(self.device)
        fov_x = float(
            2 * np.arctan(np.tan(vfov * np.pi / 180.0 / 2) * ar) * 180 / np.pi
        )

        # Switch to https://github.com/haruishi43/equilib#coordinate-system
        rot = {
            "roll": float(roll / 180 * np.pi),
            "pitch": -float(elevation / 180 * np.pi),  # rotate vertical
            "yaw": -float(azimuth / 180 * np.pi),  # rotate horizontal
        }
        # Run equi2pers
        crop = equi2pers(
            equi=equi_img,
            rot=rot,
            w_pers=im_w,
            h_pers=im_h,
            fov_x=fov_x,
            skew=0.0,
            sampling_method="default",
            mode="bilinear",
        )
        crop = postprocess(crop, to_cv2=img_format == "BGR")

        horizon = self.getRelativeHorizonLineFromAngles(
            elevation / 180 * np.pi, roll / 180 * np.pi, vfov / 180 * np.pi, im_h, im_w
        )
        vvp = self.getRelativeVVP(
            elevation / 180 * np.pi, roll / 180 * np.pi, vfov / 180 * np.pi, im_h, im_w
        )
        return crop, horizon, vvp

    @staticmethod
    def crop_equi(equi_img, vfov, im_w, im_h, azimuth, elevation, roll, ar, mode):
        """
        Crop a perspective image from an equirectangular image with specified camera parameters
        camera frame: x right, y down, z out.
        image frame: u right, v down, origin at top left

        Args:
            equi_img (np.ndarray): equirectangular image
            vfov (float): vertical field of view of cropped image (degrees)
            im_w (int): width of cropped image
            im_h (int): height of cropped image
            azimuth (float): camera rotation about camera frame y-axis of cropped image (degrees)
            elevation (float): camera rotation about camera frame x-axis of cropped image (degrees)
            roll (float): camera rotation about camera frame z-axis of cropped image (degrees)
            ar (float): aspect ratio od cropped image
            mode (str): sampling mode for grid sample
        Returns:
            crop (np.ndarray): Cropped perspective image
        """
        fov_x = float(
            2 * np.arctan(np.tan(vfov * np.pi / 180.0 / 2) * ar) * 180 / np.pi
        )

        # Switch to https://github.com/haruishi43/equilib#coordinate-system
        rot = {
            "roll": float(roll / 180 * np.pi),
            "pitch": -float(elevation / 180 * np.pi),  # rotate vertical
            "yaw": -float(azimuth / 180 * np.pi),  # rotate horizontal
        }
        # Preprocess
        if len(equi_img.shape) == 3:
            equi_img_processed = equi_img.transpose(2, 0, 1)
        else:
            equi_img_processed = equi_img[None, :, :]
        equi_img_processed = torch.FloatTensor(equi_img_processed)

        # Run equi2pers
        crop = equi2pers(
            equi=equi_img_processed,
            rot=rot,
            w_pers=im_w,
            h_pers=im_h,
            fov_x=fov_x,
            skew=0.0,
            sampling_method="default",
            mode=mode,
        )
        if len(crop.shape) > 2:
            crop = np.asarray(crop.to("cpu").numpy(), dtype=equi_img.dtype)
            crop = np.transpose(crop, (1, 2, 0))
        else:
            crop = np.asarray(crop.to("cpu").numpy(), dtype=equi_img.dtype)
        return crop

    @staticmethod
    def getGravityField(im_h, im_w, absvvp):
        """
        Retrieve gravity field from absolute vertical vanishing point

        Args:
            im_h (int): image height
            im_w (int): image width
            absvvp ([float, float, float]): Absolute vertical vanishing point in image frame (top left corner as 0)

        Returns:
            np.ndarray: gravity field of shape (im_h, im_w, 2)
        """
        assert not np.isinf(absvvp).any()
        # arrow
        gridx, gridy = np.meshgrid(
            np.arange(0, im_w),
            np.arange(0, im_h),
        )
        start = np.stack((gridx.reshape(-1), gridy.reshape(-1))).T
        arrow = normalize(absvvp[:2] - start) * absvvp[2]
        arrow_map = arrow.reshape(im_h, im_w, 2)
        return arrow_map

    @staticmethod
    def getAbsVVP(im_h, im_w, horizon, vvp):
        """get absolute vertical vanishing point from horizon line and relative vertical vanishing point

        Args:
            im_h (int): image height
            im_w (int): image width
            horizon ([float, float]): fraction of image left/right border intersection with respect to image height
            vvp ([float, float, {-1, 1}]): relative vertical vanishing point, defined as vertical vanishing point divided by image height
        Returns:
            vvp_abs ([float, float, float]): absolute vertical vanishing point in image frame (top left corner as 0),
            vvp_abs[2] in {-1, 1} depending on if it is south or north pole, or if the up vectors are pointing towards (+1) or away (-1) from it.
        """
        if not np.isinf(vvp).any():
            # VVP
            vvp_abs = np.array([vvp[0] * im_w, vvp[1] * im_h])
            return np.array([vvp_abs[0], vvp_abs[1], vvp[2]])
        else:
            # approximate
            vvp_abs = (
                1e8
                * normalize(np.array([[im_h * (horizon[1] - horizon[0]), -im_w]]))[0]
            )
            return np.array(
                [vvp_abs[0] + 0.5 * im_w - 0.5, vvp_abs[1] + 0.5 * im_h - 0.5, 1]
            )

    @staticmethod
    def getRelativeVVP(elevation, roll, vfov, im_h, im_w):
        """Relative vertical vanishing point in image frame (top left corner as 0)
           Defined as vertical vanishing point divided by image height

        Args:
            elevation (float): camera rotation about camera frame x-axis (radians)
            roll (float): camera rotation about camera frame z-axis (radians)
            vfov (float): vertical field of view (radians)
            im_h (int): image height
            im_w (int): image width

        Returns:
            vvp[0] (float): x coordinate of vertical vanishing point, divided by image height.
            vvp[1] (float): y coordinate of vertical vanishing point, divided by image height.
            vvp[2] {-1, 1}: whether the up vectors are pointing towards (+1) or away (-1) from the vertical vanishing point.

        """
        if elevation == 0:
            return (
                np.inf,
                np.inf,
            )
        vx = (
            0.5
            - 0.5 / im_w
            - 0.5 * np.sin(roll) / np.tan(elevation) / np.tan(vfov / 2) * im_h / im_w
        )
        vy = (
            0.5 - 0.5 / im_h - 0.5 * np.cos(roll) / np.tan(elevation) / np.tan(vfov / 2)
        )
        return vx, vy, np.sign(elevation)

    @staticmethod
    def getRelativeHorizonLineFromAngles(elevation, roll, vfov, im_h, im_w):
        """Get relative horizon line from camera parameters

        Args:
            elevation (float): camera rotation about camera frame x-axis (radians)
            roll (float): camera rotation about camera frame z-axis (radians)
            vfov (float): vertical field of view (radians)
            im_h (int): image height
            im_w (int): image width

        Returns:
            (float, float): in image frame, fraction of image left/right border intersection with respect to image height
        """
        midpoint = PanoCam.getMidpointFromAngle(elevation, roll, vfov)
        dh = PanoCam.getDeltaHeightFromRoll(roll, im_h, im_w)
        return midpoint - dh, midpoint + dh

    @staticmethod
    def getMidpointFromAngle(elevation, roll, vfov):
        """get midpoint of the horizon line from roll, pitch, and vertical field of view

        Args:
            elevation (float): camera rotation about camera frame x-axis (radians)
            roll (float): camera rotation about camera frame z-axis (radians)
            vfov (float): vertical field of view (radians)

        Returns:
            float: location of the midpoint of the horizon line with respect to the image height
        """
        if elevation == np.pi / 2 or elevation == -np.pi / 2:
            return np.inf * np.sign(elevation)
        return 0.5 + 0.5 * np.tan(elevation) / np.cos(roll) / np.tan(vfov / 2)

    @staticmethod
    def getDeltaHeightFromRoll(roll, im_h, im_w):
        """
        Args:
            roll (float): camera rotation about camera frame z-axis (radians)
            im_h (int): image height
            im_w (int): image width

        Returns:
            float: the height distance of horizon from the midpoint at image left/right border intersection.
        """
        if roll == np.pi / 2 or roll == -np.pi / 2:
            return np.inf * np.sign(roll)
        return -im_w / im_h * np.tan(roll) / 2

    @staticmethod
    def get_lat(vfov, im_w, im_h, elevation, roll):
        """get latitude map from camera parameters

        Args:
            vfov (float): vertical field of view (radians)
            im_w (int): image width
            im_h (int): image height
            elevation (float): camera rotation about camera frame x-axis (radians)
            roll (float): camera rotation aboout camera frame z-axis (radians)

        Returns:
            np.ndarray: latitude map of shape (im_h, im_w) in degrees
        """
        focal_length = im_h / 2 / np.tan(vfov / 2)

        # Uniform sampling on the plane
        dy = np.linspace(-im_h / 2, im_h / 2, im_h)
        dx = np.linspace(-im_w / 2, im_w / 2, im_w)
        x, y = np.meshgrid(dx, dy)

        x, y = x.ravel() / focal_length, y.ravel() / focal_length
        focal_length = 1
        x_world = x * np.cos(roll) - y * np.sin(roll)
        y_world = (
            x * np.cos(elevation) * np.sin(roll)
            + y * np.cos(elevation) * np.cos(roll)
            - focal_length * np.sin(elevation)
        )
        z_world = (
            x * np.sin(elevation) * np.sin(roll)
            + y * np.sin(elevation) * np.cos(roll)
            + focal_length * np.cos(elevation)
        )
        l = -np.arctan2(y_world, np.sqrt(x_world**2 + z_world**2)) / np.pi * 180

        return l.reshape(im_h, im_w)

    @staticmethod
    def get_up(vfov, im_w, im_h, elevation, roll):
        """get gravity field from camera parameters

        Args:
            vfov (float): vertical field of view (radians)
            im_w (int): image width
            im_h (int): image height
            elevation (float): camera rotation about camera frame x-axis (radians)
            roll (float): camera rotation rotation aboout camera frame z-axis (radians)

        Returns:
            np.ndarray: gravity field of shape (im_h, im_w, 2)
        """
        horizon = PanoCam.getRelativeHorizonLineFromAngles(
            elevation=elevation, roll=roll, vfov=vfov, im_h=im_h, im_w=im_w
        )
        vvp = PanoCam.getRelativeVVP(
            elevation=elevation, roll=roll, vfov=vfov, im_h=im_h, im_w=im_w
        )
        absvvp = PanoCam.getAbsVVP(im_h=im_h, im_w=im_w, horizon=horizon, vvp=vvp)

        gridx, gridy = np.meshgrid(np.arange(0, im_w), np.arange(0, im_h))
        start = np.stack((gridx.reshape(-1), gridy.reshape(-1))).T
        arrow = normalize(absvvp[:2] - start) * absvvp[2]
        gt_up = arrow.reshape(im_h, im_w, 2)
        return gt_up

    @staticmethod
    def get_up_general(focal_rel, im_w, im_h, elevation, roll, cx_rel, cy_rel):
        """get gravity field from camera parameters.
           no assumptions about centered principal point.

        Args:
            focal_rel (float): relative focal length, defined as focal length divided by image height
            im_w (int): image width
            im_h (int): image height
            elevation (float): camera rotation about camera frame x-axis (radians)
            roll (float): rotation aboout z-axis (radians)
            cx_rel (float): relative cx location (pixel coordinate / image width - 0.5)
            cy_rel (float): relative cy location (pixel coordinate / image height - 0.5)

        Returns:
            np.ndarray: gravity field of shape (im_h, im_w, 2)
        """
        cx = (cx_rel + 0.5) * im_w
        cy = (cy_rel + 0.5) * im_h
        X = (
            np.linspace((-0.5 * im_w) + 0.5, (0.5 * im_w) - 0.5, im_w)
            .reshape(1, im_w)
            .repeat(im_h, 0)
            .astype(np.float32)
            + 0.5 * im_w
        )
        Y = (
            np.linspace((-0.5 * im_h) + 0.5, (0.5 * im_h) - 0.5, im_h)
            .reshape(im_h, 1)
            .repeat(im_w, 1)
            .astype(np.float32)
            + 0.5 * im_h
        )
        xy_cam = np.stack([X, Y], axis=2)
        focal_length = focal_rel * im_h

        if elevation == 0:
            up_vecs = np.ones(xy_cam.shape) * np.array(
                [[-np.sin(roll)], [-np.cos(roll)]]
            ).reshape((1, 2))
        else:
            vvp = np.array(
                [
                    [
                        (np.sin(roll) * np.cos(elevation) * focal_length)
                        / -np.sin(elevation)
                        + (cx)
                    ],
                    [
                        (np.cos(roll) * np.cos(elevation) * focal_length)
                        / -np.sin(elevation)
                        + (cy)
                    ],
                ]
            ).reshape((1, 2))
            up_vecs = vvp - xy_cam
            up_vecs = up_vecs * np.sign(elevation)

        up_vecs_norm = np.linalg.norm(up_vecs, axis=2)[:, :, None]
        up_vecs = up_vecs / up_vecs_norm
        return up_vecs

    @staticmethod
    def get_lat_general(focal_rel, im_w, im_h, elevation, roll, cx_rel, cy_rel):
        """get latitude map from camera parameters.
           no assumptions about centered principal point.

        Args:
            focal_rel (float): relative focal length, defined as focal length divided by image height
            im_w (int): image width
            im_h (int): image height
            elevation (float): camera rotation about camera frame x-axis (radians)
            roll (float): rotation aboout z-axis (radians)
            cx_rel (float): relative cx location (pixel coordinate / image width - 0.5)
            cy_rel (float): relative cy location (pixel coordinate / image height - 0.5)

        Returns:
            np.ndarray: latitude map of shape (im_h, im_w) in degrees
        """
        # Uniform sampling on the plane
        focal_length = focal_rel * im_h
        cx = (cx_rel + 0.5) * im_w
        cy = (cy_rel + 0.5) * im_h
        dy = np.linspace(
            (-im_h / 2) - (cy - (im_h / 2)), (im_h / 2) - (cy - (im_h / 2)), im_h
        )
        dx = np.linspace(
            (-im_w / 2) - (cx - (im_w / 2)), (im_w / 2) - (cx - (im_w / 2)), im_w
        )
        x, y = np.meshgrid(dx, dy)

        x, y = (x.ravel() / focal_length), (y.ravel() / focal_length)
        focal_length = 1
        x_world = x * np.cos(roll) - y * np.sin(roll)
        y_world = (
            x * np.cos(elevation) * np.sin(roll)
            + y * np.cos(elevation) * np.cos(roll)
            - focal_length * np.sin(elevation)
        )
        z_world = (
            x * np.sin(elevation) * np.sin(roll)
            + y * np.sin(elevation) * np.cos(roll)
            + focal_length * np.cos(elevation)
        )
        l = -np.arctan2(y_world, np.sqrt(x_world**2 + z_world**2)) / np.pi * 180

        return l.reshape(im_h, im_w)

    @staticmethod
    def crop_distortion(image360_path, f, xi, H, W, az, el, roll):
        """
        Reference: https://github.com/dompm/spherical-distortion-dataset/blob/main/spherical_distortion/spherical_distortion.py
        Crop distorted image with specified camera parameters

        Args:
            image360_path (str): path to image which to crop from
            f (float): focal_length of cropped image
            xi:
            H (int): height of cropped image
            W (int): width of cropped image
            az: camera rotation about camera frame y-axis of cropped image (degrees)
            el: camera rotation about camera frame x-axis of cropped image (degrees)
            roll: camera rotation about camera frame z-axis of cropped image (degrees)
        Returns:
            im (np.ndarray): cropped, distorted image
        """

        u0 = W / 2.0
        v0 = H / 2.0

        grid_x, grid_y = np.meshgrid(list(range(W)), list(range(H)))

        if isinstance(image360_path, str):
            image360 = imageio.imread(image360_path)  # .astype('float32') / 255.
        else:
            image360 = image360_path.copy()

        ImPano_W = np.shape(image360)[1]
        ImPano_H = np.shape(image360)[0]
        x_ref = 1
        y_ref = 1

        fmin = minfocal(
            u0, v0, xi, x_ref, y_ref
        )  # compute minimal focal length for the image to ve catadioptric with given xi

        # 1. Projection on the camera plane

        X_Cam = np.divide(grid_x - u0, f)
        Y_Cam = -np.divide(grid_y - v0, f)

        # 2. Projection on the sphere

        AuxVal = np.multiply(X_Cam, X_Cam) + np.multiply(Y_Cam, Y_Cam)

        alpha_cam = np.real(xi + csqrt(1 + np.multiply((1 - xi * xi), AuxVal)))

        alpha_div = AuxVal + 1

        alpha_cam_div = np.divide(alpha_cam, alpha_div)

        X_Sph = np.multiply(X_Cam, alpha_cam_div)
        Y_Sph = np.multiply(Y_Cam, alpha_cam_div)
        Z_Sph = alpha_cam_div - xi

        # 3. Rotation of the sphere
        coords = np.vstack((X_Sph.ravel(), Y_Sph.ravel(), Z_Sph.ravel()))
        rot_el = np.array(
            [
                1.0,
                0.0,
                0.0,
                0.0,
                np.cos(deg2rad(el)),
                -np.sin(deg2rad(el)),
                0.0,
                np.sin(deg2rad(el)),
                np.cos(deg2rad(el)),
            ]
        ).reshape((3, 3))
        rot_az = np.array(
            [
                np.cos(deg2rad(az)),
                0.0,
                np.sin(deg2rad(az)),
                0.0,
                1.0,
                0.0,
                -np.sin(deg2rad(az)),
                0.0,
                np.cos(deg2rad(az)),
            ]
        ).reshape((3, 3))
        rot_roll = np.array(
            [
                np.cos(deg2rad(roll)),
                -np.sin(deg2rad(roll)),
                0.0,
                np.sin(deg2rad(roll)),
                np.cos(deg2rad(roll)),
                0.0,
                0.0,
                0.0,
                1.0,
            ]
        ).reshape((3, 3))
        sph = rot_roll.T.dot(rot_el.dot(coords))
        sph = rot_az.dot(sph)

        sph = sph.reshape((3, H, W)).transpose((1, 2, 0))
        X_Sph, Y_Sph, Z_Sph = sph[:, :, 0], sph[:, :, 1], sph[:, :, 2]

        # 4. cart 2 sph
        ntheta = np.arctan2(X_Sph, Z_Sph)
        nphi = np.arctan2(Y_Sph, np.sqrt(Z_Sph**2 + X_Sph**2))

        pi = np.pi

        # 5. Sphere to pano
        min_theta = -pi
        max_theta = pi
        min_phi = -pi / 2.0
        max_phi = pi / 2.0

        min_x = 0
        max_x = ImPano_W - 1.0
        min_y = 0
        max_y = ImPano_H - 1.0

        ## for x
        a = (max_theta - min_theta) / (max_x - min_x)
        b = max_theta - a * max_x  # from y=ax+b %% -a;
        nx = (1.0 / a) * (ntheta - b)

        ## for y
        a = (min_phi - max_phi) / (max_y - min_y)
        b = max_phi - a * min_y  # from y=ax+b %% -a;
        ny = (1.0 / a) * (nphi - b)
        lat = nphi.copy()
        xy_map = np.stack((nx, ny)).transpose(1, 2, 0)

        # 6. Final step interpolation and mapping
        # im = np.array(my_interpol.interp2linear(image360, nx, ny), dtype=np.uint8)
        im = grid_sample.numpy_grid_sample.default(
            image360.transpose(2, 0, 1), np.stack((ny, nx))
        ).transpose(1, 2, 0)
        if (
            f < fmin
        ):  # if it is a catadioptric image, apply mask and a disk in the middle
            r = diskradius(xi, f)
            DIM = im.shape
            ci = (np.round(DIM[0] / 2), np.round(DIM[1] / 2))
            xx, yy = np.meshgrid(
                list(range(DIM[0])) - ci[0], list(range(DIM[1])) - ci[1]
            )
            mask = np.double((np.multiply(xx, xx) + np.multiply(yy, yy)) < r * r)
            mask_3channel = np.stack([mask, mask, mask], axis=-1).transpose((1, 0, 2))
            im = np.array(np.multiply(im, mask_3channel), dtype=np.uint8)

        col = nphi[:, W // 2]
        zero_crossings_rows = np.where(np.diff(np.sign(col)))[0]
        if len(zero_crossings_rows) >= 2:
            print("WARNING | Number of zero crossings:", len(zero_crossings_rows))
            zero_crossings_rows = [zero_crossings_rows[0]]

        if len(zero_crossings_rows) == 0:
            offset = np.nan
        else:
            assert col[zero_crossings_rows[0]] >= 0
            assert col[zero_crossings_rows[0] + 1] <= 0
            dy = col[zero_crossings_rows[0] + 1] - col[zero_crossings_rows[0]]
            offset = zero_crossings_rows[0] - col[zero_crossings_rows[0]] / dy
            assert col[zero_crossings_rows[0]] / dy <= 1.0
        # Reproject [nx, ny+epsilon] back
        epsilon = 1e-5
        end_vector_x = nx.copy()
        end_vector_y = ny.copy() - epsilon
        # -5. pano to Sphere
        a = (max_theta - min_theta) / (max_x - min_x)
        b = max_theta - a * max_x  # from y=ax+b %% -a;
        ntheta_end = end_vector_x * a + b
        ## for y
        a = (min_phi - max_phi) / (max_y - min_y)
        b = max_phi - a * min_y
        nphi_end = end_vector_y * a + b
        # -4. sph 2 cart
        Y_Sph = np.sin(nphi)
        X_Sph = np.cos(nphi_end) * np.sin(ntheta_end)
        Z_Sph = np.cos(nphi_end) * np.cos(ntheta_end)
        # -3. Reverse Rotation of the sphere
        coords = np.vstack((X_Sph.ravel(), Y_Sph.ravel(), Z_Sph.ravel()))
        sph = rot_el.T.dot(rot_roll.dot(rot_az.T.dot(coords)))
        sph = sph.reshape((3, H, W)).transpose((1, 2, 0))
        X_Sph, Y_Sph, Z_Sph = sph[:, :, 0], sph[:, :, 1], sph[:, :, 2]

        # -1. Projection on the image plane

        X_Cam = X_Sph * f / (xi * csqrt(X_Sph**2 + Y_Sph**2 + Z_Sph**2) + Z_Sph) + u0
        Y_Cam = -Y_Sph * f / (xi * csqrt(X_Sph**2 + Y_Sph**2 + Z_Sph**2) + Z_Sph) + v0
        up = np.stack((X_Cam - grid_x, Y_Cam - grid_y)).transpose(1, 2, 0)
        up = normalize(up.reshape(-1, 2)).reshape(up.shape)

        return im, ntheta, nphi, offset, up, lat, xy_map


def draw_vanishing_opencv(
    img, horizon, vvp, pad=(1, 1), elevation=0, roll=0, azimuth=0, vfov=30
):
    if img.dtype == "uint8":
        img = img.astype(float) / 255
    im_h, im_w, im_c = img.shape
    canvas = np.ones((im_h * (pad[0] * 2 + 1), im_w * (pad[1] * 2 + 1), im_c))
    offset_h = pad[0] * im_h
    offset_w = pad[1] * im_w
    canvas[offset_h : offset_h + im_h, offset_w : offset_w + im_w, :] = img

    # Horizon
    if not np.isinf(horizon).any():
        cv2.line(
            canvas,
            (int(offset_w), int(offset_h + horizon[0] * im_h)),
            (int(offset_w + im_w), int(offset_h + horizon[1] * im_h)),
            (1, 0, 0),
            3,
        )

    if not np.isinf(vvp).any():
        # VVP
        vvp_abs = np.array([vvp[0] * im_w + offset_w, vvp[1] * im_h + offset_h])
        cv2.circle(canvas, (int(vvp_abs[0]), int(vvp_abs[1])), 5, (1, 0, 0), -1)

    # arrow
    gridx, gridy = np.meshgrid(
        np.arange(offset_w, offset_w + im_w + 20, 20),
        np.arange(offset_h, offset_h + im_h + 20, 20),
    )

    start = np.stack((gridx.reshape(-1), gridy.reshape(-1))).T

    if not np.isinf(vvp).any():
        arrow = normalize(vvp_abs - start) * vvp[2] * 30
    else:
        arrow = normalize(np.array([[im_h * (horizon[1] - horizon[0]), -im_w]])) * 30
    end = start + arrow

    start = start.astype(int)
    end = end.astype(int)
    for i in range(len(start)):
        cv2.arrowedLine(canvas, start[i], end[i], (0, 1, 0), thickness=1, tipLength=0.1)

    canvas = (255 * canvas).astype(np.uint8)
    # canvas = cv2.cvtColor(canvas, cv2.COLOR_BGR2BGR)
    # cv2.imwrite(os.path.join(save_path, prefix+'.png'), canvas)
    return canvas


def blend_color(img, color, alpha=0.2):
    if img.dtype == "uint8":
        foreground = img[:, :, :3]
    else:
        foreground = img[:, :, :3] * 255

    if color.dtype == "uint8":
        background = color[:, :, :3]
    else:
        background = color[:, :, :3] * 255

    alpha = np.ones_like(foreground) * alpha
    # Convert uint8 to float
    foreground = foreground.astype(float)
    background = background.astype(float)

    # Multiply the foreground with the alpha matte
    foreground = cv2.multiply(alpha, foreground)

    # Multiply the background with ( 1 - alpha )
    background = cv2.multiply(1.0 - alpha, background)

    # Add the masked foreground and background.
    outImage = cv2.add(foreground, background)

    outImage = outImage.astype(np.uint8)
    return outImage