我试图从 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 之间的无符号 int 值。max 和 min 是整个图片的最大值和最小值,因为我们需要更改对比度。它有效,但我的方法需要 0.1-0.2 秒来创建一个 uiimage - 堆栈 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++;
}