How do I extract pixel data from DICOM files on iOS? - iphone

Does anyone know how I can extract the pixel data from a DICOM file and pass it to an image viewer on iOS?
Sorry if this is a simple question, but it seems to be a major component to a huge can of worms I have opened.

I'm using GDCM on iOS. I haven't pushed it very hard yet but it's working well so far. I basically followed the directions for hacking XCode projects to run in iOS in this excellent article on ITK.
Here's how I got it to compile for iOS:
Downloaded source from sourceforge, installed cmake via ports. You'll need a recent version of cmake (I'm using 2.8.2)
If the source is in a folder called gdcm-2.0.17/, then create another directory at that level (say gdcmbin), cd to that directory, and enter ccmake -GXCode ../gdcm-2.0.17/ in the terminal window. This creates the XCode project. When I did this I didn't create any of the example programs or create shared libraries (which won't work in iOS). Just run the defaults.
Follow the directions in the ITK paper on changing the build options (step #7 on page 4).
Then link GDCM into your project using the excellent instructions at Clint Harris' blog
When you're setting up the header search path in your project to GDCM - you have to enter two paths: blah/gdcm-2.0.17/Source/** and blah/gdcmbin/**. The trailing '/Source' on the first path is necessary - otherwise you get headers that aren't appropriate for your architecture.
One glitch (annoying but haven't spent the time to figure it out yet): you get a bunch of linking errors when you switch from simulator to device (or vice versa). This is because the gdcm project doesn't put the outputs into different directories for different targets. So - run a clean and rebuild in the gdcm project when you're switching. I'll probably get annoyed by this soon enough to change it :-).
Here's a rough snippet of how you'd call the library and put the results in an Objective-C dictionary:
NSMutableDictionary * imageDictionary = [[NSMutableDictionary alloc] initWithCapacity:40];
// The output of gdcm::Reader is a gdcm::File
gdcm::File &file = reader.GetFile();
// the dataset is the the set of element we are interested in:
gdcm::DataSet &ds = file.GetDataSet();
const Tag studyInstance(0x0020,0x000d); // Study Instance UID
const DataElement &dicomVal = ds.GetDataElement(studyInstance);
std::string stringVal( dicomVal.GetByteValue()->GetPointer(), dicomVal.GetByteValue()->GetLength() );
NSString *val = [NSString stringWithCString:stringVal.c_str() encoding:[NSString defaultCStringEncoding]];
[imageDictionary setObject:val forKey:#"studyInstanceUID"];
(Note: this is in an *.mm file that mixes C++ and ObjectiveC)

Imebra has an Objective-C wrapper that can be used also with Swift.
#import "imebraobjc/imebra.h"
// Get the DICOM dataset from file
NSError* error = nil;
ImebraDataSet* pDataSet = [ImebraCodecFactory loadFromFile:#"test.dcm" error:&error]
// CHeck the patient name (automatically convert from DICOM charsets to UTF-8)
NSString* checkPatientName = [pDataSet getString:[[ImebraTagId alloc] initWithGroup:0x10 tag:0x10] elementNumber:0 error:&error]; // Automatically converted to UTF-8 if necessary
// Get the frame 0
ImebraImage* pCheckImage = [pDataSet getImageApplyModalityTransform:0 error:&error];
// Get the image data
ImebraReadingDataHandlerNumeric* readingDataHandler = [pCheckImage getReadingDataHandler:&error];
// Scan the pixels. Access the data handler memory for faster data access
for(unsigned int pixel(0); pixel != readingDataHandler.size; ++pixel)
{
unsigned int pixelValue = [readingDataHandler getUnsignedLong:pixel error:&error];
}
ImebraDrawBitmap* pDrawBitmap = [[ImebraDrawBitmap alloc] init];
// Obtain a NSImage (UIImage on iOS)
NSImage* pNsImage = [pDrawBitmap getImebraImage:pCheckImage error:&pError];

If you want to find DICOM software, look at idoimaging.com, a clearinghouse for medical imaging software. You can choose your platform, input format, output format, language, etc. iOS isn't listed as a format, but much of the software listed there is available with source, useful in library form, and available for MacOS X. For example, I selected:
input format: DICOM
platform: Macintosh
language: C
and found several packages. Given the similarities between MacOS and iOS and the fact that some of these are cross-platform with source included, it shouldn't be too difficult to get one of them working on iOS.

Related

How to load Vuforia QCAR tracking file data during runtime in iOS

I am working on Augmented reality using Vuforia sdk, and i want to load 3d object on runtime, i want to fetch it from web-service and load it when i need that, So basically my question only is this how can we load any .h file on run time. First is this possible that we can load it on runtime and if possible then how can we do that.
If you have dynamic data, putting it into a .h file doesn't make sense. Headers are only useful while the program is being compiled, not at run time.
AFAIK vuforia just puts arrays of vertical, triangles, etc. into h file. So you don't need to load h file in runtime (and this is impossible, there is no h files when programm is running). Instead you need to get those arrays and give them to vuforia
just download that file from network, store it within application directories and load it like the sample data bundled with the app
NSString * pathToDownloadedFile; //should have path to your file
QCAR::DataSet *dataSet = nil;
QCAR::TrackerManager& trackerManager = QCAR::TrackerManager::getInstance();
QCAR::ImageTracker* imageTracker = static_cast<QCAR::ImageTracker*> (trackerManager.getTracker(QCAR::Tracker::IMAGE_TRACKER));
dataSet = imageTracker->createDataSet();
dataSet->load([pathToDownloadedFile cStringUsingEncoding:NSASCIIStringEncoding], QCAR::DataSet::STORAGE_ABSOLUTE);
imageTracker->activateDataSet(dataSet);

Cannot load country-specific resource

I am loading text resources in different languages from text files using:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"txt"];
if(filePath) {
NSString *myText = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
}
Works very well with my language specific resources in English, German etc.
But I do also have some country-specific resources, like an English text for United Kingdom, an English text for USA, a German text for Austria etc.
I would expect these resources to be loaded if the phone is set to the specific country (Region Format), but they don't. The filepath will always be set to the file in the "pure" language folder.
So for example I have myfile.txt in the folders:
en.lproj
en-GB.lproj
en-US.lproj
but it always loads the one in en.lproj
Folders have been created using the localization function in Xcode and are all located in the same folder.
What is a bit odd is that these folders created by Xcode do seperate language and country by a hyphen, but according to the documentation it should be an underscore. I have tried to copy folders (with underscores) into the project manually, but it still doesn't work.
How do I manage to load the country-specific files?
Thanks.....
I created a "dutch-netherlands" translation of Localisations.string and set iPhone language to dutch, but NSLocalizedString always returned the English translation. The solution was to create a "dutch" translation (nl.lproj) instead of "dutch-netherlands" (nl-NL.lproj).
In the settings screen of iOS4 or iOS5, you can select Dutch language but there's no subselection for Dutch-Netherlands or Dutch-Belgium, even though these are available in XCode. It seems to me that XCode offers more translation options than that are actually supported by iOS.
I mean there's no way to make your app use "nl-NL" resources. I guess the same problem goes for German and Austria ("de-DE" and "de-AT").
Note: Your code sample is a complicated way of loading text resources. It's fine if you are loading non-string resources such as images or media files, but for simple strings it's much easier to just use one .strings file per language (containing multiple localized strings) and call NSLocalizedString to get the relevant language version of a given string.
Answer: The iOS setting under "Region Format" does not affect resource loading. Instead it controls how the system performs locale-dependent functionality (such as date/number formatting and parsing). The iOS setting that controls which language version of your localized resources gets loaded is the Language setting (General > International > Language). Currently, it includes only two English variants: "English" and "British English".
I have the same problem with nl-NL.lproj (Netherland) and ca-ES.lproj (Catalan). The solution for me is add the language using Xcode. Go to Xcode Project, and then in Info Tab add the langugages. Automatically will create the folders nl-NL.lproj and ca-ES.lproj.
Then you can try this on Simulator (Xcode version 4.6) and these languages don't word, instead will load English language.
First rename manually the folder nl-NL.lproj to nl.lproj.
Finally go to file {MY_PROJECT}.xcodeproj/project.pbxproj and replace the PATH of the following lines:
C3D2888817108DDA00CE8AC2 /* nl-NL */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "nl-NL"; path = "nl.lproj/Localizable.strings"; sourceTree = "<group>"; };
C3D2888917108DE500CE8AC2 /* nl-NL */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "nl-NL"; path = "nl.lproj/InfoPlist.strings"; sourceTree = "<group>"; };
That's all, now I can get these languages settings correctly.

iOS support for Canon RAW format?

I know the iPad can read Canon RAW (.CR2) files when used with the Camera Kit but is any of that file format reading accessible to an iOS developer? Or are we just limited to things supported in the UIImage Class Reference document?
(https://developer.apple.com/documentation/uikit/uiimage)
For something like this, Apple's image APIs are not going to be sufficient for what you need. However, there are a few third party APIs that should work with the iPhone. Libraw is a good one that I found that has a C interface. Since Objective-C is a strict superset of C, you will probably be able to use it in your iOS application.
From what I see in their docs, you might be able to write a CR2 image as a TIFF, then decode it as a UIImage like this:
NSString * cr2Path = #"path/to/cr2/file";
NSString * tiffPath = [NSTemporaryDirectory stringByAppendingPathComponent:#"x.tiff"];
libraw_data_t * cr2Data = libraw_init(0);
libraw_open_file(cr2Data, [cr2Path UTF8String]);
libraw_unpack(cr2Data);
// setup encoding params
cr2Data->params.output_tiff = 1;
cr2Data->params.use_camera_wb = 1;
// encode and write as tiff
libraw_dcraw_process(cr2Data);
libraw_dcraw_ppm_tiff_writer(cr2Data, [tiffPath UTF8String]);
libraw_recycle(cr2Data); // free the memory
// load the UIImage
UIImage * myImage = [[UIImage alloc] initWithContentsOfFile:tiffPath];
// use myImage here ...
[myImage release];
From what I have seen, it looks like you will be able to download it here and then add the src files from the src/, libraw/, dcraw/, and internal/ directories to your Xcode project. Hopefully you will not run into any weird linking/compiling issues.

Files in folders not found in iOS app using C++

I'm trying to read files stored in assets folder and its subfolders using std::ifstream in an iOS app written mostly in C++ (The same code is also used in other, non-iOS projects), but they're not found. Example: there is a file assets/shaders/ortho2d.vert and I'm trying to load it like this:
std::ifstream vertFStream( vertFile ); // vertFile's contents is "assets/shaders/ortho2d.vert"
if (!vertFStream) {
std::cerr << vertFile << " missing!" << std::endl;
exit( 1 );
}
I've added the assets folder to the XCode project as a blue folder and it shows up in Targets->Copy Bundle Resources.
Try this:
NSBundle *b = [NSBundle mainBundle];
NSString *dir = [b resourcePath];
NSArray *parts = [NSArray arrayWithObjects:
dir, #"assets", #"shaders", #"ortho2d.vert", (void *)nil];
NSString *path = [NSString pathWithComponents:parts];
const char *cpath = [path fileSystemRepresentation];
std::string vertFile(cpath);
std::ifstream vertFStream(vertFile);
You may need to check the relative path from where the application is running and probably use a full path to ensure the file is found.
The fact that the open failed does not necessarily mean the file is not found, it just might not be readable at this moment. (Incorrect permissions or file locked).
exit(1) is rather drastic.
sorry but some punctuations:
on iOS using file system calls from C++ is highly discouraged for security issues and limited support from a security point of view.
calls to file system should be done afer you know decently iOS app
folder layout. (bundles, resources, Documents folder" and so on..)
otherwise it will fail. c) you can mix c++ and objC but definitively
is not a correct approach.
under iOS you must use swift or objC (excect in very limited cases)
use iOS APIs, exactly as under android you would use java

Compile error in CorePlot-CocaTouch project

I'm having trouble compiling the current release.
I was able to download a copy of the source distribution today using:
hg clone https://core-plot.googlecode.com/hg/ core-plot
I opened the "core-plot/framework".
I then double clicked on CorePlot-CocoaTouch.xcodeproj to launch Xcode.
When I build the project I get the following error:
-(void)bind:(NSString *)binding toObject:(id)observable withKeyPath:(NSString *)keyPath options:(NSDictionary *)options
{
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
[NSException raise:CPException format:BindingsNotSupportedString];
Format not a string literal and no formal arguments
#else
[super bind:binding toObject:observable withKeyPath:keyPath options:options];
#endif
}
I am running on a new MacBook with OS 10.6, and IPhone Simulator 4.0.
Any help will be greatly appreciated.
Charles
A more appropriate place to ask this question would be the Core Plot mailing list, because I'm one of the few developers for the project that regularly visits here.
That said, the issue here is that we're using a string constant for a format string, which Xcode now seems to be warning about (rightly so, as this can lead to problems). To work around this for now, you can replace the line in CPLayer.m
static NSString * const BindingsNotSupportedString = #"Bindings are not supported on the iPhone in Core Plot";
with
#define BindingsNotSupportedString #"Bindings are not supported on the iPhone in Core Plot"
Search in your project for BindingsNotSupportedString, seems like it's not in the current file and therefore needs to be included. Or just try to change this to an acceptable format.