-
Notifications
You must be signed in to change notification settings - Fork 998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
apply_affine_to_boxes
does not handle flipping properly, due to the left-closed and right-open nature of the boxes
#7740
Comments
Hi @function2-llx, I'm not exactly sure what outcome you're expecting. It appears the result is correct as the six digits represent coordinates in the XYZXYZ format. Thanks. |
@KumoLiu Hello, the format is indeed correct, however, the values of coordinates are mismatched with import torch
from monai.apps.detection.transforms.box_ops import apply_affine_to_boxes, flip_boxes
from monai.data import MetaTensor
import monai.transforms as mt
def main():
for flip_axis in range(3):
flip = mt.Flip(spatial_axis=flip_axis)
x = torch.zeros(1, 1, 1, 1)
flipped: MetaTensor = flip(x)
box = torch.tensor([[0, 0, 0, 1, 1, 1]])
box_flipped = apply_affine_to_boxes(box, flipped.affine.inverse())
print('wrong:', box_flipped)
box_flipped_correct = flip_boxes(box, x.shape[1:], flip_axis)
print('correct:', box_flipped_correct)
if __name__ == '__main__':
main() The results are:
|
Hi @function2-llx, the difference you're seeing arises from the second argument you're passing into the This comment in the discussion about the issue might also provide some helpful context. Thanks. |
I'm sorry if I misunderstand anything, but isn't the MONAI/monai/apps/detection/transforms/box_ops.py Lines 161 to 169 in e1a69b0
|
Hi @function2-llx,
Yes, for this |
Describe the bug
apply_affine_to_boxes
does not handle flipping properly, due to the left-closed and right-open nature of the boxes. That is to say, when a box is flipped, the left-closed and right-open coordinates become left-open and right closed, which should be maintained.To be clarified, I understand that there's a
flip_boxes
function available, but I want to use this example for simplicity. Same issue exists for more complicated affine matrix, as long as containing flipping.To Reproduce
Expected behavior
Produce the same results as
flip_boxes
.Actual Results
Suggestion
My suggestion for the fix is to convert the box coordinates to be closed on the both sides before applying the affine matrix, and convert them back to the desired format after applying the affine.
Additional Context
If I understand correctly, the left-closed and right open nature is suggested here:
MONAI/monai/data/box_utils.py
Lines 40 to 45 in e1a69b0
Feature Request
This is not about the major topic of this issue, but by the way, I would like to mention that in the code above I calculate the inverse of the affine matrix. Currently, I only find this way works to apply arbitrary affine transform to boxes. It would be nice if
apply_affine_to_boxes
could do it for be internally by solving a linear equation for numerical stability.The text was updated successfully, but these errors were encountered: