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
Dicom in Numpy und verändert zurück
Das funktioniert bei mir:
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::
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::
Hier habe ich ein bisschen DICOM Beispielcode
http://www.magben.de/?h1=3d&h2=dicom_2d
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")
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)
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")
http://www.magben.de/?h1=3d&h2=dicom_2d