Simple ITK Crop Image

Hey guys:

I have a 3D grey-level image (256, 256, 192);
I have a 3D label-image binary (1 and 0) to define a segmentation (256, 256, 192);

I use LabelShapeStatisticsImageFilter to get a bounding box or region for my label image;
The region/boundingbox output is something like this: [115, 58, 122,32, 18, 8]

I then use that region info to crop this bounding box region from grey-level image;
The crop filter takes an image, lowerBoundaryCropSize and upperBoundaryCropSize.

Question: How to find out those boundary indexes from region info?

I tried many combinations, like three first numbers for lower boundary, and the rest for upper boundary. Also subtracting the last three numbers from initial ones and set is as either lower or upper boundary. No success.

Error:The input image’s size [256, 256, 192] is less than the total of the crop size!

thaks in advance;

The return follows the SimpleITK common convention for converting a region into a array: [idx_x, idx_y, idx_z, size_x, size_y, size_z].

The RegionOfInterest filter has the index and size parameters which conveniently works with the output values produced.

1 Like

Excellent. Used and Worked.
A single observation; RegionOfInterest takes as input (Image, Size, Index) rather than (Image, Region) .

Hi, how could I use the bounding box of one image to crop another? Say I have two images: imgA and imgB. I first get a bounding box from imgA via the following:

label_shape_filter = sitk.LabelShapeStatisticsImageFilter()
label_shape_filter.Execute(imgA)
bounding_box = label_shape_filter.GetBoundingBox(1) 

which returns a bounding box of (154, 314, 210, 284, 8, 44). I then want to crop imgB using this bounding box. How could I achieve this using RegionOfInterest?

Hello @machadoL,

Work with the images as physical objects (assuming the axes of both images are aligned):

p1 = imgA.TransformIndexToPhysicalPoint(bounding_box[0 : int(len(bounding_box) / 2)])
p2 = imgA.TransformIndexToPhysicalPoint([x+sz for x,sz in zip(bounding_box[0 : int(len(bounding_box) / 2)], bounding_box[int(len(bounding_box) / 2) :])])

# crop using the indexes computed for imgB
imgB_start_index = imgB.TransformPhysicalPointToIndex(p1)
imgB_end_index = imgB.TransformPhysicalPointToIndex(p2)
2 Likes