简体   繁体   English

将 DICOM 图像保存为无损 JPEG

[英]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.

相关问题 将 12 位 DICOM 图像转换为 8 位 jpeg - converting 12 bit DICOM image to 8 bit jpeg 如何解析和保存 Multipart/related type=image/jpeg 响应? (Dicom Wado 回应) - How to parse and save a Multipart/related type=image/jpeg response? (Dicom Wado Response) 使用PIL无损裁剪jpeg图像? - Is cropping of jpeg images using the PIL lossless? 是否可以在Python中打开ljpg(无损jpeg)文件? - Is it possible to open a ljpg (lossless jpeg) file in Python? 保存前确定jpeg和png图像的图像大小 - determine image size of jpeg and png image before saving it 如何在不保存图像的情况下将 Numpy 数组图像转换为 JPEG? - How to convert a Numpy array image to a JPEG without saving the image? 保存图像时“无法将模式 RGBA 写入 JPEG” - 'cannot write mode RGBA as JPEG' when saving image Python - 从 PostgreSQL 保存和恢复图像/图片/JPEG - Python - Saving and Recovering Image/Pictures/JPEG from PostgreSQL 为什么每次保存时加载 jpeg 或 jpg 图像都会改变它? - Why does loading a jpeg or jpg image changes it upon saving everytime? 将生成的频谱图图像数据作为 jpeg 文件保存到谷歌驱动器中的目录中 - Saving generated spectrogram image data into a directory as jpeg files in google drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM