dcmjs-org / dcmjs

Javascript implementation of DICOM manipulation
https://dcmjs.netlify.com/
MIT License
299 stars 112 forks source link

Convert JSON TO dicom object #65

Closed MohsinAther closed 4 years ago

MohsinAther commented 5 years ago

Hi, i want to convert JSON to dicom object. i read #64 but input is a dicom file in that thread.

my JSON would be like "00080018" : { "vr" : "UI", "Value" : [ "1.3.12.2.1107.5.4.3.321890.19960124.162922.29" ] }, "00080020" : { "vr" : "DA", "Value" : [ "19511013" ] }......

pieper commented 5 years ago

It's basically the same. You can populate the javascript object from scratch rather than using one loaded from a file. You can follow the basic style shown in #64 (use tag names and the hex and vr are pulled from the dictionary).

MohsinAther commented 5 years ago

I loaded my js file and edited the tag. but im unable to save it correctly. i also used "WriteBufferStream" but not working

Error "Cannot read property 'isLittleEndian' of undefined". whereas write function take 3 args static write(jsonObjects, useStream, syntax)

i'm doing something wrong ?

const dcmjs = require("dcmjs"); const fs = require("fs");

const filePath = require("./data.json");

var dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(filePath);

dataset.PatientName = "DemoPatient"

dataset = dcmjs.data.DicomMetaDictionary.denaturalizeDataset(dataset);

let new_file_WriterBuffer = dcmjs.data.DicomMessage.write(dataset)

fs.writeFileSync("./seriesImage1.dcm", new_file_WriterBuffer);

pieper commented 5 years ago

Hard to say at a glance, but the best bet is probably just to print out each variable as you go and confirm it has the value you expect. Or start with the other working example and make incremental changes and confirm each one. This week is kind of busy for me, but probably next week we should make a some examples (tests) that cover this use case.

MohsinAther commented 5 years ago

i have added a debugger and watched each and every value . let DicomDict = dcmjs.data.DicomMessage.readFile(arrayBuffer);

this line is converting, dicom file from buffer to object => output is a object which has metadata and dict objects and same object is calling write function

DicomDict.write();

i have made the same js object in "DicomDict" variable as given in example and tried (without let DicomDict = dcmjs.data.DicomMessage.readFile(arrayBuffer); )

and then called DicomDict.write();

Error => DicomDict.write() is not a function.

it would be really appreciated if you can help me in this in comments. thanks

pieper commented 5 years ago

Hi - maybe you already figured this out, but if not this code might help.

Basically this WIP code converts between part10 and json files. It doesn't handle PixelData or other data that should be base64 encoded but it works for other data.

https://github.com/dcmjs-org/dcmjs/blob/add-commander/bin/dcmjs-cli.js#L32-L43

MohsinAther commented 5 years ago

but i need pixeldata aswell

pieper commented 5 years ago

Yes, I plan to add that to the dump/undump with base64 encode/decode according to the standard (or maybe as a separate file so the json is easier to manipulate).

In any case you should be able to just set PixelData to be the right kind of TypedArray.

I hope this is enough to get you going. If you get something working please post it here for future reference. Or if it's not working let me know and I can try to flesh out some more examples (obviously this project needs more documentation anyway 😄 )

MohsinAther commented 5 years ago

i have written a new function and passing json to it. (i hardcoded some meta just for testing purpose, and added pixel data to 7fe00010 tag. its writing the dcm file. meta is correct but viewers i.e RadiAnt could not open it. and i tried to view in dicom tree viewer which is showing its tree perfectly. but i dont know, im missing something.

function toArrayBuffer(buf) {
        var ab = new ArrayBuffer(buf.length);
        var view = new Uint8Array(ab);
        for (var i = 0; i < buf.length; ++i) {
            view[i] = buf[i];
        }
        return ab;
    }
    var meta = { '7fe00010': { vr:'OB', Value:[]}}

    var buff = Buffer.from(buffer['7fe00010'].InlineBinary)

    meta['7fe00010'].Value.push(toArrayBuffer(buff));

var meta={}

    meta['00020002'] = {vr: 'UI', Value: [buffer['00080016'].Value[0]]};
    meta['00020003'] = {vr: 'UI', Value: [buffer['00080018'].Value[0]]};
     meta['00020010'] = {vr: 'UI', Value: ["1.2.840.10008.1.2"]};
    meta['00020012'] = {vr: 'UI', Value: ["2.25.661331934852393026330823390029087831751"]};
    meta['00020013'] = {vr: 'SH', Value: ["DICOMzero-0.0"]};

    delete buffer['7fe00010']

    var dicomDict = new DicomDict(meta);

    dicomDict.dict = buffer;

    return dicomDict;
pieper commented 5 years ago

Here's some code to generate an image instance from scratch. The resulting file can be loaded in 3D Slicer, so it should be a good starting point for you:

https://github.com/dcmjs-org/dcmjs/blob/add-commander/examples/nodejs/generate.js

lam0620 commented 5 years ago

Any body know how to convert DICOM dataset to json ?

"00080018" : { "vr" : "UI", "Value" : [ "1.3.12.2.1107.5.4.3.321890.19960124.162922.29" ] }, "00080020" : { "vr" : "DA", "Value" : [ "19511013" ] }......

pieper commented 5 years ago

The example linked above does this, but using a dataset with names (a namified version of the dicom json model).

You can see a round trip from part10 to json model back to edited part10 here:

https://github.com/dcmjs-org/dcmjs/blob/add-commander/examples/nodejs/readwrite.js

lam0620 commented 5 years ago

Thanks @pieper

Does dcmjs support convert dicom dataset to dicom json model ? Same: http://dicom.nema.org/dicom/2013/output/chtml/part18/sect_F.4.html

pieper commented 5 years ago

That should be DicomDict.dict in the example (I can't doublecheck right now - I'm away from my regular desk until next week).

https://github.com/dcmjs-org/dcmjs/blob/add-commander/examples/nodejs/readwrite.js#L9

What's your overall goal? You can also look at the dicomweb-server code which works with that form.

lam0620 commented 5 years ago

Thank you @pieper . Let me check.

My overall goal is I'd like to convert a dicom dataset responsed from DIMSE (use dimse-dicom package) to dicom json model for dicomweb client as ohif viewer react version.

pieper commented 5 years ago

Sounds good @lam0620, let us know how it goes.

For reference, we are also looking at the option of building a DIMSE gateway on the server side as described in this issue. We are leaning toward the dcmtk-node option since dimse-dicom isn't actively developed at the moment.

lam0620 commented 5 years ago

Hi @pieper

  1. I fixed my issues about converting dicom dataset to dicom json model by creating new code based on DicomMetaDictionary.denaturalizeDataset()

  2. DIMSE gateway I can not build use dimse-dicom yet, so I use OHIF meteor version to make a DIMSE gateway

    • Remove unuse pakages, files from OHIF
    • Add new restful APIs (QIDO, WADO-RS) => not sure right!!!
    • Test work with OHIF react version <--> DIMSE gateway <--> CC PACS model DICOM can be displayed correctly!
  3. Other I will try research https://github.com/jmhmd/dcmtk-node!

https://github.com/sync-for-science/dcmrs-broker A DIMSE getway use dcm4che lib. Currently only the retrieval of DICOM Part 10 objects is supported. The retrieval of bulk data and meta data are not supported.

pieper commented 4 years ago

Sounds like the original issue has been resolved. @lam0620, let us know if you have follow up information that might be useful for others in the future.

ahmetaltay33 commented 3 years ago

My javascript solution for converting dicom Json to dicom Dataset.

<script src="https://unpkg.com/dcmjs"></script>
<script src="https://unpkg.com/dicomweb-client"></script>

<script>
    const dwc = DICOMwebClient.api.DICOMwebClient;
    const options = {
        url: "https://server.dcmjs.org/dcm4chee-arc/aets/DCM4CHEE/rs"
    };
    const dicomweb = new dwc(options);

    const getStudies = async () => {
        const studies = await dicomweb.searchForStudies(
            {
                queryParams:
                {
                    limit: 10,
                    offset: 0,
                    fuzzymatching: true,
                    StudyDate: "20090101-20211231",
                    ModalitiesInStudy: "CT",
                    includefield: "00081030,00080060"
                }
            });
        return studies;
    }

    const convertToDataset = (dcmJson) => {
        return dcmjs.data.DicomMetaDictionary.naturalizeDataset(dcmJson);
    }

    const getStudiesAsDataset = async () => {
        const studies = await getStudies();
        const result = new Array();
        studies.forEach(study => {
            const dataset = convertToDataset(study); 
            result.push(dataset);
        });
        return result;
    }

    getStudiesAsDataset().then(studiesDs => {
        studiesDs.forEach(study => {
            console.log('Study Instance UID: ' + study.StudyInstanceUID); 
            console.log('Accession Number: ' + study.AccessionNumber); 
            console.log('Patient ID: ' + study.PatientID);             
            console.log('Patient Name: ' + study.PatientName); 
        });
    });
</script>