bbox
bbox copied to clipboard
Error in jaccard_index_3d when using quaternions
The following code:
from bbox import BBox3D
import pyquaternion
a = BBox3D(x=0, y=0, z=0, q=pyquaternion.Quaternion(axis=[1, 0, 0], angle=3.14159265))
b = BBox3D(x=1, y=0, z=0, q=pyquaternion.Quaternion(axis=[1, 0, 0], angle=3.14159265))
bbox.metrics.iou_3d(a,b)
produces the error
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-7-e3b42501e331> in <module>
----> 1 bbox.metrics.iou_3d(a,b)
~/PycharmProjects/thea_context/.venv/lib/python3.6/site-packages/bbox/metrics.py in iou_3d(a, b)
131 Alias for `jaccard_index_3d`.
132 """
--> 133 return jaccard_index_3d(a, b)
134
135
~/PycharmProjects/thea_context/.venv/lib/python3.6/site-packages/bbox/metrics.py in jaccard_index_3d(a, b)
152 return np.round_(0, decimals=5)
153
--> 154 intersection_points = polygon_intersection(a.p[0:4, 0:2], b.p[0:4, 0:2])
155 inter_area = polygon_area(intersection_points)
156
~/PycharmProjects/thea_context/.venv/lib/python3.6/site-packages/bbox/geometry.py in polygon_intersection(poly1, poly2)
171 input_list = output_list
172 output_list = []
--> 173 s = input_list[-1]
174
175 for e in input_list:
IndexError: list index out of range
Any luck figuring this out?
It doesn't have to be related to quaternions specifically. I managed to get the same error with the following code:
from bbox import BBox3D
from bbox.metrics import iou_3d
bbox_a = BBox3D(x=332.5, y=85.0, z=205.5, length=125, width=74, height=45)
bbox_b = BBox3D(x=332.5, y=144.0, z=366.5, length=113, width=44, height=65)
iou_3d(bbox_a, bbox_b)
Interesting. I'll look into this over the weekend.
I finally got time to look into this and the issue seems to be that the polygon collision code for 3D boxes isn't quite correct. I'll try fixing this soon.
Specifically, the example given here involves 2 non-overlapping boxes, so the polygon collision function is returning True when it should be returning False.
Perhaps we can use shapely to compute intersection area between two polygons. Quick example is here.
Details: Changing
intersection_points = polygon_intersection(a.p[0:4, 0:2], b.p[0:4, 0:2])
inter_area = polygon_area(intersection_points)
to
poly1 = shapely.geometry.Polygon(a.p[0:4, 0:2])
poly2 = shapely.geometry.Polygon(b.p[0:4, 0:2])
inter_area = poly1.intersection(poly2).area
However, you have to install shapely first (ie. pip install shapely)