Dicom in Numpy und verändert zurück

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
Ormel
User
Beiträge: 2
Registriert: Dienstag 29. September 2015, 12:08

Hey Leute,

ich bastle gerade mit Python, Numpy und Pydicom ein wenig an CTs herum.
Aus meinem Dicom Bild ein Numpy Array zu erstellen und damit zu rechnen funktioniert auch wunderbar,
aber ich finde keine Möglichkeit, aus meinem Numpy Array wieder ein Dicom Bild zu erstellen.
Bzw. meinem bestehenden Dicom Bild das neue Pixel Array zuzuweisen.

Kann mir da jemand weiterhelfen?

Beste Grüße
Benutzeravatar
MagBen
User
Beiträge: 799
Registriert: Freitag 6. Juni 2014, 05:56
Wohnort: Bremen
Kontaktdaten:

Das funktioniert bei mir:

Code: Alles auswählen

import dicom
data = dicom.read_file("IMG00129")
pixel_array = data.PixelArray
pixel_array[:] = 0
data.PixelData = pixel_array.tostring()
data.save_as("zero.dcm")
Das veränderte Numpy-Array muss also als String zurückgeschrieben werden.

dicom/docs/working_with_pixel_data.rst, Zeile 37 bis 60:
A property of Dataset called ``pixel_array`` provides more useful pixel data
for uncompressed images. The ``pixel_array`` property returns a NumPy array::

Code: Alles auswählen

    >>> import dicom
    >>> ds=pydicom.read_file("MR_small.dcm")
    >>> ds.pixel_array
    array([[ 905, 1019, 1227, ...,  302,  304,  328],
           [ 628,  770,  907, ...,  298,  331,  355],
           [ 498,  566,  706, ...,  280,  285,  320],
           ...,
           [ 334,  400,  431, ..., 1094, 1068, 1083],
           [ 339,  377,  413, ..., 1318, 1346, 1336],
           [ 378,  374,  422, ..., 1369, 1129,  862]], dtype=int16)
    >>> ds.pixel_array.shape
    (64, 64)
NumPy can be used to modify the pixels, but if the changes are to be saved,
they must be written back to the ``PixelData`` attribute::

Code: Alles auswählen

    >>> for n,val in enumerate(ds.pixel_array.flat): # example: zero anything < 300
    ...     if val < 300:
    ...         ds.pixel_array.flat[n]=0
    >>> ds.PixelData = ds.pixel_array.tostring()
    >>> ds.save_as("newfilename.dcm")
Hier habe ich ein bisschen DICOM Beispielcode
http://www.magben.de/?h1=3d&h2=dicom_2d
Bild
a fool with a tool is still a fool, www.magben.de, YouTube
Ormel
User
Beiträge: 2
Registriert: Dienstag 29. September 2015, 12:08

Vielen Dank!

Super Antwort :)

Das tostring() hatte mir gefehlt, jetzt macht das Programm was es soll ;)
Antworten