python - 在 python 中调整 DICOM 图像的大小
问题描述
我正在尝试将不同尺寸的 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
解决方案
这是使用 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)
推荐阅读
- php - 使 mysql 查询适应 mssql
- java - move xml namespace declarations to root element with jax-ws annotations
- vba - NOT的奇怪行为
- python - Pythonic way to get the max difference between any 2 consecutive elements of a list
- java - 文档显示在单独的布局中,而不是显示在一个布局中?
- docker - 入口路由不适用于使用 helm 部署的服务
- android-ndk - 将地址清理程序与 android NDK 一起使用时的链接问题
- amazon-web-services - 如何编写 IAM 策略,根据标签授予对特定服务的完全访问权限?
- oracle-adf - ADF ComboBox 组件是否可以输入给定 LOV 中不存在的值?
- python-3.x - Unable to get values from a GET request