# Extract HounsField Units of Segmentation and Save

**URL:** <https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [December 2, 2021, 5:56am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875 "2021-12-02T05:56:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [December 2, 2021, 5:56am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/1 "2021-12-02T05:56:35Z")

</div>

Dear @lassoan  
Hello lassoan ,  
I read [this](https://discourse.slicer.org/t/export-hounsfield-unit-values-as-a-table-from-each-segmentation/18296/15) and [this](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-histogram-of-a-segmented-region) too. And I tried to extract HounsField Units and checked saved file when I clicked “Reload and Test” button.  
These are result.csv and script code.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/5/e5a8d1832621cac17f3a046b90a255e344ce9139.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/e/fe6f0512d042894de5c3be8cfc9bb9489e1b892c.png)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/1/b11ddf250780ed146ef7ca6b9d51efaac4b58a63.png)

Next day I reboot my local pc then reload and test my module, permission error is appear.  
after a few minutes, When I retry there was no error…

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/3/634b42005486b70651367ea07ed336fdae320c44.png)

I want to obtain HU values for outputs made through the Segment editor, How do I set `masterVolumeNode` , `segmentationNode` , `labelValue` ???

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/6/c6187321e21ba6e13d8e4ce8628811c45466b225.png)

Thank you for reading!!

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [December 2, 2021, 3:29pm UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/2 "2021-12-02T15:29:23Z")

</div>

I edit code, but “getNode” is not recognized as a member function. and " \*sgmentVoxels" too.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/d/5def41b5af92b0c3fee674be7473bda540421f45.jpeg)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/5/15d3f50a502065e3df04f5693a10c5f264e53e9e.png)

I don’t want sample data’s Volume(2D View, R,G,Y) , How do I edit code??

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/d/1d3973b01c05895af2a6f4be4622882472136c8b.jpeg)

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [December 6, 2021, 9:33am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/3 "2021-12-06T09:33:16Z")

</div>

@lassoan I’m sorry for tagging you. Can you take a look when you have some free time?

---

<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:** [December 6, 2021, 1:37pm UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/4 "2021-12-06T13:37:34Z")

</div>

If you have `import slicer` in your module source code then you can access functions in `slicer.util` like this: `slicer.util.getNode('someNode')`.

Note that `getNode` method is intended for testing and debugging only. In scripted modules you usually don’t know in advance the ID or name of the node that the user will choose, but you get the selected node from a MRML node selector widget.

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [December 7, 2021, 12:11am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/5 "2021-12-07T00:11:42Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/7/178b4f723ef38aab42eea60fbb50bdb89c71ea71.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/a/ba129e63cebf331466642743498a3a66c38667c5.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/5/2561ebef699cb33d8444c486d6c85ad400fd0c94.png)

I edit the code like capture, but An error has appeared…

---

<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:** [December 7, 2021, 3:40am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/6 "2021-12-07T03:40:52Z")

</div>

You should get the input `segmentationNode` and `masterVolumeNode` from your node selectors - something like `segmentationNode = self.ui.segmentationNodeSelector.currentNode()`.

You set the output `labelmapVolumeNode` correctly in the `...AddNewNodeByClass...` line. The `labelmapVolumeNode = slicer.util.getNode...` line should be deleted.

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [December 8, 2021, 11:59pm UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/7 "2021-12-08T23:59:11Z")

</div>

Thanks for reply and sorry to check lately.  
I edit segemntationNode,labelmapVolumeNode,masterVolumeNode like this.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/8/98bb154a9c6f3db0326842166aba1f8a9abd13b4.png)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/f/2fae91769757905e15789e5896252c149d13f422.png)  
I want to get HU Values, when I click “Get HU Values” . but this error is occured…

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [December 10, 2021, 9:06am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/8 "2021-12-10T09:06:15Z")

</div>

I edit default Button’s code( “Apply”) like this photo.  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/6/d664b17daeaf17c1fc5f25991a78f3f733ccb7fd.png)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/5/b58a67364fc99e1ba156cc4e8b6a8886a676758a.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/7/17ab07be7e4798786a31912ff0122787cc44270a.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/7/07017dd010e0c6bb68819b937a9eb72ee80b5110.png)

And when I click Apply Button, result img and csv file are these .

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/a/3aaf005d8055da9903b6c47118e3d19a97c7a077.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/e/ee450bcd69dab8f6b15d6a483feb66f80abc77f6.png)

Now Volume Display is update, but I can’t find HU Values and don’t want to show Red Circle Volume in Volume Display…

---

<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:** [December 10, 2021, 6:03pm UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/9 "2021-12-10T18:03:59Z")

</div>

We cannot review and comment on source code in screenshots. Could you upload your module (including all the files that the Extension Wizard generated) to github and post the link here?

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [December 12, 2021, 3:30am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/10 "2021-12-12T03:30:09Z")

</div>

Thanks for reply.  
I uploaded the source at [here](https://github.com/kookoo9999/HounsFieldUnit).  
Please review it. 🙂

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [December 13, 2021, 9:07am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/11 "2021-12-13T09:07:42Z")

</div>

I want to get the HU Values(x,y,z,HU) from Segment(via 3D Slicer Segmentation Editor’s output).

I tried debugging using VS Code, and I checked that labelmapVolumeNode had no value. And it is also true that we can’t find a way to check the volume and segment.  
If you don’t mind, can you show me your source code?

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [December 21, 2021, 12:09am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/12 "2021-12-21T00:09:49Z")

</div>

Hello , @lassoan  
How do I get the Hounsfield Unit Values for the Segment Volume that I made using SegmentEditor? I should like to have the benefit of your advice.  
You can check my source code at [here](https://github.com/kookoo9999/HounsFieldUnit).

---

<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:** [December 21, 2021, 5:45am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/13 "2021-12-21T05:45:06Z")

</div>

These [examples in the script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#quantifying-segments) should help.

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [December 21, 2021, 7:31am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/14 "2021-12-21T07:31:40Z")

</div>

Thanks for reply!  
There was an error that I couldn’t find the getNode, so I modified it to slicer.util.getNode, and this error came out.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/d/8d01688e8e05d7db0c896eff08f792f29321d8a3.png)

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [December 21, 2021, 7:54am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/15 "2021-12-21T07:54:48Z")

</div>

You can see the edit source code [here](https://github.com/kookoo9999/HounsFieldUnit).

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [January 5, 2022, 11:51pm UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/16 "2022-01-05T23:51:29Z")

</div>

I occured this message "AttributeError : ‘MRMLCore.vtkMRMLScalarVolumeNode’ object has no attribute ‘GetSegmentation’ from this line “segmentationNode.GetSegmentation().GetNthSegmentID(0)” when I clicked the ‘Get HU Values’ button.  
You can check ‘Get HU Values’ button’s all source at [here](https://github.com/kookoo9999/HounsFieldUnit/blob/main/HounsFieldUnit.py).

---

<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:** [January 6, 2022, 1:13am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/17 "2022-01-06T01:13:37Z")

</div>

The error message is truncated. Always copy-paste the entire error message _text_ (in addition to that you can also attach screenshots but only if they contain more information than just a textbox).

The error message tells that you passed a volume node as an argument to Segment Statistics module, but it expected that argument to be a segmentation node instead.

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [January 6, 2022, 3:03am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/18 "2022-01-06T03:03:06Z")

</div>

@lassoan Thanks for reply.  
This is full error message.  
Traceback (most recent call last):  
File “C:/Slicer\_Module/HounsFiledUnit/src/HounsFieldUnit/HounsFieldUnit/HounsFieldUnit.py”, line 358, in onGetHUButton  
segmentId = segmentationNode.GetSegmentation().GetNthSegmentID(0)  
AttributeError: ‘MRMLCore.vtkMRMLScalarVolumeNode’ object has no attribute ‘GetSegmentation’

and This is photo of full code of Get HU Buttons.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/d/edb369331a5260e75dba66c00abdaa60161fe85a.png)

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [January 7, 2022, 2:44am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/19 "2022-01-07T02:44:12Z")

</div>

@lassoan Hello, lassoan  
I modified code to this and get result by .csv format.

> ```
> segmentationNode = slicer.util.getNode("Segment_1_1")
> 
> masterVolumeNode = slicer.util.getNode("Segment")
> 
> labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
> 
> slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode,labelmapVolumeNode,masterVolumeNode)
> 
> volumeArray = slicer.util.arrayFromVolume(masterVolumeNode)
> 
> labelArray = slicer.util.arrayFromVolume(labelmapVolumeNode)
> 
> labelValue = 1
> 
> segmentVoxels = volumeArray[labelArray==labelValue]
> 
> import numpy as np
> 
> coordinates = np.where(labelArray==labelValue)
> 
> hu = volumeArray[coordinates]
> 
> coordinateWithHU = np.zeros([len(coordinates[0]),4])
> 
> coordinateWithHU[:,0:3] = np.array(coordinates).T
> 
> coordinateWithHU[:,3] = hu
> 
> slicer.util.pip_install('pandas')
> 
> import pandas as pd
> 
> pd.DataFrame(coordinateWithHU).to_csv("c:/Extraction_IJK_of_HU.csv")
> 
> ```

and I know colum ‘A’ means count of voxels, but I don’t know what rows(0,1,2,3) mean.  
what is HU Values ??

---

<div class="post-metadata">

**Author:** ![kookoo9999](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kookoo9999/32/12394_2.png) [@kookoo9999](https://discourse.slicer.org/u/kookoo9999)\
**Post date:** [March 22, 2022, 6:08am UTC](https://discourse.slicer.org/t/extract-hounsfield-units-of-segmentation-and-save/20875/20 "2022-03-22T06:08:46Z")

</div>

@lassoan  
Hello, My code is [here](https://github.com/kookoo9999/HounsFieldUnit) and you can check data I used in “data” directory.  
After I input the sample directory’s data and occured coordinates(maybe x,y,z,hu) but HU(HounsField Unit) value is number one(1)…  
I can’t understand why HU is 1. I think HU get coordinates(x,y,z) each HU values and these values are not same.  
Can you review my code??  
Thank you.
