# How to find the image match between the external reference with the annotations and the sequence in 3DSlicer?

**URL:** <https://discourse.slicer.org/t/how-to-find-the-image-match-between-the-external-reference-with-the-annotations-and-the-sequence-in-3dslicer/18145>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [June 15, 2021, 4:12pm UTC](https://discourse.slicer.org/t/how-to-find-the-image-match-between-the-external-reference-with-the-annotations-and-the-sequence-in-3dslicer/18145 "2021-06-15T16:12:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mostafa2020](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@Mostafa2020](https://discourse.slicer.org/u/Mostafa2020)\
**Post date:** [June 15, 2021, 4:12pm UTC](https://discourse.slicer.org/t/how-to-find-the-image-match-between-the-external-reference-with-the-annotations-and-the-sequence-in-3dslicer/18145/1 "2021-06-15T16:12:20Z")

</div>

Hi All,

I have annotation files that contain a box surrounding the tumor. In order to do the segmentation process in 3D Slicer and extract features, I am going to find the exact match between the annotation file and the same image in 3D Slicer. I wondered if there is a way to match the annotation image (surrounding box) with the coordinates that 3Dslicer gives about the position of the image in the sequence.

Thank you in advance.

 ![Screen Shot 2021-06-15 at 12.01.54 PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/e/ee34db0973ef03dfc63a0fac87dfa21e814712b7.jpeg)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 17, 2021, 1:58am UTC](https://discourse.slicer.org/t/how-to-find-the-image-match-between-the-external-reference-with-the-annotations-and-the-sequence-in-3dslicer/18145/2 "2021-06-17T01:58:49Z")

</div>

Yes, sure, it should be no problem. What software did you use to specify the boxes? What is the format of the annotation files? Can you copy the content of an example annotation file here?

---

<div class="post-metadata">

**Author:** ![Mostafa2020](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@Mostafa2020](https://discourse.slicer.org/u/Mostafa2020)\
**Post date:** [June 17, 2021, 3:15am UTC](https://discourse.slicer.org/t/how-to-find-the-image-match-between-the-external-reference-with-the-annotations-and-the-sequence-in-3dslicer/18145/3 "2021-06-17T03:15:07Z")

</div>

Hi Andras, The image annotations are saved as XML files in PASCAL VOC format and Python code is written to visualize the annotation boxes on top of the DICOM images. The following is the content of one of the annotation files for the first patient. Totally, there are 20 annotation files for the first patient, and I am going to find a way to match the annotation image (surrounding box) with the coordinates that 3D Slicer gives about the position of the image in the sequence.

> 

| /@verified | no |
| --- | --- |
| /filename | Unknow |
| /folder | Unknow |
| /object/bndbox/xmax | 360 |
| /object/bndbox/xmin | 278 |
| /object/bndbox/ymax | 394 |
| /object/bndbox/ymin | 308 |
| /object/Difficult | 0 |
| /object/name | A |
| /object/pose | Unspecified |
| /object/truncated | 0 |
| /path | Unknow |
| /segmented | 0 |
| /size/depth | 1 |
| /size/height | 512 |
| /size/width | 512 |
| /source | Unknow |
| /source/database | Unknown |

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 17, 2021, 3:51am UTC](https://discourse.slicer.org/t/how-to-find-the-image-match-between-the-external-reference-with-the-annotations-and-the-sequence-in-3dslicer/18145/4 "2021-06-17T03:51:15Z")

</div>

Probably the simplest is to write a short Python script that creates fills a numpy array with these ROI rectangles (you can use numpy indexing to fill a rectangular region with a value), then save the numpy array into a nrrd file using pynrrd.

Make sure the set origin, spacing, and axis directions of the nrrd file according to the DICOM file’s geometry. If you are not sure how to get this information then you can import the DICOM series into Slicer, save as nrrd, and use the same header.

---

<div class="post-metadata">

**Author:** ![Mostafa2020](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@Mostafa2020](https://discourse.slicer.org/u/Mostafa2020)\
**Post date:** [June 17, 2021, 3:57am UTC](https://discourse.slicer.org/t/how-to-find-the-image-match-between-the-external-reference-with-the-annotations-and-the-sequence-in-3dslicer/18145/5 "2021-06-17T03:57:52Z")

</div>

Great!! Would you please tell me if there is an example of Python code, which you mentioned, that I can use as a reference?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 17, 2021, 4:04am UTC](https://discourse.slicer.org/t/how-to-find-the-image-match-between-the-external-reference-with-the-annotations-and-the-sequence-in-3dslicer/18145/6 "2021-06-17T04:04:35Z")

</div>

You don’t need anything fancy, just create a numpy array using `data=np.zeros([512,512,512])`, fill each rectangle by numpy array indexing, such as `data[10:20, 30:40, 45]=1` and write the resulting file nrrd as shown [here](https://pynrrd.readthedocs.io/en/latest/user-guide.html#example-reading-and-writing-nrrd-files-with-c-order-indexing).

If you save the file with `.label.nrrd` file extension then Slicer will load it by default as a labelmap volume and if you use `.seg.nrrd` then Slicer will load it as a segmentation by default.

---

<div class="post-metadata">

**Author:** ![Mostafa2020](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@Mostafa2020](https://discourse.slicer.org/u/Mostafa2020)\
**Post date:** [June 17, 2021, 5:00am UTC](https://discourse.slicer.org/t/how-to-find-the-image-match-between-the-external-reference-with-the-annotations-and-the-sequence-in-3dslicer/18145/7 "2021-06-17T05:00:41Z")

</div>

Awesome!! Thanks so much. I will try it.
