# Display Dicom data just received by websocket connection

**URL:** <https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293>\
**Category:** Development\
**Tags:** dicom\
**Created:** [June 24, 2019, 3:30pm UTC](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293 "2019-06-24T15:30:41Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![rbahegne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbahegne/32/4837_2.png) [@rbahegne](https://discourse.slicer.org/u/rbahegne)\
**Post date:** [July 1, 2019, 1:23pm UTC](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293/21 "2019-07-01T13:23:34Z")

</div>

`node->CreateDefaultDisplayNodes()` does return “vtkMRMLScalarVolumeNode::CreateDefaultDisplayNodes failed: scene is invalid” as well if I don’t do a set scene.

But the maybe it’s connected to my issue about loading dicom. With a regular 4.10.2 slicer i’m not able to load a dicom series. It result in one black slice. Sample nrrd are working.

---

<div class="post-metadata">

**Author:** ![rbahegne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbahegne/32/4837_2.png) [@rbahegne](https://discourse.slicer.org/u/rbahegne)\
**Post date:** [July 1, 2019, 3:03pm UTC](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293/22 "2019-07-01T15:03:44Z")

</div>

I can load a Dicom serie using the add Data function. But i can’t load it using the DCM database , i see the series in the database, i can check metadata, but i cant examine them (button does nothing) and i can’t load them either.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 1, 2019, 6:30pm UTC](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293/23 "2019-07-01T18:30:08Z")

</div>

Are there errors in the log?

---

<div class="post-metadata">

**Author:** ![rbahegne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbahegne/32/4837_2.png) [@rbahegne](https://discourse.slicer.org/u/rbahegne)\
**Post date:** [July 2, 2019, 8:20am UTC](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293/24 "2019-07-02T08:20:26Z")

</div>

No, here is a log when i add a Dicom to DB and tried to examine it and load it :

> Session start time …: 2019-07-02 10:14:13
> 
> Slicer version …: 4.11.0-2019-05-21 (revision a3fce80) linux-amd64 - not installed release
> 
> Operating system …: Linux / 4.15.0-54-generic / #58-Ubuntu SMP Mon Jun 24 10:55:24 UTC 2019 - 64-bit
> 
> Memory …: 7772 MB physical, 976 MB virtual
> 
> CPU …: GenuineIntel Intel(R) Core™ i5-8500 CPU @ 3.00GHz, 6 cores, 6 logical processors
> 
> VTK configuration …: OpenGL2 rendering, Sequential threading
> 
> Developer mode enabled …: yes
> 
> Prefer executable CLI …: yes
> 
> Additional module paths …: /home/raphaelbahegne/Dev/Repo/SiStream-Debug-Build/lib/Slicer-4.11/qt-scripted-modules, /home/raphaelbahegne/Dev/Repo/SiStream-Debug-Build/lib/Slicer-4.11/qt-loadable-modules, /home/raphaelbahegne/Dev/Repo/SiStream-Debug-Build/lib/Slicer-4.11/cli-modules
> 
> Scripted subject hierarchy plugin registered: Annotations
> 
> Scripted subject hierarchy plugin registered: SegmentEditor
> 
> Scripted subject hierarchy plugin registered: SegmentStatistics
> 
> Switch to module: “Welcome”
> 
> Switch to module: “DICOM”
> 
> TagCacheDatabase adding table
> 
> New patient inserted: 1
> 
> New patient inserted as : 1
> 
> Need to insert new study: “1.3.12.2.1107.5.2.18.42563.30000019012809010957000000004”
> 
> Study Added
> 
> Need to insert new series: “1.3.12.2.1107.5.2.18.42563.2019012916550569633133260.0.0.0”
> 
> Series Added
> 
> “DICOM indexer has successfully processed 30 files [0.46s]”
> 
> Imported a DICOM directory, checking for extensions
> 
> Imported a DICOM directory, checking for extensions

---

<div class="post-metadata">

**Author:** ![rbahegne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbahegne/32/4837_2.png) [@rbahegne](https://discourse.slicer.org/u/rbahegne)\
**Post date:** [July 2, 2019, 8:54am UTC](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293/25 "2019-07-02T08:54:51Z")

</div>

A “restore default” in application setting solved my problem. I don’t know what caused thoses troubles.

I’m back to creating nodes.

---

<div class="post-metadata">

**Author:** ![rbahegne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbahegne/32/4837_2.png) [@rbahegne](https://discourse.slicer.org/u/rbahegne)\
**Post date:** [July 4, 2019, 8:16am UTC](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293/26 "2019-07-04T08:16:15Z")

</div>

Ok, i fixed my problem here part of my code if someone want to do same 🙂

```
long int val_long, width, height;
for (int sliceNum=0; sliceNum<dcmImageList.count(); sliceNum++)
{
    DcmDataset *dataSet = dcmImageList[sliceNum].getDataset();
    if( dataSet != nullptr )
    {
        vtkNew<vtkMRMLScalarVolumeNode> dicomNode;
        dataSet->findAndGetLongInt(DCM_Columns,val_long);
        width = val_long;
        dataSet->findAndGetLongInt(DCM_Rows,val_long);
        height = val_long;

        if (sliceNum==0) {
            imageData->SetDimensions(width, height, dcmImageList.count());
            imageData->AllocateScalars(VTK_UNSIGNED_SHORT,1);
        }

        const unsigned short *dicom_buf;
        dataSet->findAndGetUint16Array(DCM_PixelData, dicom_buf);

        E_TransferSyntax xfer = dataSet->getOriginalXfer();
        dataSet->chooseRepresentation(xfer, nullptr);
        DicomImage *di = new DicomImage(dataSet, xfer, 0);

        memcpy(imageData->GetScalarPointer(0,0,sliceNum), di->getOutputData(16), di->getOutputDataSize());
        imageData->Modified();
    }
    else
    {
        std::cout << "-------------------empty image--------------------------------" << std::endl;
    }
}
imageData->SetOrigin(0,0,0);

this->GetMRMLScene()->AddNode(dicomNode);
//this->GetMRMLScene()->AddNewNodeByClass("vtkMRMLScalarVolumeNode", "dicomNode");
dicomNode->SetAndObserveImageData(imageData);
dicomNode->SetOrigin(197,192,-83);
dicomNode->SetSpacing(1.25,1.25,3.00);
dicomNode->SetIToRASDirection(-1,0,0);
dicomNode->SetJToRASDirection(0,-1,0);
dicomNode->SetKToRASDirection(0,0,1);
dicomNode->CreateDefaultDisplayNodes();
dicomNode->CreateDefaultStorageNode();
dicomNode->UpdateScene(this->GetMRMLScene()); 

```

(Some dicom infos are hardcoded at the moment)

Few observations :

- if i use AddNewNodeByClass instead of AddNode it throw “scene invalid” and it does not work
- CreateDisplayDefaultNodes throw a warning :

> Warning: In /home/raphaelbahegne/Dev/Slicer/Libs/MRML/Core/vtkMRMLSubjectHierarchyNode.cxx, line 718  
> vtkSubjectHierarchyItem (0x562fd3c5c650): FindChildByID: Item cache does not contain requested ID 140486651064064  
> HasItemAttribute: Failed to find subject hierarchy item by ID 140486651064064

But the image is displayed properly.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 4, 2019, 1:05pm UTC](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293/27 "2019-07-04T13:05:58Z")

</div>

> [@rbahegne](#):
>
> if i use AddNewNodeByClass instead of AddNode it throw “scene invalid” and it does not work

`this->GetMRMLScene()->AddNewNodeByClass("vtkMRMLScalarVolumeNode", "dicomNode")` creates a new node by the name “dicomnode”. The new node has nothing to do with the other node that you created previously. That previously created node is not added to the scene, that’s why you got the error that the scene was invalid.

You can use to use `scene->AddNewNodeByClass(...)` instead of `vtkNew<...> node; scene->AddNode(node)`. AddNewNodeByClass is simpler (just one method calls instead of two) and it creates the new node with default properties stored in the scene (for volume nodes there is not much to customize but for display or storage nodes the user may have set some non-default values).

> [@rbahegne](#):
>
> CreateDisplayDefaultNodes throw a warning

Most likely this warning is a false alarm. Do you use latest master version of Slicer?

---

<div class="post-metadata">

**Author:** ![rbahegne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbahegne/32/4837_2.png) [@rbahegne](https://discourse.slicer.org/u/rbahegne)\
**Post date:** [July 4, 2019, 1:27pm UTC](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293/28 "2019-07-04T13:27:46Z")

</div>

It got the src and built slicer just 2 month ago, so it’s 4.11.0

Thanks a lot it becomes clearer, and clearer.

---

<div class="post-metadata">

**Author:** ![rbahegne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbahegne/32/4837_2.png) [@rbahegne](https://discourse.slicer.org/u/rbahegne)\
**Post date:** [July 5, 2019, 8:28am UTC](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293/29 "2019-07-05T08:28:52Z")

</div>

Ok i used `AddNewNodeByClass` but it gives me a vtkMrmlNode not a Scalar volume one.

So it did a cast :

> vtkMRMLScalarVolumeNode _dicomNode = (vtkMRMLScalarVolumeNode_)scene-\>AddNewNodeByClass(“vtkMRMLScalarVolumeNode”, “dicomNode”);

But it seem a little bit brutal, is there a cleaner way ?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 5, 2019, 12:23pm UTC](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293/30 "2019-07-05T12:23:11Z")

</div>

You can use vtkMRMLScalarVolumeNode::SafeDownCast to retrieve a more specific pointer.

[Previous page](https://discourse.slicer.org/t/display-dicom-data-just-received-by-websocket-connection/7293.md?page=1)
