如何使用python读取Xcode中的.dcm?

2024-09-30 22:22:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个应用程序来查看和分析DICOM切片。我在MATLAB中做过这个应用程序,但是MATLAB没有足够的工具来构建一个非常好的GUI,而且3D图片也很糟糕。所以,我试着用ITK和VTK在Xcode中构建一个应用程序很长一段时间,但是没有成功。有一天我发现xcodeproject PythonDicomDocument——这个项目(用python编写)可以读取并显示DICOM图像!我读过一篇关于python和cocoa的教程,但是我仍然不明白这个项目是如何工作的——它有filePythonDicomDocumentDocument.py:

from Foundation import *
from AppKit import *
from iiDicom import *

import objc


import dicom
import numpy
import Image


class PythonDicomDocumentDocument(NSDocument):
imageView = objc.IBOutlet('imageView')

def init(self):

    self = super(PythonDicomDocumentDocument, self).init()
    self.image = None
    return self

def windowNibName(self):

    return u"PythonDicomDocumentDocument"

def windowControllerDidLoadNib_(self, aController):
    super(PythonDicomDocumentDocument, self).windowControllerDidLoadNib_(aController)
    if self.image:
        self.imageView.setImageScaling_(NSScaleToFit)
        self.imageView.setImage_(self.image)


def dataOfType_error_(self, typeName, outError):
    return None

def readFromData_ofType_error_(self, data, typeName, outError):
    return NO

def readFromURL_ofType_error_(self, absoluteURL, typeName, outError):
    if absoluteURL.isFileURL():
        slice = iiDcmSlice.alloc().initWithDicomFileSlice_(absoluteURL.path())

        dicomImage = slice.sliceAsNSImage_context_(True, None)

        if dicomImage:
            self.image = dicomImage
                            #self.image = dicomImage

            return True, None

    return False, None

文件main.m:

^{pr2}$

}

所以我想“翻译”MATLAB代码以便阅读。dcm:

directory = uigetdir; % after this command Finder window will appear and user will             choose a folder with .dcm files

fileFolder = directory; % the path to the folder is saved to a variable fileFolder
dirOutput = dir(fullfile(fileFolder,'*.dcm')); % choose files .dcm in specified folder     %and save their names 
fileNames = {dirOutput.name}';

Names = char(fileNames);
numFrames = numel(fileNames); % count the number of files in the folder


    for i = 1:numFrames
    Volume(:,:,i) = dicomread(fullfile(fileFolder,Names(i,:))); % create a 3D array of         %DICOM pixel data
    end;

有谁能告诉我如何使用python运行相同的代码来读取Xcode中的.dcm文件???在

我听说python和MATLAB很相似。在


Tags: theimageimportselfnone应用程序returndef
2条回答

恭喜您选择Python来使用DICOM;在我的经验中,SciPy/numpy/matplotlib族在处理海量数据方面比MATLAB(或至少gnuoctave)要好得多。在

使用GDCM的python绑定、GDCM示例中的ConvertNumpy.pymatplotlib加载和显示代码:

#!/usr/bin/env python

import gdcm
import ConvertNumpy
import numpy as np
import matplotlib.pyplot as plt

def loadDicomImage(filename):
    reader=gdcm.ImageReader()
    reader.SetFileName(filename)
    reader.Read()
    gdcmimage=reader.GetImage()
    return ConvertNumpy.gdcm_to_numpy(gdcmimage)

image=loadDicomImage('mydicomfile.dcm')

plt.gray()
plt.imshow(image)
plt.show()

请注意,如果DICOM数据包含的“padding”值明显超出了图像的air-bone范围,则可能会混淆imshow的自动缩放;使用vmax、vmin参数来指定您实际想要查看的范围,或者实现您自己的窗口调平代码(这在numpy中是微不足道的)。在

您可能想看看OsiriX source code,它是一个用Cocoa编写的开源DICOM查看器。在

相关问题 更多 >