(python)将医学图像转换为jpg和png的实用程序

med2image的Python项目详细描述


快速概述

  • 将DICOM或NIFTI转换为JPG或PNG

概述

med2image是一个简单的python3实用程序,用于转换医学图像 格式化后的文件,如png和jpg。

目前,nifti和dicom的输入格式是可以理解的,而任何 可以生成matplotlib支持的图形输出类型。

依赖关系

确保在主机上安装了以下依赖项 系统/Python3虚拟环境:

  • nibabel(读取nifti文件)
  • pydicom(读取dicom文件)
  • matplotlib(以各种图像格式保存数据)
  • 枕头(以JPG格式保存数据)

安装

安装此脚本及其所有依赖项的最佳方法是 从pypi获取它

pip3 install med2image

您可能会得到一个关于未安装python3 tk的错误。所以你应该安装那个软件包。 例如在ubuntu上:

sudo apt-get update
sudo apt-get install -y python3-tk

命令行参数

-i|--inputFile <inputFile>
Input file to convert. Typically a DICOM file or a nifti volume.

[-d|--outputDir <outputDir>]
The directory to contain the converted output image files.

-o|--outputFileStem <outputFileStem>
The output file stem to store conversion. If this is specified
with an extension, this extension will be used to specify the
output file type.

SPECIAL CASES:
For DICOM data, the <outputFileStem> can be set to the value of
an internal DICOM tag. The tag is specified by preceding the tag
name with a percent character '%', so

    -o %ProtocolName

will use the DICOM 'ProtocolName' to name the output file. Note
that special characters (like spaces) in the DICOM value are
replaced by underscores '_'.

Multiple tags can be specified, for example

    -o %PatientName%PatientID%ProtocolName

and the output filename will have each DICOM tag string as
specified in order, connected with dashes.

A special %inputFile is available to specify the input file that
was read (without extension).

[-t|--outputFileType <outputFileType>]
The output file type. If different to <outputFileStem> extension,
will override extension in favour of <outputFileType>.

[-s|--sliceToConvert <sliceToConvert>]
In the case of volume files, the slice (z) index to convert. Ignored
for 2D input data. If a '-1' is sent, then convert *all* the slices.
If an 'm' is specified, only convert the middle slice in an input
volume.

[-f|--frameToConvert <sliceToConvert>]
In the case of 4D volume files, the volume (V) containing the
slice (z) index to convert. Ignored for 3D input data. If a '-1' is
sent, then convert *all* the frames. If an 'm' is specified, only
convert the middle frame in the 4D input stack.

[--showSlices]
If specified, render/show image slices as they are created.

[--reslice]
For 3D data only. Assuming [i,j,k] coordinates, the default is to save
along the 'k' direction. By passing a --reslice image data in the 'i' and
'j' directions are also saved. Furthermore, the <outputDir> is subdivided into
'slice' (k), 'row' (i), and 'col' (j) subdirectories.

[-x|--man]
Show full help.

[-y|--synopsis]
Show brief help.

nifti转换

理解3d和4d nifti输入数据。对于4D Nifti, 特定的帧可以与特定的切片一起指定 索引。在大多数情况下,只需要一个切片,因为大多数nifti数据是 而且,所有的切片都可以转换,或者只转换中间的切片。

示例

卷内所有切片

要转换名为vol.nii的输入nifti卷中的所有切片,请保存 在一个被调用的目录中的结果,并将其用作文件干的输出 命名图像,do

med2image -i vol.nii -d out -o image.jpg -s-1

或者更详细地说,

med2image --inputFile vol.nii     --outputDir out      \
          --outputFileStem image  --outputFileType jpg \
          --sliceToConvert -1

这将创建以下文件输入输出

image-slice000.jpg
image-slice001.jpg
image-slice002.jpg
image-slice003.jpg
image-slice004.jpg
image-slice005.jpg
image-slice006.jpg
image-slice007.jpg
...
image-slice049.jpg
image-slice050.jpg
image-slice051.jpg
image-slice052.jpg
image-slice053.jpg

仅转换一个切片

大多数情况下,您可能只想转换 卷(例如生成 音量)。为此,只需指定m to–slicetoconvert

med2image -i input.nii -o input.jpg -s m

或者,同样,稍微详细一点,使用outputdirectory说明符

med2image -i input.nii -d out -o vol --outputFileType jpg --sliceToConvert m

或者,可以转换特定的切片索引。使用

med2image -i input.nii -d out -o vol --outputFileType jpg --sliceToConvert 20

只转换卷的第20个部分。

DICOM转换

转换单个dicom文件

要将名为slice.dcm的单个DICOM文件转换为slice.jpg,请执行以下操作:

med2image -i slice.dcm -o slice.jpg

它将在当前目录中创建一个文件slice.jpg。

转换目录/系列中的所有DICOM

要转换目录中的所有dicom,只需将a'-1'指定为 切片索引:

med2image -i inputDir/slice.dcm -d outputDir -o slice.jpg -s-1

注意,这假设所有的dicom文件都在inputdir目录下 属于同一系列。

多方向重新标记

默认情况下,只有采集方向上的切片 转换。但是,通过向脚本传递-r,所有维度都是 转换。因为剧本不知道 图像和方向简单地标记为X、Y和Z。

Z方向是原始采集(切片)方向,而X方向 y对应于垂直于行和列方向的平面。

转换后的图像存储在标有X、Y和Z的子目录中。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java GridBagLayout不填充区域   java Memozied Fibonacci未运行与常规Fibonacci解决方案   Java Web启动未启动问题   Java中异常和if-then的区别   java从命令提示符运行批处理文件获取错误   socket在Java中验证SSL证书的公共名称   如何在JAVA中检查字符串数组中的相等字   用java语言将音频文件转换成文本文件的语音识别   java为什么foo(1,2,3)没有传递给varargs方法foo(对象…)作为整数[]   java通过蓝牙将奇怪的数据从Arduino传输到Android   java ContainerRequestFilter获取空entitystream   java如何从安卓 studio中删除不兼容类型错误   基本Java错误   在Spring引导中使用REST API时发生java错误   javascript通过从SQL查询派生的URL打开页面