DICOM to PNG error

Never mind - I found a sample dcm file from your other post

Demo...

Flow...

[{"id":"ad5fee391ca75efb","type":"function","z":"61dce39602934bc2","name":"dcmjs-imaging","func":"const buf = msg.payload;\n\nconst { DicomImage, WindowLevel } = dcmjsImaging;\n\nmsg.payload = renderFile(buf.buffer);\nmsg.payload.jimp = {\n    w: msg.payload.image.width, h: msg.payload.image.height, background: \"black\"\n}\nreturn msg;\n\n\n\nfunction renderFile(arrayBuffer) {\n    // debugger\n    let frame = 0;\n    const image = new DicomImage(arrayBuffer);\n    const windowLevels = WindowLevel.fromDicomImage(image);\n    const windowLevel =\n        windowLevels.length > 0 ? windowLevels[0] : createDefaultWindowLevel(image);\n\n    const result = {\n        framecount: image.getNumberOfFrames()\n    }\n\n    result.image = renderFrame(image, frame, windowLevel);\n    return result;\n}\n\nfunction renderFrame(image, frame, windowLevel) {\n\n    const result = {\n        height: image.getHeight(),\n        width: image.getWidth(),\n        bitmap: null\n    }\n  \n    try {\n\n        const renderedPixels = new Uint8Array(image.render(frame, windowLevel));\n        // const imageData = ctx.createImageData(image.getWidth(), image.getHeight());\n        // const canvasPixels = imageData.data;\n        const canvasPixels = [];\n        for (let i = 0; i < 4 * image.getWidth() * image.getHeight(); i++) {\n            canvasPixels[i] = renderedPixels[i];\n        }\n        // ctx.putImageData(imageData, 0, 0);\n        result.bitmap = canvasPixels;\n        return result;\n    } catch (err) {\n        throw err;\n    }\n}\n\nfunction createDefaultWindowLevel(image) {\n    const photometricInterpretation = image.getElement('PhotometricInterpretation');\n    if (!photometricInterpretation) {\n        return;\n    }\n    if (['MONOCHROME1', 'MONOCHROME2'].indexOf(photometricInterpretation) == -1) {\n        return;\n    }\n    const bitsStored = image.getElement('BitsStored');\n    if (!bitsStored) {\n        return;\n    }\n    const pixelRepresentation = image.getElement('PixelRepresentation') || 0;\n    const signed = pixelRepresentation !== 0;\n\n    const minValue = signed ? -(1 << (bitsStored - 1)) : 0;\n    const maxValue = signed ? (1 << (bitsStored - 1)) - 1 : (1 << bitsStored) - 1;\n\n    return new WindowLevel(maxValue - minValue, (minValue + maxValue) / 2);\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"dcmjsImaging","module":"dcmjs-imaging"}],"x":1900,"y":120,"wires":[["0559c20ace56b130"]]},{"id":"ce16d978a4c669d7","type":"image viewer","z":"61dce39602934bc2","name":"","width":"175","data":"image","dataType":"msg","active":true,"x":1870,"y":180,"wires":[[]]},{"id":"3faeb01f9beb7838","type":"inject","z":"61dce39602934bc2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1440,"y":120,"wires":[["a48ea31d02b63d22"]]},{"id":"a48ea31d02b63d22","type":"http request","z":"61dce39602934bc2","name":"http get bbmri-53323851.dcm","method":"GET","ret":"bin","paytoqs":"ignore","url":"https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":1660,"y":120,"wires":[["ad5fee391ca75efb"]]},{"id":"0559c20ace56b130","type":"jimp-image","z":"61dce39602934bc2","name":"create blank image","data":"payload.jimp","dataType":"msg","ret":"img","parameter1":"","parameter1Type":"msg","parameter2":"","parameter2Type":"msg","parameter3":"","parameter3Type":"msg","parameter4":"","parameter4Type":"msg","parameter5":"","parameter5Type":"msg","parameter6":"","parameter6Type":"msg","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","sendProperty":"image","sendPropertyType":"msg","parameterCount":0,"jimpFunction":"none","selectedJimpFunction":{"name":"none","fn":"none","description":"Just loads the image.","parameters":[]},"x":1490,"y":180,"wires":[["d3710a9bf19f1a47"]]},{"id":"d3710a9bf19f1a47","type":"change","z":"61dce39602934bc2","name":"set image pixels","rules":[{"t":"set","p":"image.bitmap.data","pt":"msg","to":"payload.image.bitmap","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1700,"y":180,"wires":[["ce16d978a4c669d7"]]}]

If you want to save as PNG, add another image node set to "write" operation & it will save the internal JIMP object as a PNG (or whatever format you set)


NOTE:

For performance reasons, avoid creating branches as more than one wire leaving a node will generate LARGE duplicates of the image data (only process nodes in series when working with large data like images)

3 Likes