[vtkusers] Writing text in an image

Alexis Cheng acheng_1221 at hotmail.com
Fri Jul 8 15:10:21 EDT 2011


Hi All,

I am looking for a method to write text into a vtkImage or make a vtkImage with text in it. I have tried a couple of methods with no success. Below is what I have tried.

//////////////////
//2D polydata -> 3D polyData -> image data
        vtkSmartPointer<vtkVectorText> textSource = vtkSmartPointer<vtkVectorText>::New();
        textSource->SetText("Hello");
        textSource->Update();

        vtkSmartPointer<vtkLinearExtrusionFilter> linExtFilter = vtkSmartPointer<vtkLinearExtrusionFilter>::New();
        linExtFilter->SetCapping(1);
        linExtFilter->SetExtrusionTypeToNormalExtrusion();
        linExtFilter->SetScaleFactor(0.1);
        linExtFilter->SetInput(textSource->GetOutput());
        linExtFilter->Update();

        vtkSmartPointer<vtkPolyDataToImageStencil> polyToImageStencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
        polyToImageStencil->SetInput(linExtFilter->GetOutput());
        polyToImageStencil->SetOutputWholeExtent(MTImage->GetExtent());
        polyToImageStencil->Update();

        vtkSmartPointer<vtkImageStencil> imgStencil = vtkSmartPointer<vtkImageStencil>::New();
        imgStencil->SetInput(BlackImage);
        imgStencil->SetStencil(polyToImageStencil->GetOutput());
        imgStencil->ReverseStencilOff();
        imgStencil->SetBackgroundValue(255);
        imgStencil->Update();
        
        vtkSmartPointer<vtkPNGWriter> textWriter = vtkSmartPointer<vtkPNGWriter>::New();
        textWriter->SetFileName("textImage.png");
        textWriter->SetInput(imgStencil->GetOutput());
        textWriter->Write();
//////////////////////
//2D polydata -> image
        vtkSmartPointer<vtkVectorText> textSource = vtkSmartPointer<vtkVectorText>::New();

        textSource->SetText("Hello");

        textSource->Update();

        vtkSmartPointer<vtkProbeFilter> textFilter = vtkSmartPointer<vtkProbeFilter>::New();
        textFilter->SetSource(textImage);
        textFilter->SetInputConnection(textSource->GetOutputPort());
        textFilter->Update();

        vtkSmartPointer<vtkImageCast> textCast = vtkSmartPointer<vtkImageCast>::New();
        textCast->SetOutputScalarTypeToUnsignedChar();
        textCast->SetInput(textFilter->GetOutput());
        textCast->Update();
//////////////////////

Am I using these classes and filters correctly? Are there any methods that facilitate annotating images with text? Thank you in advance.

Regards,
Alexis Cheng
Electrical Engineering
University of British Columbia
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110708/dd940e29/attachment.htm>


More information about the vtkusers mailing list