Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python在Xcode中读取.dcm?_Python_Xcode_Cocoa_Matlab_Dicom - Fatal编程技术网

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

如何使用python在Xcode中读取.dcm?,python,xcode,cocoa,matlab,dicom,Python,Xcode,Cocoa,Matlab,Dicom,我正在尝试创建一个用于查看和分析DICOM切片的应用程序。我在MATLAB中开发了这个应用程序,但是MATLAB没有足够的工具来构建一个非常好的GUI,3D图片也很糟糕。所以,我尝试使用ITK和VTK在Xcode中构建一个应用程序很长一段时间,但没有成功。有一天我发现了xcodeproject PythonDicomDocument——这个项目(用python编写)可以读取和显示DICOM图像!我已经阅读了一篇关于python和cocoa的教程,但我仍然无法理解这个项目是如何工作的——它有一个文

我正在尝试创建一个用于查看和分析DICOM切片的应用程序。我在MATLAB中开发了这个应用程序,但是MATLAB没有足够的工具来构建一个非常好的GUI,3D图片也很糟糕。所以,我尝试使用ITK和VTK在Xcode中构建一个应用程序很长一段时间,但没有成功。有一天我发现了xcodeproject PythonDicomDocument——这个项目(用python编写)可以读取和显示DICOM图像!我已经阅读了一篇关于python和cocoa的教程,但我仍然无法理解这个项目是如何工作的——它有一个文件pythondicomDocumentDocumentDocument.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
**#import "<"Python/Python.h>**

**#import "<"Cocoa/Cocoa.h>**


int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *resourcePath = [mainBundle resourcePath];
    NSArray *pythonPathArray = [NSArray arrayWithObjects: resourcePath,               [resourcePath stringByAppendingPathComponent:@"PyObjC"],      @"/System/Library/Frameworks/Python.framework/Versions/Current/Extras/lib/python/", nil];

setenv("PYTHONPATH", [[pythonPathArray componentsJoinedByString:@":"] UTF8String], 1);

    NSArray *possibleMainExtensions = [NSArray arrayWithObjects: @"py", @"pyc",            @"pyo", nil];
    NSString *mainFilePath = nil;

for (NSString *possibleMainExtension in possibleMainExtensions) {
    mainFilePath = [mainBundle pathForResource: @"main" ofType: possibleMainExtension];
    if ( mainFilePath != nil ) break;
}

if ( !mainFilePath ) {
    [NSException raise: NSInternalInconsistencyException format: @"%s:%d main() Failed to find the Main.{py,pyc,pyo} file in the application wrapper's Resources directory.", __FILE__, __LINE__];
}

Py_SetProgramName("/usr/bin/python");
Py_Initialize();
PySys_SetArgv(argc, (char **)argv);

    const char *mainFilePathPtr = [mainFilePath UTF8String];

    FILE *mainFile = fopen(mainFilePathPtr, "r");



int result = PyRun_SimpleFile(mainFile, (char *)[[mainFilePath lastPathComponent] UTF8String]);



if ( result != 0 )
    [NSException raise: NSInternalInconsistencyException
                format: @"%s:%d main() PyRun_SimpleFile failed with file '%@'.  See console for errors.", __FILE__, __LINE__, mainFilePath];

[pool drain];

return result;
文件main.m:

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
**#import "<"Python/Python.h>**

**#import "<"Cocoa/Cocoa.h>**


int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *resourcePath = [mainBundle resourcePath];
    NSArray *pythonPathArray = [NSArray arrayWithObjects: resourcePath,               [resourcePath stringByAppendingPathComponent:@"PyObjC"],      @"/System/Library/Frameworks/Python.framework/Versions/Current/Extras/lib/python/", nil];

setenv("PYTHONPATH", [[pythonPathArray componentsJoinedByString:@":"] UTF8String], 1);

    NSArray *possibleMainExtensions = [NSArray arrayWithObjects: @"py", @"pyc",            @"pyo", nil];
    NSString *mainFilePath = nil;

for (NSString *possibleMainExtension in possibleMainExtensions) {
    mainFilePath = [mainBundle pathForResource: @"main" ofType: possibleMainExtension];
    if ( mainFilePath != nil ) break;
}

if ( !mainFilePath ) {
    [NSException raise: NSInternalInconsistencyException format: @"%s:%d main() Failed to find the Main.{py,pyc,pyo} file in the application wrapper's Resources directory.", __FILE__, __LINE__];
}

Py_SetProgramName("/usr/bin/python");
Py_Initialize();
PySys_SetArgv(argc, (char **)argv);

    const char *mainFilePathPtr = [mainFilePath UTF8String];

    FILE *mainFile = fopen(mainFilePathPtr, "r");



int result = PyRun_SimpleFile(mainFile, (char *)[[mainFilePath lastPathComponent] UTF8String]);



if ( result != 0 )
    [NSException raise: NSInternalInconsistencyException
                format: @"%s:%d main() PyRun_SimpleFile failed with file '%@'.  See console for errors.", __FILE__, __LINE__, mainFilePath];

[pool drain];

return result;
有人能告诉我如何使用python运行相同的代码来读取Xcode中的.dcm文件吗


我听说python和MATLAB很相似。

祝贺您选择python与DICOM一起工作;根据我的经验,/numpy/matplotlib族在处理海量数据方面比MATLAB(或者至少是gnuoctave)要好得多

使用的python绑定加载和显示代码的琐事,来自GDCM的示例和:

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