Iphone将彩色图像转换为2bit图像(黑白) [英] Iphone convert color image to 2bit image (black-white)
问题描述
我需要帮助才能将彩色图像转换为黑白,而不是灰度。
I need help to convert a color image to "black and white", not grayscale.
我用iPhone SDK和Core Graphics做了什么,我确信这是可能的。
I what to do this with the iPhone SDK and Core Graphics, as I'm convinced this is possible.
推荐答案
这是可能的,我之前尝试过两条路径:
This is possible and I previously tried two paths:
-
转换为灰度,然后逐像素转换为B& W。
- 问题,这是我不使用透明图像获得良好效果。
Convert to Grayscale then apply pixel by pixel conversion to B&W.
--Problem with this is I don't get good results with images with transparency.
如果你不是很严格,给定RGBA图像,得到像素RGB平均值并转换为B& W提供阈值并保持透明度。从技术上讲,这仍然是RGBA,但更多是黑色,白色和透明度。
If you're not very strict, given an RGBA image, get the pixel RGB average and convert to B&W with a supplied threshold and retain its transparency. Technically this is still RGBA but more of Black, White and Transparency.
例如
UIImage *originalImage = [UIImage imageNamed:@"option_bluetooth.png"];
unsigned char *pixelBuffer = [self getPixelData:originalImage.CGImage];
size_t length = originalImage.size.width * originalImage.size.height * 4;
CGFloat intensity;
int bw;
//50% threshold
const CGFloat THRESHOLD = 0.5;
for (int index = 0; index < length; index += 4)
{
intensity = (pixelBuffer[index] + pixelBuffer[index + 1] + pixelBuffer[index + 2]) / 3. / 255.;
if (intensity > THRESHOLD) {
bw = 255;
} else {
bw = 0;
}
pixelBuffer[index] = bw;
pixelBuffer[index + 1] = bw;
pixelBuffer[index + 2] = bw;
}
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext=CGBitmapContextCreate(pixelBuffer, originalImage.size.width, originalImage.size.height, 8, 4*originalImage.size.width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
CFRelease(colorSpace);
free(pixelBuffer);
CGImageRef cgImage=CGBitmapContextCreateImage(bitmapContext);
CGContextRelease(bitmapContext);
UIImage *bwImage = [UIImage imageWithCGImage:cgImage];
我通过写入屏幕外的上下文获取像素数据(获取原始数据的新方法是Apple建议对我不起作用)
I get the pixel data by writing to an offscreen context (the new way of getting the raw data which Apple suggests does not work for me)
例如
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(imageHeight * imageWidth * 4);
CGContextRef offscreenContext = CGBitmapContextCreate(rawData, imageWidth, imageHeight,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(offscreenContext, CGRectMake(0, 0, imageWidth, imageHeight), cgCropped);
CGContextRelease(offscreenContext);
以下是获取Pixel Data的代码
Here's the code to get Pixel Data
+ (unsigned char *) getPixelData: (CGImageRef) cgCropped {
size_t imageWidth = CGImageGetWidth(cgCropped);
size_t imageHeight = CGImageGetHeight(cgCropped);
size_t bitsPerComponent = 8;
size_t bytesPerPixel = 4;
size_t bytesPerRow = bytesPerPixel * imageWidth;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(imageHeight * imageWidth * 4);
CGContextRef offscreenContext = CGBitmapContextCreate(rawData, imageWidth, imageHeight,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(offscreenContext, CGRectMake(0, 0, imageWidth, imageHeight), cgCropped);
CGContextRelease(offscreenContext);
return rawData;
}
这篇关于Iphone将彩色图像转换为2bit图像(黑白)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!