Extract HounsField Units of Segmentation and Save

Dear @lassoan
Hello lassoan ,
I read this and this 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
image

image

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

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

Thank you for reading!!

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


image

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

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

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.

image
image
image

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

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.

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

image
I want to get HU Values, when I click “Get HU Values” . but this error is occured…

I edit default Button’s code( “Apply”) like this photo.
image

image
image
image

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

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

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?

Thanks for reply.
I uploaded the source at here.
Please review it. :slight_smile:

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?

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.

These examples in the script repository should help.

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

You can see the edit source code here.

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.

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.

@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

@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 ??

@lassoan
Hello, My code is here 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.