【发布时间】:2014-04-23 14:50:31
【问题描述】:
我正在尝试编写可以将现有图像裁剪到某个指定大小/区域的代码。我正在处理 DICOM 图像,我使用的 API 允许我直接获取像素值。我已将图像中感兴趣区域的像素值放入浮点数组(dstImage,如下)。
我遇到的问题是使用此像素数据实际构建/创建新的裁剪图像文件。源图像是灰度的,但是我在网上找到的所有示例(如this one)都是针对 RGB 图像的。我尝试按照该链接中的示例进行调整,调整灰度并尝试许多不同的值,但我继续在 CGBitmapContextCreate 代码行上遇到错误,并且仍然不清楚这些值应该是什么。
我对源图像的强度值高于 255,所以我的印象是这不是 8 位灰度,而是 16 位灰度。
这是我的代码:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context;
context = CGBitmapContextCreate(dstImage, // pixel data from the region of interest
dstWidth, // width of the region of interest
dstHeight, // height of the region of interest
16, // bits per component
2 * dstWidth, // bytes per row
colorSpace,
kCGImageAlphaNoneSkipLast);
CFRelease(colorSpace);
CGImageRef cgImage = CGBitmapContextCreateImage(context);
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
CFSTR("test.png"),
kCFURLPOSIXPathStyle,
false);
CFStringRef type = kUTTypePNG;
CGImageDestinationRef dest = CGImageDestinationCreateWithURL(url,
type,
1,
0);
CGImageDestinationAddImage(dest,
cgImage,
0);
CFRelease(cgImage);
CFRelease(context);
CGImageDestinationFinalize(dest);
free(dstImage);
我一直收到的错误是:
CGBitmapContextCreate: unsupported parameter combination: 16 integer bits/component; 32 bits/pixel; 1-component color space; kCGImageAlphaNoneSkipLast; 42 bytes/row.
最终目标是从 dstImage 中的像素数据创建图像文件并将其保存到硬盘。非常感谢您对此提供帮助,以及深入了解如何确定我应该在 CGBitmapContextCreate 调用中使用哪些值。
谢谢
【问题讨论】:
标签: objective-c macos image-processing