How to properly rescale pixel values to be in the range [0, 1]? #1656
-
Thanks for this beautiful library! This question is very similar to the one asked on SO. However, I am struggling to apply the suggested solution. I have loaded a CT dicom image having 512 rows and 512 columns: dataset = pydicom.dcmread(dicom_path) Then, I have created a 2D numpy array, called I have converted mask = mask.astype(np.float16)
dataset.PixelData = mask.astype(np.float16).tobytes() Finally, I have written the modified dicom to file: dataset.save_as("dicom_modified.dcm") However, when I open the newly created file with a DICOM viewer software, instead of having My first solution was to applied the method proposed in the comment by @scaramallion in the SO link provided above: if dataset.PixelRepresentation == 0:
mask = mask / (dataset.BitsStored**2 - 1)
else:
mask = (mask + (dataset.BitsStored - 1)**2) / (dataset.BitsStored**2 - 1) but now I get Lastly, I have modified Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Just stumbled over this old question... probably long solved, but I'll comment for anyone else finding this. Your solution uses the modality LUT to scale the output, which is ok as long it is considered by the viewer (which it should be). If you are only interested in the display in the viewer and not in the actual pixel data, this is sufficient. The solution from SO was related to the actual pixel data without considering the modality LUT. As you have a typical CT image with an intercept of -1024, your viewer will take that into account and accordingly shift the pixel values, so setting this to 0 is indeed needed if you don't want this. After that, you could either adapt your pixel data as mentioned, or adapt the modality LUT as you did to change the display of the pixel data in the viewer instead. |
Beta Was this translation helpful? Give feedback.
Just stumbled over this old question... probably long solved, but I'll comment for anyone else finding this.
Your solution uses the modality LUT to scale the output, which is ok as long it is considered by the viewer (which it should be). If you are only interested in the display in the viewer and not in the actual pixel data, this is sufficient.
The solution from SO was related to the actual pixel data without considering the modality LUT. As you have a typical CT image with an intercept of -1024, your viewer will take that into account and accordingly shift the pixel values, so setting this to 0 is indeed needed if you don't want this. After that, you could either adapt your pixel data …