[英]Saving DICOM image as lossless JPEG
My original image is in DICOM format and I want to save it in lossless jpg format (or at least keep as much information as I can!!!).我的原始图像是 DICOM 格式,我想以无损 jpg 格式保存它(或至少保留尽可能多的信息!!!)。 How can I do that in python?
我怎么能在python中做到这一点? Currently, I am using the following code which produces lossy png image.
目前,我正在使用以下代码生成有损 png 图像。 I call it lossy png because the png image looks different from dicom image when I see dicom image by dicom browser.
我称之为有损 png,因为当我通过 dicom 浏览器看到 dicom 图像时,png 图像看起来与 dicom 图像不同。 Also, how can I modify this image to get jpg image rather than png image.
另外,如何修改此图像以获取 jpg 图像而不是 png 图像。
import numpy as np
import png
import pydicom
ds = pydicom.dcmread("./MyImage.dcm")
shape = ds.pixel_array.shape
# Convert to float to avoid overflow or underflow losses.
image_2d = ds.pixel_array.astype(float)
# Rescaling grey scale between 0-255
image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 256
# Convert to uint
image_2d_scaled = np.uint8(image_2d_scaled)
# Write the PNG file
with open("out.png", 'wb') as png_file:
w = png.Writer(shape[1], shape[0], greyscale=True)
w.write(png_file, image_2d_scaled)
The problem of loosing information depends on pixel depth, not image compression.丢失信息的问题取决于像素深度,而不是图像压缩。 Your DICOM image has probably around 11 bits per pixel (from -1024 to 1023), it may be even more.
您的 DICOM 图像可能每像素大约 11 位(从 -1024 到 1023),甚至可能更多。 And you convert it to 8-bit image without setting an intensity window.
您无需设置强度窗口即可将其转换为 8 位图像。
This means that original pixel of value -1024 becomes black, a pixel of +1023 becomes white, but the most important part of image, from around -100 to +300 becomes mean gray color.这意味着值 -1024 的原始像素变为黑色,+1023 的像素变为白色,但图像最重要的部分,从 -100 到 +300 左右变为平均灰色。 If you are interested in lungs, then the important values are lower, if you want to see bones, then they are higher etc. The most important is, that you cannot see all parts of DICOM image on single png/jpg/bmp image.
如果你对肺感兴趣,那么重要的值就低,如果你想看到骨头,那么它们就更高等等。最重要的是,你不能在单个 png/jpg/bmp 图像上看到 DICOM 图像的所有部分。 You need a special viewer, which allows to set gray levels.
您需要一个特殊的查看器,它允许设置灰度级。 The most common non-medical format, which can handle full pixel depth, is TIFF, but still you need a special viewer.
可以处理全像素深度的最常见的非医学格式是 TIFF,但您仍然需要一个特殊的查看器。
There is another efficient way to convert the Dicom image into JPEG or PNG format using OpenCV and pydicom libraries.还有另一种使用 OpenCV 和 pydicom 库将 Dicom 图像转换为 JPEG 或 PNG 格式的有效方法。 Here below code converts all set of dicom images into jpeg/png in one run (using for loop).
下面的代码在一次运行中将所有 dicom 图像集转换为 jpeg/png(使用 for 循环)。
import pydicom as dicom
import os
import cv2
# make it True if you want in PNG format
PNG = False
# Specify the .dcm folder path
folder_path = "/Image"
# Specify the output .jpg/png folder path
jpg_folder_path = "/JPGImage"
images_path = os.listdir(folder_path)
for n, image in enumerate(sorted(images_path)):
print(os.path.join(folder_path, image))
ds = dicom.dcmread(os.path.join(folder_path, image), force=True)
ds.file_meta.TransferSyntaxUID = dicom.uid.ImplicitVRLittleEndian
pixel_array_numpy = ds.pixel_array
if PNG == False:
image = image.replace('.dcm', '.jpg')
else:
image = image.replace('.dcm', '.png')
pixel_array_numpy2 = cv2.flip(pixel_array_numpy, 0)
cv2.imwrite(os.path.join(jpg_folder_path, image), pixel_array_numpy2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.