DICOM: Аннотирование DICOM изображений
В этом разделе
Аннотации DICOM файла хранятся в файле состояния представления DICOM. Класс
DicomAnnotationCodec позволяет загружать и сохранять аннотации в файл состояния представления DICOM.
На данный момент класс
DicomAnnotationCodec поддерживает следующие аннотации:
Вот C#/VB.NET код, который демонстрирует, как загрузить аннотации DICOM файла, изменить аннотации и сохранить измененные аннотации в исходный файл:
/// <summary>
/// Loads DICOM annotations from file,
/// changes the text of text annotations,
/// saves DICOM annotation back to the source file.
/// </summary>
/// <param name="presentationStateFilePath">The path to a presentation state file.</param>
/// <param name="textValue">The value of text annotations.</param>
public static void ChangeTextAnnotations(string presentationStateFilePath, string textValue)
{
// load presentation state file
using (Vintasoft.Imaging.Codecs.ImageFiles.Dicom.DicomFile dicomFile =
new Vintasoft.Imaging.Codecs.ImageFiles.Dicom.DicomFile(presentationStateFilePath, false))
{
// create codec of DICOM annotations
Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationCodec annotationCodec =
new Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationCodec();
// get annotations from the presentation state file
Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationDataCollection[] annotationDataCollections =
annotationCodec.Decode(dicomFile.Annotations);
// for each annotation collection
foreach (Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationDataCollection annotationDataCollection in annotationDataCollections)
{
// for each annotation in annotation collection
foreach (Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationData annotationData in annotationDataCollection)
{
// if annotation is text annotation
if (annotationData is Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData)
{
// get text annotation data
Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData textAnnotationData =
(Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData)annotationData;
// change text of text annotation
textAnnotationData.UnformattedTextValue = textValue;
}
}
}
// save annotations in the source presentation state file
annotationCodec.Encode(dicomFile.Annotations, annotationDataCollections);
// save changes in DICOM file
dicomFile.SaveChanges();
}
}
''' <summary>
''' Loads DICOM annotations from file,
''' changes the text of text annotations,
''' saves DICOM annotation back to the source file.
''' </summary>
''' <param name="presentationStateFilePath">The path to a presentation state file.</param>
''' <param name="textValue">The value of text annotations.</param>
Public Shared Sub ChangeTextAnnotations(presentationStateFilePath As String, textValue As String)
' load presentation state file
Using dicomFile As New Vintasoft.Imaging.Codecs.ImageFiles.Dicom.DicomFile(presentationStateFilePath, False)
' create codec of DICOM annotations
Dim annotationCodec As New Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationCodec()
' get annotations from the presentation state file
Dim annotationDataCollections As Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationDataCollection() = annotationCodec.Decode(dicomFile.Annotations)
' for each annotation collection
For Each annotationDataCollection As Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationDataCollection In annotationDataCollections
' for each annotation in annotation collection
For Each annotationData As Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationData In annotationDataCollection
' if annotation is text annotation
If TypeOf annotationData Is Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData Then
' get text annotation data
Dim textAnnotationData As Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData = DirectCast(annotationData, Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData)
' change text of text annotation
textAnnotationData.UnformattedTextValue = textValue
End If
Next
Next
' save annotations in the source presentation state file
annotationCodec.Encode(dicomFile.Annotations, annotationDataCollections)
' save changes in DICOM file
dicomFile.SaveChanges()
End Using
End Sub