# Create new segment from binary vtkimagedata object

**URL:** <https://discourse.slicer.org/t/create-new-segment-from-binary-vtkimagedata-object/22960>\
**Category:** Support\
**Created:** [April 14, 2022, 10:21am UTC](https://discourse.slicer.org/t/create-new-segment-from-binary-vtkimagedata-object/22960 "2022-04-14T10:21:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haytham](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@Haytham](https://discourse.slicer.org/u/Haytham)\
**Post date:** [April 14, 2022, 10:21am UTC](https://discourse.slicer.org/t/create-new-segment-from-binary-vtkimagedata-object/22960/1 "2022-04-14T10:21:26Z")

</div>

Hello,  
I create a 3D vtkImageData objet using this code:

``  
**vtkNew imageTumor;**  
\*\* imageTumor-\>SetExtent(0, 100, 0, 100, 0, 100);\*\*  
\*\* imageTumor-\>AllocateScalars(VTK\_DOUBLE, 1);\*\*  
\*\* imageTumor-\>SetSpacing(1.5, 2.25, 8);\*\*  
\*\* imageTumor-\>SetOrigin(0.0, 0.0, 0.0);\*\*  
\*\* int\* extent = imageTumor-\>GetExtent();\*\*

```auto

And I inisilize this image by 0 and 1 depending on a certain conditions, using this code:

```

**for (int x = extent[0]; x \< extent[1]; x++)**  
\*\* {\*\*  
\*\* for (int y = extent[2]; y \< extent[3]; y++)\*\*  
\*\* {\*\*  
\*\* for (int z = extent[4]; z \< extent[5]; z++)\*\*  
\*\* {\*\*  
\*\* if(imageMTT-\>GetScalarComponentAsDouble(x, y, z, 0)\>150)\*\*  
\*\* imageTumor-\>SetScalarComponentFromDouble(x, y, z, 0, 1.0);\*\*  
\*\* else\*\*  
\*\* imageTumor-\>SetScalarComponentFromDouble(x, y, z, 0, 0.0);\*\*  
\*\* }\*\*  
\*\* }\*\*  
\*\* }\*\*

```auto

the question is how can visualize this image? i don't know if we can transform a vtkimagedata ona segment. any ideas to help me.
thanks
```

---

<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:** [April 14, 2022, 12:46pm UTC](https://discourse.slicer.org/t/create-new-segment-from-binary-vtkimagedata-object/22960/2 "2022-04-14T12:46:59Z")

</div>

You can import a vtkImageData into a vtkMRMLSegmentationNode using [ImportLabelmapToSegmentationNode](http://apidocs.slicer.org/master/classvtkSlicerSegmentationsModuleLogic.html#afd85ead2153b7e973600f0b27b0583cd) method.

A small comment: `GetScalarComponentAsDouble` is intended for testing or get/set single values in a volume. It is not intended for filling an entire volume. It may be 10x or more slower than using VTK image filters or iterating through an image using a pointer. The loop in the example above is also much slower because the hottest loop jumps between slices instead of processing neighbor voxels (`for (int x... for (int y... for (int z...` should be changed to `for (int z... for (int y... for (int x...`). Processing all voxels in a single thread also slows down the operation. I would recommend to either learn more about how to implement image processing filters or use existing VTK filters to achieve reasonable processing speed. If you describe the processing you want to do (`initialize this image by 0 and 1 depending on a certain conditions`) then we may give advice on how to fill the volume more efficiently.

---

<div class="post-metadata">

**Author:** ![Haytham](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@Haytham](https://discourse.slicer.org/u/Haytham)\
**Post date:** [April 14, 2022, 1:00pm UTC](https://discourse.slicer.org/t/create-new-segment-from-binary-vtkimagedata-object/22960/3 "2022-04-14T13:00:01Z")

</div>

> [@lassoan](#):
>
> all voxels in a single thread

thanks for your responce and the important comment

I try to select the value of Perfusion parameters (MTT, TPP, rCBV, rCBF) from a vtkImageData object. And depending on those parameters, Idecide whether this voxel contains tumors or not.  
After this, we obtain a vtkImagedata object (0 or 1) I need to visualize this object…  
thanks

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [April 14, 2022, 1:46pm UTC](https://discourse.slicer.org/t/create-new-segment-from-binary-vtkimagedata-object/22960/4 "2022-04-14T13:46:11Z")

</div>

I believe you could use simpleITK for creating the label map from a numpy array. Should be easier I think.

Hope it helps

---

<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:** [April 14, 2022, 5:07pm UTC](https://discourse.slicer.org/t/create-new-segment-from-binary-vtkimagedata-object/22960/5 "2022-04-14T17:07:16Z")

</div>

I agree that it would make sense to start prototyping in Python and then if you need better speed then vectorize the code and/or port slowest parts to C++. By “vectorizing” the operations I mean process an entire line, slice, or volume of voxels at a time (using numpy, ITK, or other tools) then you may be able to implement the whole computation in Python.

Note that there have been previous projects for processing time-resolved images, such [PK modeling extension](https://github.com/QIICR/PkModeling) for analyzing DCE-MRI.

I’m not sure if there are CT perfusion modeling extensions (or algorithms that could be easily converted to extensions) for computing MTT, TP, rCBV, rCBF. @fedorov has features like this been developed as part of the QIICR project?

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [April 14, 2022, 8:06pm UTC](https://discourse.slicer.org/t/create-new-segment-from-binary-vtkimagedata-object/22960/6 "2022-04-14T20:06:55Z")

</div>

> [@lassoan](#):
>
> I’m not sure if there are CT perfusion modeling extensions (or algorithms that could be easily converted to extensions) for computing MTT, TP, rCBV, rCBF. @fedorov has features like this been developed as part of the QIICR project?

No, nothing like that was done in QIICR. There is also this relevant extension, but it is for PET: [Documentation/Nightly/Modules/dPetBrainQuantification - Slicer Wiki](https://www.slicer.org/w/index.php/Documentation/Nightly/Modules/dPetBrainQuantification).
