Hello,
I created a lasso a 3D polydata, then I used vtkPolyDataToImageStencil to convert it to ImageStencil.
I have a custom volume and I have it’s short data, it has width,height,depth. Now I want to color the voxels in the volume that are marked by the polydata.
I check if the value of current image stencil is white, then I mark the voxel as white but it doesn’t work.
I see the result as a cube!
and that’s the result after applying lasso
FULL Pipeline:
finalPolyData->SetPoints(closedSurfacePoints.GetPointer());
finalPolyData->SetStrips(closedSurfaceStrips.GetPointer());
finalPolyData->SetPolys(closedSurfacePolys.GetPointer());
//ActorFinalPolyData->VisibilityOn();
vtkNew<vtkImageData> whiteImage;
double bounds[6];
finalPolyData->GetBounds(bounds);
double spacing[3]; // desired volume spacing
spacing[0] = 0.5;
spacing[1] = 0.5;
spacing[2] = 0.5;
whiteImage->SetSpacing(spacing);
// compute dimensions
int dim[3];
for (int i = 0; i < 3; i++)
{
dim[i] = static_cast<int>(
ceil((bounds[i * 2 + 1] - bounds[i * 2]) / spacing[i]));
}
whiteImage->SetDimensions(dim);
whiteImage->SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2] - 1);
double origin[3];
origin[0] = bounds[0] + spacing[0] / 2;
origin[1] = bounds[2] + spacing[1] / 2;
origin[2] = bounds[4] + spacing[2] / 2;
whiteImage->SetOrigin(origin);
whiteImage->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
// fill the image with foreground voxels:
unsigned char inval = 255;
unsigned char outval = 0;
vtkIdType count = whiteImage->GetNumberOfPoints();
for (vtkIdType i = 0; i < count; ++i)
{
whiteImage->GetPointData()->GetScalars()->SetTuple1(i, inval);
}
// polygonal data --> image stencil:
vtkNew<vtkPolyDataToImageStencil> pol2stenc;
pol2stenc->SetInputData(finalPolyData);
pol2stenc->SetOutputOrigin(origin);
pol2stenc->SetOutputSpacing(spacing);
pol2stenc->SetOutputWholeExtent(whiteImage->GetExtent());
pol2stenc->Update();
// cut the corresponding white image and set the background:
imgstenc->SetInputData(whiteImage);
imgstenc->SetStencilConnection(pol2stenc->GetOutputPort());
imgstenc->ReverseStencilOff();
imgstenc->SetBackgroundValue(outval);
imgstenc->Update();
vtkNew<vtkMetaImageWriter> imageWriter;
imageWriter->SetFileName("labelImage.mhd");
imageWriter->SetInputConnection(imgstenc->GetOutputPort());
imageWriter->Write();
DVertex origin_ = mVolume->GetOrigin();
double maSpacing[3]; /// Image spacing
maSpacing[0] = mVolume->GetXSpacing();
maSpacing[1] = mVolume->GetYSpacing();
maSpacing[2] = mVolume->GetZSpacing();
auto mask = std::make_shared<DNA_Mask>(140, mVolume->GetWidth(), mVolume->GetHeight(), mVolume->GetDepth());
mask->SetSpacing(maSpacing[0], maSpacing[1], maSpacing[2]);
mask->SetOrigin(origin_);
vtkImageData* data = imgstenc->GetOutput();
int dims[3];
data->GetDimensions(dims);
//#pragma omp parallel for
short* volumeData = mVolume->GetData().get();
for (int z = 0; z < dims[2]; z++)
{
for (int y = 0; y < dims[1]; y++)
{
for (int x = 0; x < dims[0]; x++)
{
unsigned char* pPixel = static_cast<unsigned char*>(data->GetScalarPointer(x, y, z));
if (*pPixel == 255)
{
int image_linear_index = z * dims[0] * dims[1] + y * dims[0] + x;
volumeData[image_linear_index] = 255;
}
}
}
}