# OBB bounding Box not correct

**URL:** <https://discourse.slicer.org/t/obb-bounding-box-not-correct/39522>\
**Category:** Support\
**Tags:** python\
**Created:** [October 3, 2024, 2:11pm UTC](https://discourse.slicer.org/t/obb-bounding-box-not-correct/39522 "2024-10-03T14:11:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![G\_Tom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/g_tom/32/68257_2.png) [@G\_Tom](https://discourse.slicer.org/u/G_Tom)\
**Post date:** [October 3, 2024, 2:11pm UTC](https://discourse.slicer.org/t/obb-bounding-box-not-correct/39522/1 "2024-10-03T14:11:58Z")

</div>

I use this code to compute and visualize the bounding box but it is not correct. Not sure why:

> ```
> # Compute bounding boxes
> import SegmentStatistics
> segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
> segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
> segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.obb_origin_ras.enabled",str(True))
> segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.obb_diameter_mm.enabled",str(True))
> segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.obb_direction_ras_x.enabled",str(True))
> segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.obb_direction_ras_y.enabled",str(True))
> segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.obb_direction_ras_z.enabled",str(True))
> segStatLogic.computeStatistics()
> stats = segStatLogic.getStatistics()
> 
> # Draw ROI for each oriented bounding box
> import numpy as np
> for segmentId in stats["SegmentIDs"]:
> # Get bounding box
> obb_origin_ras = np.array(stats[segmentId,"LabelmapSegmentStatisticsPlugin.obb_origin_ras"])
> obb_diameter_mm = np.array(stats[segmentId,"LabelmapSegmentStatisticsPlugin.obb_diameter_mm"])
> obb_direction_ras_x = np.array(stats[segmentId,"LabelmapSegmentStatisticsPlugin.obb_direction_ras_x"])
> obb_direction_ras_y = np.array(stats[segmentId,"LabelmapSegmentStatisticsPlugin.obb_direction_ras_y"])
> obb_direction_ras_z = np.array(stats[segmentId,"LabelmapSegmentStatisticsPlugin.obb_direction_ras_z"])
> # Create ROI
> segment = segmentationNode.GetSegmentation().GetSegment(segmentId)
> roi=slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsROINode")
> roi.SetName(segment.GetName() + " OBB")
> roi.GetDisplayNode().SetHandlesInteractive(False) # do not let the user resize the box
> roi.SetSize(obb_diameter_mm)
> # Position and orient ROI using a transform
> obb_center_ras = obb_origin_ras+0.5*(obb_diameter_mm[0] * obb_direction_ras_x + obb_diameter_mm[1] * obb_direction_ras_y + obb_diameter_mm[2] * obb_direction_ras_z)
> boundingBoxToRasTransform = np.row_stack((np.column_stack((obb_direction_ras_x, obb_direction_ras_y, obb_direction_ras_z, obb_center_ras)), (0, 0, 0, 1)))
> boundingBoxToRasTransformMatrix = slicer.util.vtkMatrixFromArray(boundingBoxToRasTransform)
> roi.SetAndObserveObjectToNodeMatrix(boundingBoxToRasTransformMatrix)
> 
> ```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/9/b980b9d8e221c35da7db39ce16f1c0d409f2a971.png)

Thanks for your suggestions on how to correct this ?
