# 2D pixel data to 3D binary masks

**URL:** https://discourse.vtk.org/t/2d-pixel-data-to-3d-binary-masks/13834
**Category:** Support
**Tags:** code
**Created:** [May 3, 2024, 4:17am UTC](https://discourse.vtk.org/t/2d-pixel-data-to-3d-binary-masks/13834 "2024-05-03T04:17:36Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![constifer](https://discourse.vtk.org/user_avatar/discourse.vtk.org/constifer/32/8799_2.png) [@constifer](https://discourse.vtk.org/u/constifer)
#### Post date: [May 3, 2024, 4:17am UTC](https://discourse.vtk.org/t/2d-pixel-data-to-3d-binary-masks/13834/1 "2024-05-03T04:17:36Z")

</div>

Hello everyone, I’ve been trying to convert some json files which contain cardiac contours in pixel coords into binary masks with VTK. The json contours were made using an unknown shareware we no longer have access to, and it’s been hell trying to figure out how to imitate the [DicomRTStruct2Image](https://github.com/benpresles/vv/blob/master/tools/clitkDicomRTStruct2Image.cxx) for this. What I’ve done is to merge all the original files to json nested files separating phases (es/ed) across the entire 3D volume that describe a nested structure of “tissues-\>spatial slices-\>x,y coord list” and then wrote this snippet to grab these for VTK:

> vtkSmartPointer json\_data = vtkSmartPointer::New();  
> vtkSmartPointer append = vtkSmartPointer::New();  
> for (auto& it = data.begin(); it != data.end(); ++it) { //for each target tissue  
> for (auto& pk : it.value()) { //for each spatial slice  
> vtkSmartPointer mMesh = vtkSmartPointer::New();  
> mMesh-\>Allocate();  
> vtkSmartPointer mPoints = vtkSmartPointer::New();  
> mMesh-\>SetPoints(mPoints);  
> vtkIdType ids[2];  
> int idx = 0;  
> for (auto& xypair : pk) { //for each x,y pair  
> mMesh-\>GetPoints()-\>InsertNextPoint(xypair[0], xypair[1], slice);  
> ids[0] = idx++;  
> ids[1] = (ids[0] + 1) % pk.size(); // increment pairs & the n-1 → 0  
> mMesh-\>GetLines()-\>InsertNextCell(2, ids);  
> }  
> slice++; //increjase depth  
> append-\>AddInputData(mMesh);  
> }  
> break; //TODO - extend the loops across tissue types  
> }  
> append-\>Update();  
> json\_data-\>DeepCopy(append-\>GetOutput());

I’ve verified that this does indeed generates the contours in the same fashion clitk does to a working liver example I’ve got. Although when I’m running this through the rest of the pipeline modifying [the filter](https://github.com/benpresles/vv/blob/master/common/clitkDicomRTStruct2ImageFilter.cxx) I’m getting a blank image. Something that i have noticed, is that the transformation matrix always comes back as the identity matrix.

I have a feeling that my woes are coming from either the incrementing integer slice across the Z axis or that my coordinates should be converted before usage but then again, I’m not quite sure. Any pointers would be greatly appreciated.

---

<div class="post-metadata">

### Author: ![constifer](https://discourse.vtk.org/user_avatar/discourse.vtk.org/constifer/32/8799_2.png) [@constifer](https://discourse.vtk.org/u/constifer)
#### Post date: [May 3, 2024, 6:54am UTC](https://discourse.vtk.org/t/2d-pixel-data-to-3d-binary-masks/13834/2 "2024-05-03T06:54:16Z")

</div>

Oh, yeah my assumption was right. The Z axis needs to be inverted (i just started from the end and went to the beginning). i just multiplied the affine matrix from the reference image with each point to get the real world coords.
