2D pixel data to 3D binary masks

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 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 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.

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.