[vtkusers] Writing text in an image

David Doria daviddoria at gmail.com
Fri Jul 8 15:19:28 EDT 2011


On Fri, Jul 8, 2011 at 3:10 PM, Alexis Cheng <acheng_1221 at hotmail.com> wrote:
> 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
>

I think you're supposed to use an ImageStencil. I haven't looked at
this in a long long time, but if you could fix it that'd be great!

http://www.itk.org/Wiki/VTK/Examples/Cxx/Broken/Images/ImageText

David



More information about the vtkusers mailing list