Question about vtkImageStencil

Hi, do the stencil and the input need to be the same size while using vtkImageStencil?

vtkSmartPointer<vtkImageData> RemoveOsteotomyFromImage(vtkImageData* 
imageWithOsteotomy, vtkImageData* imageToRemoveOsteotomy)
{
vtkSmartPointer<vtkImageData> osteotomyRemovedImage;
if (imageWithOsteotomy && imageToRemoveOsteotomy) {
	double ranges[2];
	imageToRemoveOsteotomy->GetScalarRange(ranges);

	// Generate the stencil according to the removed osteotomy in stencil
	// The removed part is colored as ranges[0] using ImageUtilities::GenerateStencilImage before
	vtkSmartPointer<vtkImageToImageStencil> removedOsteotomy = vtkSmartPointer<vtkImageToImageStencil>::New();
	removedOsteotomy->SetInputData(imageWithOsteotomy);
	removedOsteotomy->ThresholdByLower(ranges[0]);
	removedOsteotomy->Update();

	// Make the intersection of the stencil and the image data color the same as the image background 
	vtkSmartPointer<vtkImageStencil> imageStencil = vtkSmartPointer<vtkImageStencil>::New();
	imageStencil->SetStencilData(removedOsteotomy->GetOutput());
	imageStencil->SetInputData(imageToRemoveOsteotomy);
	imageStencil->ReverseStencilOn();
	imageStencil->SetBackgroundValue(ranges[0]);
	imageStencil->Update();
	osteotomyRemovedImage = imageStencil->GetOutput();
}

return osteotomyRemovedImage;

}

Here is the code, basically, imageWithOsteotomy is a part of imageToRemoveOsteotomy (Cropped using vtkExtractVOI). Then there is some changes made to imageWithOsteotomy. And Now I’m applying the same change to imageToRemoveOsteotomy.
The result is not very consistent using this way. The borders somehow have some unwanting changes

The input and the stencil can be different sizes. However, the input and the stencil should have the same Origin and the same Spacing. The difficulty is that vtkExtractVOI modifies the Origin, so it should not be used with vtkImageStencil. Instead of vtkExtractVOI, you can use vtkImageClip, which does not modify the Origin.

Thank you! Let me try that