Check if a 2D ROI bounding box is within another 2D ROI bounding box dimensions

Hi All, I have a use case where I need to check if ROI A’s 2D bounding box falls within ROI B’s 2D bounding box dimensions. Since each 2D bounding box dimensions are in format (startx, starty, x size, ysize) is there direct way to perform such comparison or we need to build custom logic for such comparison between 2 ROI’s 2D bounding boxes? Thanks Jiten

If you convert them into ImageRegions you can then use the crop method:
https://itk.org/Doxygen/html/classitk_1_1ImageRegion.html#ace7e0498fd5434e808036febf142ff7d

1 Like

Hi Bradley, this is great Idea! Let me try I out for my use case ! Will keep u posted ! Thanks, Jiten

Hi @blowekamp (Bradley) I converted ROI into Image Region using ExtractImageFilter in simple ITK, with index and size as expected for the ROIs. In the use case I am solving, the source of 2 ROI’s are from different segmentation process. While cropping one region by another, would it matter if pixel type of these 2 ROI Image regions are different? For example first ROI pixel type is 8 bit unsigned int second ROI pixel type is 32 bit unsigned int. Thanks Jiten

Regions are templeted only over dimension, while images both over dimension and pixel type. So pixel type doesn’t matter for regions.

1 Like

Important information that should have been included in your original questions:

  • What interface are you using? SimpleITK
  • How are the ROI/BB computed? You including the format. It is important to note that these ROIs are in index space defined by the image’s meta data.
  • The ROIs are from different images.
  • Do the images have the same spacing, origin and orientation? That is are the pixels physically congruent?

Fully understanding and being able to describe your problem is the first step to a solution. We have figured out much of the above, but the last question still remains, and if the images have different orientations or direction cosines, it will be a more complex problem.

2 Likes

Thanks much @dzenanz, @blowekamp for your feedback!!! It totally makes sense to get as much as detailed with my use case. In reality the proposals that you have provided pretty awesome opened new thought process altogether and was not even in my radar so while forming question. By implementing your ideas more questions surfaced up that I needed to address before I decide on final approach. Nonetheless, I will be providing more details of underlying image going forward with my use case. To answer last question, the rois from different images are congruent. I should be ready to perform 1 ImageRegion cropping from other Thanks a ton! Jiten

Hi @dzenanz, @blowekamp since I am using simple ITK. I created ImageRegions using ExtractImageFIlter then I tried to use CropImageFilter but not getting desired results. Below is code snippet am I doing any thing incorrectly? Let me know if you need more information.

Let Say for example the bounding box for 2 ROIs are:
bounding box of ROI 1 image (239, 215, 93, 85)
bounding box of ROI 2 image (208, 238, 81, 101)
current_lbl_bounding_box = (239, 215, 93, 85)

Snippet for Creation of Image Region for each bounding box
extract = sitk.ExtractImageFilter()
extract.SetSize([current_lbl_bounding_box[0], current_lbl_bounding_box[1]])
extract.SetIndex([current_lbl_bounding_box[2], current_lbl_bounding_box[3]])
extracted_image = extract.Execute(singleSliceOfLabelledImg)
print(‘ROI Image Information: ‘)
print(’\t ROI image region size: {0}’.format(extracted_image.GetSize()))
print(‘\t ROI image region spacing: {0}’.format(extracted_image.GetSpacing()))
print(‘\t ROI image region pixel type as String: ’ + extracted_image.GetPixelIDTypeAsString())
print(’\t ROI image region number of channels: ’ + str(extracted_image.GetNumberOfComponentsPerPixel()))
print('\t ROI Image Region Dimension ', extracted_image.GetDimension())

Using Crop Image Filter to perform filtering of one region from another.
ir1_bb = (239, 215, 93, 85)
ir2_bb = (208, 238, 81, 101)
crop = sitk.CropImageFilter()
crop.SetLowerBoundaryCropSize([ir1_bb[0], ir1_bb[1], 0])
crop.SetUpperBoundaryCropSize([ir1_bb[2], ir1_bb[3], 1])
cropped_image = crop.Execute(ir2)

The output of this operation I should expect some information about overlapping ROIs. I saw c++ examples where we have access directly to ImageRegion object on which we can use Crop method but how can we achieve same operation in simpleITK interface? Can you let me know if I am missing anything ? Thanks Jiten

There are 3 filter you should compare:

Each filter does a similar extraction of a sub-image but are parameterized differently. You are using both the Extract, and Crop filters incorrectly.

Lets assume you are getting your region from the LabelStatisticsImageFilter::GetRegion(). This method follows the SimpleITK convention for an image region: https://simpleitk.readthedocs.io/en/master/Documentation/docs/source/conventions.html. Which is [idx_x, idx_y, idx_z, size_x, size_y, size_z].

The easiest filter to use given the region would be the RegionOfInterestImageFilter with it’s SetRegionOfInterest which is the same format as your “bounding box”.

2 Likes

Thanks @blowekamp for you feedback! I do get Region information from LabelStatisticsImageFilter::GetRegion(). Post that I tried successfully to extract actual roi sub image using all 3 filters as you mentioned, but still what I am looking is to get handle or reference to ImageRegion object so that I can make call to the crop method as you suggested in your initial response. I cannot call crop method on LabelStatisticsImageFilter::GetRegion() as it would not be instance of ImageRegion due to which I cannot call crop method. In SimpleITK is there API to get ImageRegion instance? or can we derive and construct manually from other objects? Basically what I am looking to achieve is how can I convert ROIs to ImageRegion instance in SimpleITK.Thanks Jiten

Thanks @blowekamp, @dzenanz I was able to achieve filtering I required!! Thanks a lot for your suggestions