ipadのdicomデータからUIImageを取得しようとしています。
コードは次のようになります。
reader->SetFileName(documentsFolderPath/test.dcm);
reader->Update();
ImageType * imageTest = reader->GetOutput(); // get 2d image data
PixelType * pixelData = imageTest->GetBufferPointer();
const void* buffer = pixelData;
// Set the dimensions of the current context
size_t width = 157; // set manually, but correct
size_t height = 143;
// 1 byte per component
size_t bitsPerComponent = 8;
unsigned int multiplier;
bitsPerComponent *= 2; // short = 2 byte
size_t bitsPerPixel, bytesPerRow;
CGColorSpaceRef colorSpace;
CGBitmapInfo bitmapInfo;
CGDataProviderRef theDataProvider;
CFDataRef theDataReference;
bool shouldInterpolate = NO;
CGColorRenderingIntent theIntent;
bitsPerPixel = bitsPerComponent * 1; // because of grayscale image
colorSpace = CGColorSpaceCreateDeviceGray();
bitmapInfo = kCGImageAlphaNone | kCGBitmapByteOrder32Big;
// cg data provider to build the cg image
const UInt8* rawData = static_cast<const UInt8*>(buffer);
// For some reason initiating a CGImage uses BITS per pixel, so we need to divide by 8 to get BYTES
// per pixel
bytesPerRow = (bitsPerPixel/8) * width;
theDataReference = CFDataCreate(NULL,
rawData,
(bytesPerRow*height));
theDataProvider = CGDataProviderCreateWithCFData(theDataReference);
// Finally create the image reference
CGImageRef theImageRef = CGImageCreate(width,
height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorSpace,
bitmapInfo,
theDataProvider,
nil,
shouldInterpolate,
theIntent);
// Construct an output image
UIImage *myImage = [UIImage imageWithCGImage:(theImageRef)];
imageView.image = myImage;
しかし、私の出力画像は次のようになります。
http://www.ettisberger.ch/images/outputImage.png
(塗りつぶすスケールのあるイメージビューでは、サイズは関係ありません)
しかし、背景は黒でなければならず、骨のほとんどのピクセルは多かれ少なかれ白です...
私の失敗を見てくれる人はいますか?
編集: おそらく iPad の 16 ビットが問題になるのでしょうか?
EDIT2:私は解決策を知っています。すべてのピクセルを調べて、0 ~ 255 の unsigned int 値を計算します。コントラストを変更する必要があるため、最大値と最小値は画像全体の最大値と最小値です。それは機能しますが、私の方法では 1 つの uiimage を作成するのに 0.1 ~ 0.2 秒かかります。また、400 個の画像のスタックでは非常に遅く、ユーザーは 1 分間待たなければなりませんでした:/ より良い解決策があるかどうかはわかりません。
for(int i = 0; i < (bytesPerRow*height);i++){
short tmpPixelValue = *pixelData;
short tmpNewPixelValue;
if(tmpPixelValue == 0){
tmpNewPixelValue = 0;
}else{
tmpNewPixelValue = (tmpPixelValue - min) * (255.0 / (max - min));
}
pixelBuffer[i] = (UInt8)tmpNewPixelValue;
pixelData++;
}