docstr: Tests that the cameras converted from opencv to pytorch3d convention
  return correct projections of random 3D points. The check is done
  against a set of results precomuted using `cv2.projectPoints` function.
docstr_tok: ["Tests","that","the","cameras","converted","from","opencv","to","pytorch3d","convention","return","correct","projections","of","random","3D","points",".","The","check","is","done","against","a","set","of","results","precomuted","using","`","cv2.projectPoints","`","function","."]
code: def test_opencv_conversion(self):
      """
      Tests that the cameras converted from opencv to pytorch3d convention
      return correct projections of random 3D points. The check is done
      against a set of results precomuted using `cv2.projectPoints` function.
      """
      device = torch.device("cuda:0")
      image_size = [[480, 640]] * 4
      R = [
          [
              [1.0, 0.0, 0.0],
              [0.0, 1.0, 0.0],
              [0.0, 0.0, 1.0],
          ],
          [
              [1.0, 0.0, 0.0],
              [0.0, 0.0, -1.0],
              [0.0, 1.0, 0.0],
          ],
          [
              [0.0, 0.0, 1.0],
              [1.0, 0.0, 0.0],
              [0.0, 1.0, 0.0],
          ],
          [
              [0.0, 0.0, 1.0],
              [1.0, 0.0, 0.0],
              [0.0, 1.0, 0.0],
          ],
      ]
  
      tvec = [
          [0.0, 0.0, 3.0],
          [0.3, -0.3, 3.0],
          [-0.15, 0.1, 4.0],
          [0.0, 0.0, 4.0],
      ]
      focal_length = [
          [100.0, 100.0],
          [115.0, 115.0],
          [105.0, 105.0],
          [120.0, 120.0],
      ]
      # These values are in y, x format, but they should be in x, y format.
      # The tests work like this because they only test for consistency,
      # but this format is misleading.
      principal_point = [
          [240, 320],
          [240.5, 320.3],
          [241, 318],
          [242, 322],
      ]
  
      principal_point, focal_length, R, tvec, image_size = [
          torch.tensor(x, device=device)
          for x in (principal_point, focal_length, R, tvec, image_size)
      ]
      camera_matrix = eyes(dim=3, N=4, device=device)
      camera_matrix[:, 0, 0], camera_matrix[:, 1, 1] = (
          focal_length[:, 0],
          focal_length[:, 1],
      )
      camera_matrix[:, :2, 2] = principal_point
  
      pts = torch.nn.functional.normalize(
          torch.randn(4, 1000, 3, device=device), dim=-1
      )
  
      # project the 3D points with the opencv projection function
      rvec = so3_log_map(R)
      pts_proj_opencv = cv2_project_points(
          pts, rvec, tvec, camera_matrix
      )
  
      # make the pytorch3d cameras
      cameras_opencv_to_pytorch3d = cameras_from_opencv_projection(
          R, tvec, camera_matrix, image_size
      )
      self.assertEqual(cameras_opencv_to_pytorch3d.device, device)
  
      # project the 3D points with converted cameras to screen space.
      pts_proj_pytorch3d_screen = (
          cameras_opencv_to_pytorch3d.transform_points_screen(pts)[
              ..., :2
          ]
      )
  
      # compare to the cached projected points
      self.assertClose(
          pts_proj_opencv, pts_proj_pytorch3d_screen, atol=1e-5
      )
  
      # Check the inverse.
      R_i, tvec_i, camera_matrix_i = opencv_from_cameras_projection(
          cameras_opencv_to_pytorch3d, image_size
      )
      self.assertClose(R, R_i)
      self.assertClose(tvec, tvec_i)
      self.assertClose(camera_matrix, camera_matrix_i)
  
file_path: pytorch3d/tests/test_camera_conversions.py
imports: import json
  import unittest
  import numpy
  import torch
  from pytorch3d.ops import eyes
  from pytorch3d.renderer.points.pulsar import Renderer
  from pytorch3d.transforms import so3_exp_map, so3_log_map
  from pytorch3d.utils import cameras_from_opencv_projection, opencv_from_cameras_projection, pulsar_from_opencv_projection
  from .common_testing import get_tests_dir, TestCaseMixin
label_desc: Use image node_id 3 for calling the TestCameraConversions obj's underlying member method code with example usage: obj.test_opencv_conversion() without return types