bbox icon indicating copy to clipboard operation
bbox copied to clipboard

Error in jaccard_index_3d when using quaternions

Open samwarren opened this issue 4 years ago • 6 comments

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

samwarren avatar Nov 12 '19 18:11 samwarren

Any luck figuring this out?

bokunobaka avatar Aug 07 '20 10:08 bokunobaka

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)

TCherici avatar May 07 '21 10:05 TCherici

Interesting. I'll look into this over the weekend.

varunagrawal avatar Feb 04 '22 16:02 varunagrawal

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.

varunagrawal avatar Jul 29 '22 20:07 varunagrawal

Specifically, the example given here involves 2 non-overlapping boxes, so the polygon collision function is returning True when it should be returning False.

varunagrawal avatar Jul 29 '22 20:07 varunagrawal

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)

gunnxx avatar Aug 31 '22 10:08 gunnxx