[vtkusers] Writing text in an image

David Gobbi david.gobbi at gmail.com
Fri Jul 8 17:10:50 EDT 2011


Hi Alexis,

Take a look at vtkFreeTypeUtilities, it can render text into a
vtkImageData.  You can use this to create a small vtkImageData for
each piece of annotation you need, and then you can use vtkImageBlend
to overlay these onto your image.  To control the positioning of each
text block for the overlay, you will have to set the Extent of each
one so that the extent starts at the position where you want that text
block to appear in the main image.

 - David


On Fri, Jul 8, 2011 at 1: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
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>



More information about the vtkusers mailing list