> For the complete documentation index, see [llms.txt](https://docs.imerit.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.imerit.net/data/importing-and-exporting-annotations/importing-annotations/importing-nrrd-annotations.md).

# Importing NRRD Annotations

You can import Medical Brush segmentations and rotated bounding boxes as pre-annotations for NRRD assets in the [Medical Labeling Editor](/labeling/labeling-editor-interface/medical-labeling-editor.md).

Medical Brush segmentations are imported from an NRRD segmentation mask. Rotated bounding boxes are included as objects in the Cloud Import JSON used to add the NRRD asset.

## How to Import NRRD Segmentations

### Prepare the project ontology

To match the segmentation classes in your NRRD with the segmentation classes on Hub, there are two ways. Either:

* you'll make it so that the title of the class on Ango Hub matches the `SegmentX_Name` value in the header of the NRRD file, or
* you'll make it so that that the `schemaId` of the class on Ango Hub matches the `SegmentX_ID` value in the header of the NRRD file.

Ango Hub will first try to match the segment name with the class title, then as fallback it will attempt to match the class schemaId with the segment's ID. If a match for your segment is then still not found, that segment is not imported.

For example, imagine the NRRD segmentation you wish to import has one segmentation class, and you wish for this class to be paired with a class on Ango Hub. This is how you would go about it:

1. Open the Segmentation NRRD you wish to import into Ango Hub with a text editor. The first few lines (e.g. the file header) will likely look something like this:

```
[more ...]
space origin: (100.331,-86.50509999999997,88.181799999999967)
Segment0_Color:=0.501961 0.682353 0.501961
Segment0_ColorAutoGenerated:=1
Segment0_Extent:=40 207 13 235 0 90
Segment0_ID:=Segment_1
Segment0_LabelValue:=1
Segment0_Layer:=0
Segment0_Name:=Segment_1
Segment0_NameAutoGenerated:=1
Segment0_Tags:=Segmentation.Status:inprogress|TerminologyEntry:Segmentation category and type - 3D Slicer General Anatomy list~SCT^85756007^Tissue~SCT^85756007^Tissue~^^~Anatomic codes - DICOM master list~^^~^^|
[... more]
```

2. Each segmentation class in the NRRD is represented by metadata starting with the prefix `SegmentX`, where X is an index starting from 0. All metadata pertaining to a class will have the same index. What we care about is the `SegmentX_LabelValue` value. In this case, since there is only one segmentation, its index will be 0, so we will look at `Segment0_LabelValue` only. In our case, this value is 1. We'll take note of that.
3. In the project where you'd like to import the NRRD segmentation, go to the *Settings* tab, then to the *Category Schema* section. Create a new *Medical Brush* tool. By default, schema IDs for Medical Brush tools start at 1, so in this case, since the schema ID matches the number we took note of earlier, there would be no need to change the schema ID.
4. If we did need to change the schema ID, then we would click on the *Show JSON* button (1) and change the ID from the JSON that appears to match `SegmentX_LabelValue`'s value.

<figure><img src="/files/zZCqwxfIWhuN6a571H29" alt=""><figcaption></figcaption></figure>

5. Save your category schema by clicking on *Save* at the bottom of the page.

### Upload local NRRD segmentation files

You can upload NRRD segmentation masks directly from your computer and match them to existing NRRD assets by filename.

1. Name each segmentation file after the external ID of the asset you want to pre-annotate. For example, both `my_asset.seg.nrrd` and `my_asset.nrrd` match an asset whose external ID is `my_asset.nrrd`.
2. From your project's *Assets* tab, click *Import*.
3. In the *Import Labels* tab, drag and drop or select one or more `.nrrd` or `.seg.nrrd` files.

You can select multiple segmentation files at once, or select JSON and NRRD files together. Ango Hub uploads the segmentation masks and matches each one to its asset. Notifications summarize files that could not be uploaded or matched.

{% hint style="warning" %}
Pre-annotations can only be imported to tasks in the Start stage.
{% endhint %}

### Import NRRD segmentations from URLs

Use an import JSON when your NRRD segmentation masks are already hosted at accessible URLs.

1. Create a JSON with this format:

#### For Segmentations with Publicly Accessible URLs

```json
[
  {
    "data": "https://my-bucket.com/asset.nrrd",
    "externalId": "asset_external_id",
    "medicalBrushDataUrl": "https://medical.url/sample-segmentation.nrrd"
  },
  {
    "data": "https://my-bucket.com/asset.nrrd",
    "externalId": "asset_external_id",
    "medicalBrushDataUrl": "https://medical.url/sample-segmentation.nrrd"
  }
]
```

Where `externalId` is the external ID of the asset to pre-annotate, and `medicalBrushDataUrl` is a link to your publicly-accessible NRRD segmentation mask.

{% hint style="warning" %}
If your segmentations are stored in a cloud storage service, ensure the bucket's CORS settings are set properly, or they will not be visible on Hub.

[Here is a guide](/data/storages/set-up-cors.md) on how to set up CORS in such a way that your masks will be visible.
{% endhint %}

#### For Segmentations in Private Buckets

* Integrate your private bucket with Ango Hub by following the instructions in the [Storages](/data/storages.md) docs page.
* Navigate to your organization's *Organization* page, then navigate to the *Storages* tab.
* Copy the ID of the storage where your brush traces are stored.
* Prepare a JSON with the following format:

```json
[
  {
    "data": "https://my-bucket.com/asset.nrrd",
    "externalId": "asset_external_id",
    "medicalBrushDataUrl": "https://medical.url/sample-segmentation.nrrd?storageId=YOUR_STORAGE_ID"
  },
  {
    "data": "https://my-bucket.com/asset.nrrd",
    "externalId": "asset_external_id",
    "medicalBrushDataUrl": "https://medical.url/sample-segmentation.nrrd?storageId=YOUR_STORAGE_ID"
  }
]
```

The JSON will be a list of objects, where each object represents an asset-segmentation pair. The `data` and `externalId` fields are used by Hub to understand to which asset the prelabels will be imported. You do not need to provide both, either of them is enough.

2. From your project's dashboard, enter the *Assets* tab, then click on *Import.* A dialog will pop up.

<figure><img src="/files/BNOoiXaAC2xaK4UCbXyS" alt=""><figcaption></figcaption></figure>

3. Drag and drop the JSON you created earlier on the box. Your assets will be prelabeled.

## How to Import Rotated Bounding Boxes into NRRD Assets

First, add a Rotated Bounding Box class to the project's category schema. Copy its `schemaId` from the category schema JSON.

Then, prepare a Cloud Import JSON containing the NRRD asset and an `objects` array. The `schemaId` of each object must match the Rotated Bounding Box class in the project. For the canonical annotation schema, see [3D Rotated Bounding Box in the Ango Import Format](/data/importing-and-exporting-annotations/importing-annotations/ango-import-format.md#id-3d-rotated-bounding-box).

```json
[
  {
    "data": "https://my-bucket.com/asset.nrrd",
    "externalId": "asset.nrrd",
    "objects": [
      {
        "objectId": "medical-rotated-box-1",
        "schemaId": "medical_rotated_bbox",
        "title": "Lesion",
        "rotated-bounding-box": {
          "x1": 48,
          "y1": 64,
          "z1": 96,
          "x2": 112,
          "y2": 144,
          "z2": 128,
          "rotation": 28,
          "rotationPlane": "AXIAL",
          "coordinates1": [-110.25, -92.5, -24.75],
          "coordinates2": [-46.25, -12.5, 7.25],
          "measurement": {
            "width": {
              "distance": 51.77,
              "unit": "mm"
            },
            "height": {
              "distance": 64.61,
              "unit": "mm"
            },
            "depth": {
              "distance": 32,
              "unit": "mm"
            }
          }
        }
      }
    ]
  }
]
```

The rotated bounding box fields are:

| Field                                 | Description                                                                                     |
| ------------------------------------- | ----------------------------------------------------------------------------------------------- |
| `x1`, `y1`, `z1` and `x2`, `y2`, `z2` | Two opposite corners of the box in voxel coordinates.                                           |
| `rotation`                            | The rotation angle in degrees.                                                                  |
| `rotationPlane`                       | The view in which the rotation is applied: `AXIAL`, `CORONAL`, or `SAGITTAL`.                   |
| `coordinates1` and `coordinates2`     | Optional world-space coordinates for the two corners.                                           |
| `measurement`                         | Optional physical width, height, and depth values. Each value contains a `distance` and `unit`. |

The rotation plane also determines the box's depth axis:

| Rotation plane | Width and height axes | Depth axis |
| -------------- | --------------------- | ---------- |
| `AXIAL`        | X and Y               | Z          |
| `CORONAL`      | X and Z               | Y          |
| `SAGITTAL`     | Y and Z               | X          |

{% hint style="info" %}
`coordinates1`, `coordinates2`, and `measurement` should correspond to the NRRD volume's origin, orientation, and spacing. If you omit them, Ango Hub calculates them when the box is edited.
{% endhint %}

From the project's *Assets* tab, click *Add Data*, open the *Cloud Storage* tab, and upload the JSON. When the NRRD task opens, the box is available in the Axial, Coronal, and Sagittal views and in the 3D Viewer.
