How to get correct mask value from one segment from Segment Editor?

Operating system: win10 64-bit
Slicer version:4.9.0-2017-10-23 r26509

Expected behavior: the mask value of GetBinaryLabelmapRepresentation() is same with original mask
Actual behavior:mask value which got from Binary GetBinaryLabelmapRepresentation() is different with original inputted mask value.

Hi SlicerExperts:

I am trying to show a segment with the mask value from a json file,below is my steps:
 1.create a vtkImageData,and set the basic values from below lines:
        "mask": {
            "zBegin": "19.104156494140625",
            "xBegin": "67.1884765625",
            "yEnd": "-103.6318359375",
            "zEnd": "25.404266357421875",
            "yBegin": "-99.7353515625",
            "sliceMask": {
                "count": "10",
                "item": "000000000000000000010000000000000000000000000000",
                "item": "000000000111100001111000011111000011100000000000",
                "item": "011110000111110011111100111111000111110000110000",
                "item": "011111001111111011111110111111100111110001111000",
                "item": "011111001111111011111111111111101111111001111100",
                "item": "011111001111111011111111111111111111111001111100",
                "item": "011110001111110011111111111111110111111001111100",
                "item": "001100000111110011111110111111100111110000111000",
                "item": "000000000111100001111000011110000011100000000000",
                "item": "000000000000000000000000000100000000000000000000",
          }
 2. create a vtkMarchingCubes and passed step1's result to setInputData () when creating a corresponding polydata;
 3. create one modelNode ,SetAndObservePolyData(step1's polydata) and  segmentationNode ,used ImportModelToSegmentationNode(modelNode, segmentationNode) to bind the modelNode to segmentationNode;
 4.call rep = segmentationNode.GetBinaryLabelmapRepresentation(segmentID) to get the  specific segment, and assigned   arr = rep.GetPointData().GetArray(0) to get all of the points of this Segment,and output below mask values:
	outputmask=['000000000000000000001000000000000000000000000000', 
                                   '000000000001111000011110001111100001110000000000', 
                                     '000111100011111000111111001111110011111000001100', 
                                      '001111100111111101111111011111110011111000011110', 
                                       '001111100111111111111111011111110111111100111110', 
                                       '001111100111111111111111111111110111111100111110', 
                                       '000111100011111111111111111111110111111000111110', 
                                     '000011000011111001111111011111110011111000011100', 
	                          '000000000001111000011110000111100001110000000000', 
                                   '000000000000000000000000000010000000000000000000']

The result can not match original mask values,it seems there are some data have been changed ,we want to get the mask value of segment is same with the original data,could you give us some ideas?

Thanks,
Chunbo

I am a newbie who has no idea. So if you send code segment i freeze. Would you be kind enough to delineate a bit further?

Marching cubes algorithm is for generating smooth surfaces. In general, conversion between smooth surface and binary labelmap representations is a lossy operation. If you don’t need smooth surface then you can use vtkDiscreteMarchingCubes algorithm instead, which generates small cubes (see picture below - left: smooth surface, right: cubes):

1 Like

Thanks lassoan,

I have tried vtkDiscreteMarchingCubes,the result is same:can not match original mask values in json file.

It seems that we can not create MarchingCubes object,correct?

Best,
Chunbo

I cannot answer your question without actually knowing what is the context, what is the high-level goal of your of your project and what specific problem you are trying to solve.

Hi Lassoan,

Thanks for your helps.

The background is:we want to show some regions(original mask value in a json file) like ROIs in Slicer by using Segment Editor (create one SegmentationNode to show all of these regions,each segment  represent one region); 

The goal is: we can edit the edges of regions and save them into another Json file,then we can get radiomics for each modified regions.

Our methods:
1.create vtkImageData for each masks in json file;
2.create a vtkMarchingCubes,and setInputData(vtkImageData),then create Polydata for this vtkMarchingCubes;
3.create one modelNode ,SetAndObservePolyData(Polydata) ;
4. create segmentationNode,and used ImportModelToSegmentationNode(modelNode, segmentationNode)
5.call rep = segmentationNode.GetBinaryLabelmapRepresentation(segmentID) to get specific segment;
6.assigned arr = rep.GetPointData().GetArray(0) to get all of the points of this Segment.

Current problem is: we found the output masks from step6 are different with original data even if we did not modified the corresponding Region,there is a lossy operation.

We have tried: using vtkDiscreteMarchingCubes to replace vtkMarchingCubes.

Just want to know:how to get the correct masks from segment(same with original data)?

Thanks,
Chunbo

@lassoan,since convert vtkMarchingCube/vtkDiscreteMarchingCubes to a binary labelmap is a “lossy operation”,so maybe it is impossible to get the same masks from the converted object,correct?

You can edit segmentations in Segment Editor module with various tools. Is there some operation that you cannot do in the Segment Editor?

Why do you create polydata manually? Segmentation node can provide you closed surface representation as shown here.

You still have the binary labelmap representation in the segmentation. Why do you import the segment?

if you use vtkDiscreteMarchingCubes then it should be possible to convert without losing information, but the result is just a cube for each voxel, so the vtkDiscreteMarchingCubes output is not usable for much.

Hi Lassoan,

Below is some updates.

"You can edit segmentations in Segment Editor module with various tools. Is there some operation that you cannot do in the Segment Editor?"

[Yes,we want to save the modified masks into a json file which can be inputted as a argument when computing Radiomics by a exe file;]

"Why do you create polydata manually? Segmentation node can provide you closed surface representation as shown here."

[This polydata = vtk.vtkMarchingCubes().GetOutput(),will be used to modelNode.SetAndObservePolyData(polydata) ,then loaded into segmentationNode by ImportModelToSegmentationNode(modelNode, segmentationNode).]

"You still have the binary labelmap representation in the segmentation. Why do you import the segment?"

[I am not sure when this binary labelmap representation has been created.In our logic,this binary labelmap representation was got from segmentationNode.GetBinaryLabelmapRepresentation(segmentID) at last step,since we have to get each segment’s mask after all of segments have been added,and save those masks into json file.Pls correct me if I am wrong.]

"if you use vtkDiscreteMarchingCubes then it should be possible to convert without losing information, but the result is just a cube for each voxel, so the vtkDiscreteMarchingCubes output is not usable for much."

[We have changed to this method: create a vtkImageData to save mask values firstly–>then convert it to vtkOrientedImageData–>ImportLabelmapToSegmentationNode(vtkOrientedImageData, segmentationNode)–>segmentationNode.GetBinaryLabelmapRepresentation(eachSegmentID)—>Output masks.
And the result (mask value) is same with original one.]

Thanks,
Chunbo

You can import either labelmaps or models into Segmentation node and you can retrieve labelmap or polydata VTK objects or export to labelmap volume or model node from a segmentation node.

It does not matter what input you originally set in Segmentation node (labelmap or polydata), conversions to any other representation are done automatically. You may tune smoothing and decimation parameters if you want to, but defaults should work well.

If your json file contain labelmap then set a labelmap in your Segmentation node. If your json file contains polydata then set that in your Segmentation node.

What modifications do you perform on the masks? With which module? Segment editor?

1 Like

Yes,Lassoan.We are using Segment Editor to modify the some regions(erase some edges or draw others).Then Saved the changed masks into json file for computing radiomics.

Thanks,
Chunbo

1 Like

OK, then you should be able to do everything using Segmentations and Segment Editor modules. I don’t think you need to create model nodes at all.

Got it.

Thanks for your helps,Dr. Andras Lasso.

Hello there!Did you find a solution for this? I have the same problem.We loaded the masks(uncorrected) for our mri images imgs that our model predicted. And we want just modify some parts of the mask.(adding with draw or delete some parts of the mask without draw all the mask) in order to have a better mask to give it to our model instead of the uncorrected.Thanks

You can do this in the Segment Editor module. If you have any specific problem then post it in a new topic.