comparison Sphinx/source/users/rest.rst @ 820:fa6d9c7237b4

Downloading decoded images from Python
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Feb 2022 09:34:37 +0100
parents 01fa632daae9
children e95713f90614
comparison
equal deleted inserted replaced
819:a67ceccebf02 820:fa6d9c7237b4
425 425
426 $ curl -H 'Accept: image/x-portable-arbitrarymap' http://localhost:8042/instances/609665c0-c5198aa2-8632476b-a00e0de0-e9075d94/image-uint16 > full-16.pam 426 $ curl -H 'Accept: image/x-portable-arbitrarymap' http://localhost:8042/instances/609665c0-c5198aa2-8632476b-a00e0de0-e9075d94/image-uint16 > full-16.pam
427 427
428 Users of Matlab or Octave can find related information :ref:`in the 428 Users of Matlab or Octave can find related information :ref:`in the
429 dedicated section <matlab>`. 429 dedicated section <matlab>`.
430
431
432 .. _download_numpy:
433
434 Downloading decoded images from Python
435 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
436
437 .. highlight:: python
438
439 Starting with Orthanc 1.10.0, it is possible to immediately download
440 DICOM instances and DICOM series as numpy arrays (even if they use a
441 compressed transfer syntax). This is especially useful for the
442 integration within AI (artificial intelligence) pipelines. Here is a
443 sample call::
444
445 import io
446 import numpy
447 import requests
448
449 r = requests.get('https://demo.orthanc-server.com/instances/6582b1c0-292ad5ab-ba0f088f-f7a1766f-9a29a54f/numpy')
450 r.raise_for_status()
451
452 image = numpy.load(io.BytesIO(r.content))
453 print(image.shape) # (1, 358, 512, 1)
454
455 The downloaded numpy array for one single DICOM instance contains
456 floating-point values, and has a shape of ``(1, height, width, 1)`` if
457 the corresponding instance is grayscale, or ``(1, height, width, 3)``
458 if the instance has colors (e.g. in ultrasound images). If applicable,
459 the ``Rescale Slope (0028,1053)`` and ``Rescale Intercept
460 (0028,1052)`` DICOM tags are applied to the floating-point values.
461
462 Similarly, this feature is available at the series level::
463
464 import io
465 import numpy
466 import requests
467
468 r = requests.get('https://demo.orthanc-server.com/series/dc0216d2-a406a5ad-31ef7a78-113ae9d9-29939f9e/numpy')
469 r.raise_for_status()
470
471 image = numpy.load(io.BytesIO(r.content))
472 print(image.shape) # (100, 256, 256, 1)
473
474 As can be seen, in the case of a DICOM series, the first dimension of
475 the resulting numpy array corresponds to the depth of the series
476 (i.e. to its number of 2D slices).
477
478 Some options are available for these ``/instances/.../numpy`` and
479 ``/series/.../numpy`` routes:
480
481 * ``?compress=1`` will return a ``.npz`` compressed numpy archive
482 instead of a plain ``.npy`` numpy array. This can be used to reduce
483 the network bandwidth. In such a case, the array of interest is
484 named ``arr_0`` in the ``.npz`` archive, e.g.::
485
486 print(image['arr_0'].shape)
487
488 * ``?rescale=0`` will skip the conversion to floating-point values,
489 and will not apply the rescale slope/intercept. This can be useful
490 to reduce the network bandwidth or to receive the original integer
491 values of the voxels.
492
430 493
431 Downloading studies 494 Downloading studies
432 ^^^^^^^^^^^^^^^^^^^ 495 ^^^^^^^^^^^^^^^^^^^
433 496
434 .. highlight:: bash 497 .. highlight:: bash