ITK library on iOS - Loading DICOM
I have built the ITK library for the ipad - and it works. Then I tried to make an ITK example - something like that:
// Load DICOM files
typedef itk::ImageSeriesReader< InputImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
typedef itk::GDCMImageIO Imag开发者_StackOverflow社区eIOType;
typedef itk::GDCMSeriesFileNames NamesGeneratorType;
ImageIOType::Pointer gdcmIO = ImageIOType::New();
NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
namesGenerator->SetInputDirectory( "C:/test" );
But I tried a lot of possibilites to load a DICOM
stack in a directory on the documents folder of the ipad instead of the c:/test
path. But that didn't work.
So my idea is to load a DICOM
like that over the internet:
NSData *dicomImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.someurl.ch/dicom/blabla.dcm"]];
And now I think about trying to get out the dicom data (patient name etc) and separate it from the image data. Then I think it must be possible to have at the end an UIImage to display on the IPAD.
I searched for an example for that, but sadly...i didnt found something good. If anybody has got an idea how to load a dcm on the ipad through ITK
or an idea how to get the image data out of the NSData
object?
ITK actually uses GDCM to read DICOM files, so it's probably easier to use GDCM directly. http://sourceforge.net/apps/mediawiki/gdcm/index.php
As for loading DICOM files on the iPad (or any mobile device), I would be careful when doing so. Some DICOM files are very large (on the order of GBs), and that would probably just crash your application. Of course, you'd probably have a difficult time loading files that large onto the iPad anyway :)
The pixel data is not necessarily RGB data as you would expect to find in a JPEG. Check the photometric interpretation. If it's RGB, then you're good to go (after decoding & decompression). If it's monochrome, you may need to convert to RGB values (see How to translate DICOM image width and level to JPEG brightness and contrast?) before passing the data to UIImage.
You may want to investigate DCMTK. On an iphone you will likely need the network protocol capabilities that are in DCMTK.
Since my last post, I have now a little solution for my problem.
namesGenerator->SetInputDirectory( documentFolderPath );
typedef std::vector<std::string> FileNamesContainer;
FileNamesContainer fileNames = nameGenerator->GetInputFileNames();
reader->SetFileNames( fileNames );
reader->Update();
ImageType * imageTest = reader->GetOutput(); // get 3d volume
PixelType * pixelData = imageTest->GetBufferPointer(); // get bufferpointer
With that, I can load DICOM stacks and get out the whole dicom header information :) Now my problem is about getting the image pixel data into an UIImage. I can load single pixelvalues with this code:(just for a test, getPixel is a slow method)
ImageType::IndexType pixelIndex;
pixelIndex[0] = 100;
pixelIndex[1] = 100;
pixelIndex[2] = 0;
ImageType::PixelType pixelValue = imageTest->GetPixel( pixelIndex );
But my problem is, that I dont understand how I can handle the data of *pixeldata (the bufferpointer) to create a UIImage. Sadly I didnt found some example in the ITK documentation :(
//assume that the image width is 880 and the image height is 635
int imageWidth = 880;
int imageHeight = 635;
NSString *dicomPath = [[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/"] stringByAppendingString:@"your dicom file name"];
const char *c_dicomPath = [dicomPath UTF8String];
typedef unsigned char InputPixelType;
const unsigned int InputDimension = 3;
typedef itk::Image< InputPixelType, InputDimension > InputImageType;
typedef itk::ImageFileReader< InputImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(c_dicomPath);
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
reader->SetImageIO(gdcmImageIO);
InputPixelType *imageBuf = (InputPixelType*)malloc(sizeof(InputPixelType)*imageHeight*imageWidth*3);
reader->Update();
//get dicom image
memset(imageBuf, 0, sizeof(InputPixelType)*imageHeight*imageWidth*3);
gdcmImageIO->Read(imageBuf);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithData(nil, imageBuf, imageWidth*imageHeight*3*sizeof(InputPixelType), nil);
CGImageRef imageRef = CGImageCreate(imageWidth,//width
imageHeight,//height
8,//size_t bitsPerComponent,
24,//size_t bitsPerPixel,
imageWidth*sizeof(InputPixelType)*3,//size_t bytesPerRow,
colorspace,//CGColorSpaceRef space,
kCGBitmapByteOrderDefault,//CGBitmapInfo bitmapInfo,
provider,//CGDataProviderRef provider,
nil,//const CGFloat *decode,
NO,//bool shouldInterpolate,
kCGRenderingIntentDefault//CGColorRenderingIntent intent
);
//here is the dicom image decode from dicom file
UIImage *dicomImage = [[UIImage alloc] initWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationUp];
精彩评论