How to color or fill the selected contour?

I am rendering a volume; And displaying its coronal plane,transverse plane,sigittal plane. Now i outlined a contour via the VTK tools, the picture like this picture 1, the code is as flows:
m_pContourWidget = vtkSmartPointer< vtkContourWidget >::New();
m_pContourWidget->SetInteractor(m_AxialInteractor);

contourRepresentation =vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
m_pContourWidget->SetPriority(0.9);
contourRepresentation->GetLinesProperty()->SetColor(0, 1, 0);
m_pContourWidget->SetRepresentation(contourRepresentation);
m_pContourWidget->ManagesCursorOn();
m_pContourWidget->On();

But Now I want to color or fill the contour by vtkPolyDataToImageStencil,vtkImageStencilToImage,vtkImageStencil and so on. make it like picture 2.

(picture 1)

(picture 2)

I have tried using the following code but failed:
vtkSmartPointer tempImageData = vtkSmartPointer::New();

int dim[3];
m_pReader->GetOutput()->GetDimensions(dim);
double o[3];
m_pReader->GetOutput()->GetOrigin(o);
double spacing[3];
m_pReader->GetOutput()->GetSpacing(spacing);
tempImageData->SetSpacing(spacing);
tempImageData->SetDimensions(dim);
tempImageData->SetOrigin(0, 0, 0);
tempImageData->AllocateScalars(VTK_UNSIGNED_CHAR, 1);

int slice = myInteractorStyle1->GetSlice();
tempImageData->SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, slice);

// fill the image with foreground voxels:
unsigned char inval = 255;
unsigned char outval = 0;
vtkIdType count = tempImageData->GetNumberOfPoints();
for (vtkIdType i = 0; i < count; ++i)
{
	tempImageData->GetPointData()->GetScalars()->SetTuple1(i, inval);
}
polyDataToImageStencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
polyDataToImageStencil->SetTolerance(0);

polyDataToImageStencil->SetInputData(m_pContourWidget->GetContourRepresentation()->GetContourRepresentationAsPolyData());

slice = myInteractorStyle1->GetSlice();
polyDataToImageStencil->SetOutputOrigin(tempImageData->GetOrigin());
polyDataToImageStencil->SetOutputSpacing(tempImageData->GetSpacing());
polyDataToImageStencil->SetOutputWholeExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, slice);
polyDataToImageStencil->Update();

imageStencilToImage = vtkSmartPointer<vtkImageStencilToImage>::New();
imageStencilToImage->SetInputConnection(polyDataToImageStencil->GetOutputPort());
imageStencilToImage->SetInsideValue(1);
imageStencilToImage->Update();

 
//view the imageStencilToImage
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(imageStencilToImage->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> render = vtkSmartPointer<vtkRenderer>::New();
render->AddActor(actor);
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(render);
renWin->SetSize(400, 400);
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
iren->SetInteractorStyle(style);
iren->Initialize();
iren->Start();

You can indeed use vtkPolyDataToImageStencil, vtkImageStencilToImage, and vtkImageStencil to fill in a labelmap volume using a closed contour. The code above generally looks all right, probably there is just a few bugs that you need to hunt down. If you just want to visualize a filled contour then you can add a polydata with a polygon cell. You can probably modify the contour widget representation to show this filled polygon (but maybe there are already such representations available).

However, I would recommend to not reimplement a segmentation tool (and viewer, and DICOM importer/exporter, etc.) from scratch. By customizing/extending existing free, open-source, restriction-free medical image computing frameworks, such as 3D Slicer or MITK, you could provide much better software to your users, much faster.

@wulicheng it’s also important to note that the head appears to be distorted in your screenshot. This doesn’t matter much if you are just experimenting, but could be dangerous if your goal is to make real medical software.

I am just leaning VTK and experiment ,but I have not make it.for taking me two days.