【问题标题】:Resize a DICOM image in python在 python 中调整 DICOM 图像的大小
【发布时间】:2019-04-07 14:51:04
【问题描述】:

我正在尝试将不同尺寸的 DICOM 图像调整为通用尺寸,以训练我的神经网络。我认为 cv2 可以解决我的问题。但我的 jupyter 笔记本中出现“数据类型不理解错误”

我正在尝试创建一个可以预测图像类别的张量流神经网络。因此,我需要一个共同尺寸的图像进行第一层训练

这是我创建的函数:

IMG_PX_SIZE = 224
def resize(img_dcm):
    return cv2.resize(np.array(img_dcm.pixel_array, (IMG_PX_SIZE,IMG_PX_SIZE)))

这就是我读取 dcm 文件并将其传递给函数的方式:

img = pydi.dcmread(PATH)
image = resize(img)

我希望它输出 224*224 大小的图像。但我收到以下错误:

<ipython-input-66-3cf283042491> in resize(img_dcm)
      1 IMG_PX_SIZE = 224
      2 def resize(img_dcm):
----> 3     return cv2.resize(np.array(image.pixel_array, (IMG_PX_SIZE,IMG_PX_SIZE)))

TypeError: data type not understood

【问题讨论】:

  • 为什么不先将dicom转换成png或jpg等格式,然后再调整大小? https://github.com/danishm/mritopng
  • 是的,我可以这样做,但我想改用 dicom,因为它有很多其他属性,而不是我可以使用的像素数组

标签: python opencv image-processing pydicom medical-imaging


【解决方案1】:

这是使用 Scikit-Image 调整图像大小的另一种方法:

In [105]: from pydicom.data import get_testdata_files

# read a sample image
In [106]: filename = get_testdata_files('MR_small.dcm')[0]
     ...: ds = pydicom.dcmread(filename)

In [107]: data = ds.pixel_array

In [108]: type(data)
Out[108]: numpy.ndarray

In [109]: data.shape
Out[109]: (64, 64)

In [111]: from skimage.transform import resize
In [114]: IMG_PX_SIZE = 32

# resize to new size
In [115]: resized_img = resize(data, (IMG_PX_SIZE, IMG_PX_SIZE), anti_aliasing=True)

In [116]: resized_img.shape
Out[116]: (32, 32)

【讨论】:

    【解决方案2】:

    DICOM 在 OpenCV 中不受支持,请参阅 here。您必须先将所有图像转换为合适的格式(例如 jpg 或 png),然后才能使用 OpenCV 调整它们的大小:

    OpenCV 不支持 DICOM 图像,因此您必须找到一个 合适的库(如 http://dicom.offis.de/dcmtk.php.en )并转换 加载的图像到 cv::Mat。

    然后,您可能还想使用不同的库来重新调整大小,这可能不值得:

    1. 将图像转换为 OpenCV 的可读格式
    2. 使用 OpenCV 重新调整它们的大小
    3. 将它们转换回 DICOM

    我建议您改用专门设计用于处理 DICOM 图像的库或工具。

    【讨论】:

      【解决方案3】:

      您可以从here使用此功能

      您需要先阅读您的 dicom/Niftii 文件

      def read_nifti_file(filepath):
      """Read and load volume"""
      # Read file
         scan = nib.load(filepath)
      # Get raw data
         scan = scan.get_fdata()
         return scan
      

      然后你可以调整你的音量:

      def resize_volume(img):
      """Resize across z-axis"""
      # Set the desired depth
      desired_depth = 64
      desired_width = 128
      desired_height = 128
      # Get current depth
      current_depth = img.shape[-1]
      current_width = img.shape[0]
      current_height = img.shape[1]
      # Compute depth factor
      depth = current_depth / desired_depth
      width = current_width / desired_width
      height = current_height / desired_height
      depth_factor = 1 / depth
      width_factor = 1 / width
      height_factor = 1 / height
      
      # Resize across z-axis
      img = ndimage.zoom(img, (width_factor, height_factor, depth_factor), order=1)
      return img
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-25
        • 1970-01-01
        • 1970-01-01
        • 2018-11-20
        • 1970-01-01
        • 2013-08-17
        • 2010-11-27
        相关资源
        最近更新 更多