@@ -80,7 +80,7 @@ def _add_single_bbox3d_on_image(
8080
8181
8282def add_single_bbox3d_on_image (
83- image , box , proj , color = None , box_line_width = 2 ,
83+ image , box , proj , color = None , box_line_width = 2 , orthographic = False ,
8484):
8585 """" Add single 3D bounding box on a given image.
8686
@@ -91,6 +91,7 @@ def add_single_bbox3d_on_image(
9191 color(tuple): RGBA color of the bounding box. Defaults to None. If
9292 color = None the the tuple of [0, 255, 0, 255] (Green) will be used.
9393 box_line_width (int): line width of the bounding boxes. Defaults to 2.
94+ orthographic (bool): true if proj is orthographic, else perspective
9495 """
9596 img_height , img_width , _ = image .shape
9697
@@ -104,14 +105,20 @@ def add_single_bbox3d_on_image(
104105 bur = box .front_right_top_pt
105106 blr = box .front_right_bottom_pt
106107
107- fll_raster = _project_pt_to_pixel_location (fll , proj , img_height , img_width )
108- ful_raster = _project_pt_to_pixel_location (ful , proj , img_height , img_width )
109- fur_raster = _project_pt_to_pixel_location (fur , proj , img_height , img_width )
110- flr_raster = _project_pt_to_pixel_location (flr , proj , img_height , img_width )
111- bll_raster = _project_pt_to_pixel_location (bll , proj , img_height , img_width )
112- bul_raster = _project_pt_to_pixel_location (bul , proj , img_height , img_width )
113- bur_raster = _project_pt_to_pixel_location (bur , proj , img_height , img_width )
114- blr_raster = _project_pt_to_pixel_location (blr , proj , img_height , img_width )
108+ project_pt_to_pixel = (
109+ _project_pt_to_pixel_location_orthographic
110+ if orthographic
111+ else _project_pt_to_pixel_location
112+ )
113+
114+ fll_raster = project_pt_to_pixel (fll , proj , img_height , img_width )
115+ ful_raster = project_pt_to_pixel (ful , proj , img_height , img_width )
116+ fur_raster = project_pt_to_pixel (fur , proj , img_height , img_width )
117+ flr_raster = project_pt_to_pixel (flr , proj , img_height , img_width )
118+ bll_raster = project_pt_to_pixel (bll , proj , img_height , img_width )
119+ bul_raster = project_pt_to_pixel (bul , proj , img_height , img_width )
120+ bur_raster = project_pt_to_pixel (bur , proj , img_height , img_width )
121+ blr_raster = project_pt_to_pixel (blr , proj , img_height , img_width )
115122
116123 _add_single_bbox3d_on_image (
117124 image ,
@@ -129,7 +136,7 @@ def add_single_bbox3d_on_image(
129136
130137
131138def _project_pt_to_pixel_location (pt , projection , img_height , img_width ):
132- """ Projects a 3D coordinate into a pixel location.
139+ """ Projects a 3D coordinate into a pixel location from a perspective camera .
133140
134141 Applies the passed in projection matrix to project a point from the camera's
135142 coordinate space into pixel space.
@@ -161,3 +168,43 @@ def _project_pt_to_pixel_location(pt, projection, img_height, img_width):
161168 int ((_pt [1 ] * img_height ) / 2.0 + (img_height * 0.5 )),
162169 ]
163170 )
171+
172+
173+ def _project_pt_to_pixel_location_orthographic (
174+ pt , projection , img_height , img_width
175+ ):
176+ """ Projects a 3D coordinate into a pixel location from an orthographic camera.
177+
178+ Applies the passed in projection matrix to project a point from the
179+ camera's coordinate space into pixel space.
180+
181+ For a description of the math used in this method, see:
182+ https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-
183+ orthographic-projection-matrix/projection-matrix-introduction
184+
185+ Args:
186+ pt (numpy array): The 3D point to project.
187+ projection (numpy 2D array): The camera's 3x3 projection matrix.
188+ img_height (int): The height of the image in pixels.
189+ img_width (int): The width of the image in pixels.
190+
191+ Returns:
192+ numpy array: a one-dimensional array with two values (x and y)
193+ representing a point's pixel coordinate in an image.
194+ """
195+
196+ # The 'y' component needs to be flipped because of how Unity works
197+ projection = numpy .array (
198+ [
199+ [projection [0 ][0 ], 0 , 0 ],
200+ [0 , - projection [1 ][1 ], 0 ],
201+ [0 , 0 , projection [2 ][2 ]],
202+ ]
203+ )
204+ temp = projection .dot (pt )
205+
206+ pixel = [
207+ int ((temp [0 ] + 1 ) * 0.5 * img_width ),
208+ int ((temp [1 ] + 1 ) * 0.5 * img_height ),
209+ ]
210+ return pixel
0 commit comments