[vtkusers] Problem using vtkDicomWriter

Andras Lasso lasso at queensu.ca
Wed Jan 25 08:56:28 EST 2017

If you drag-and-drop a single image slice into Slicer then by default it will load that single image slice. How to load the full volume from DICOM:

Option A: Drag-and-drop the directory that contains your DICOM files to the Slicer main window. Slicer offers to import them, accept that option, wait for the import to complete, then select the data set in the DICOM browser and load it.

Option B: Drag-and-drop a single DICOM file to Slicer. You have the option to load that single slice (default behavior), or check “Show Options” in the top-right corner and uncheck “Single File” option in the table.


From: vtkusers [mailto:vtkusers-bounces at vtk.org] On Behalf Of David Gobbi
Sent: January 25, 2017 8:36
To: Eddy Cappeau <edcpwk at gmail.com>
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] Problem using vtkDicomWriter

Hi Eddy,

The code is using a vtkDICOMCTGenerator to write an MR image, which is definitely wrong.  Use the vtkDICOMMRGenerator, and make sure the meta data matches the IOD: http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.8.3.html

The standard says that BitsAllocated must be 16 for MR images, therefore an 8-bit MR image is non-standard and DICOM software may to refuse to display it.  You should convert your data to 16-bit (In fact, it seems fishy that you are working with unsigned char, since if you are writing a reformat of an MR image, then wasn't the original MR image a 16-bit image?)

I can't answer about why Slicer is only showing one slice, as I don't currently have Slicer installed.

 - David

On Wed, Jan 25, 2017 at 1:57 AM, Eddy Cappeau <edcpwk at gmail.com<mailto:edcpwk at gmail.com>> wrote:
Hi David,

Here's the code below. This is almost the example of the api doc :

    auto generator = vtkSmartPointer <vtkDICOMCTGenerator>::New();
    vtkSmartPointer <vtkDICOMMetaData> meta = vtkSmartPointer <vtkDICOMMetaData>::New();

    meta->SetAttributeValue(DC::PatientName, "Test");
    meta->SetAttributeValue(DC::PatientID, "0000001");
    meta->SetAttributeValue(DC::ScanOptions, "");
    meta->SetAttributeValue(DC::ScanningSequence, "GR");
    meta->SetAttributeValue(DC::SequenceVariant, "SP");
    meta->SetAttributeValue(DC::ScanOptions, "");
    meta->SetAttributeValue(DC::MRAcquisitionType, "2D");

    vtkSmartPointer <vtkDICOMWriter> dicom_writer =
        vtkSmartPointer <vtkDICOMWriter>::New();
    dicom_writer->SetSeriesDescription("Sagittal Multi-planar Reformat");

    // Set the output filename format as a printf-style string.
    // Set the directory to write the files into.
    // Write the file.
Sorry, the use of an unsigned short type was a carreless mistake. The result is better with unsigned char,
but I still see just one image of the serie on 3D Slicer. Is there something missing to have a complete serie ?
I didn't know the vtkImageImport and I'll give it a try.


2017-01-24 15:41 GMT+01:00 David Gobbi <david.gobbi at gmail.com<mailto:david.gobbi at gmail.com>>:
Hi Eddy,

Can you provide the code that you used to write the image with vtkDICOMWriter?  Here is an example:

Creating an image with a loop that calls GetScalarPointer() for every pixel is not very efficient, and casting to a "char *" when the data is "unsigned short" is wrong: you are setting only 8 bits of each 16-bit pixel, while leaving the other 8 bits uninitialized.  The vtkImageImport filter is a better way of generating image data:

 - David

On Tue, Jan 24, 2017 at 2:51 AM, Eddy Cappeau <edcpwk at gmail.com<mailto:edcpwk at gmail.com>> wrote:
I'd like to use the vtkDICOMWriter class to convert a vtkImage to a set of DICOM images.

I've compile VTK with the vtkDICOM module enabled and followed the example from the pdf found on the github repository (http://dgobbi.github.io/vtk-dicom/doc/vtk-dicom.pdf).

The image data is created like this :

    auto  img = vtkSmartPointer < vtkImageData >::New();
    img->SetOrigin(0, 0, 0);
    img->SetDimensions(1024, 1024, numlayer);
    img->SetSpacing(1, 1, 1);
    img->AllocateScalars(VTK_UNSIGNED_SHORT, 1);
and the data filled like this :

           for (int n = 0; x < numlayer; n++) {
                for (int x = 0; x < 1024; x++) {
                    for (int y = 0; y < 1024; y++) {
                        char* pixel = static_cast<char*>(img->GetScalarPointer(x, y, n));
                        pixel[0] = values[x][y];

I can load the converted result with paraview without problems.
But with 3D Slicer, I can just load the first file of the serie and it display a blank image.
What do I have to do to be able to load the images with both application ?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170125/479abd81/attachment.html>

More information about the vtkusers mailing list