是否可以在不包含VTK的情况下将文本呈现给ITK图像?我想要做的是将一些标签打印到图像中,而不必添加这个巨大的VTK库。
我找到的唯一解决方案是使用Freetype和VTK来做到这一点。
//Properties of the Text
vtkSmartPointer<vtkTextProperty> textPropertyRegion = vtkSmartPointer<vtkTextProperty>::New();
textPropertyRegion->SetColor(1.0, 1.0, 1.0);
textPropertyRegion->SetFontSize(36);
textPropertyRegion->SetJustificationToCentered();
textPropertyRegion->SetBold(1);
//Renders the Text
vtkSmartPointer<vtkFreeTypeUtilities> freeType = vtkSmartPointer<vtkFreeTypeUtilities>::New();
//The text will be temporarily rendered into this image
vtkSmartPointer<vtkImageData> textImage = vtkSmartPointer<vtkImageData>::New();
freeType->RenderString(textPropertyRegion, vtkStdString("TEXT"), textImage );
int* featureImageDims = featureImage->GetDimensions();
// Set every pixel of the rendered Text Image into the main Image
for (int y = 0; y < featureImageDims[1]; y++) {
for (int x = 0; x < featureImageDims[0]; x++) {
unsigned char *valueText = static_cast<unsigned char*>(featureImage->GetScalarPointer(x, featureImageDims[1] - 1 - y, 0));
itk::Image<short,2>::IndexType idx;
idx.SetElement(0, x + 1200);
idx.SetElement(1, y + 1000);
RGBPixelType pix;
pix.fill(valueText[0]);
it.SetIndex(idx);
it.Set(pix);
}
}
这很有效,但是:重构很糟糕,我需要包含2个额外的库来完成渲染字符串并将其放入图像的任务。有更简单的解决方案吗?