Iphone将彩色图像转换为2bit图像(黑白)

时间:2010-10-27 06:13:09

标签: iphone objective-c core-graphics

我需要帮助才能将彩色图像转换为“黑白”,而不是灰度。

我用iPhone SDK和Core Graphics做了什么,因为我确信这是可能的。

3 个答案:

答案 0 :(得分:3)

这是可能的,我之前尝试过两条路径:

  1. 转换为灰度,然后逐像素转换为B& W. - 对此问题是我的透明图像效果不佳。

  2. 如果您不是非常严格,给定RGBA图像,获取像素RGB平均值并使用提供的阈值转换为B& W并保持其透明度。从技术上讲,这仍然是RGBA,但更多是黑色,白色和透明度。

  3. e.g。

      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建议的原始数据的新方法对我不起作用)

    e.g。

      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的代码

    + (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;
    }
    

答案 1 :(得分:0)

您可以使用CGBitmapContextCreate方法从图像中获取数据。

查看他的链接:https://stackoverflow.com/a/448758/1863223

答案 2 :(得分:0)

我以为我会把它扔出去,如果你不想对图像进行阈值处理,那么代码看起来就像==&gt;

intensity = (pixelBuffer[index] + pixelBuffer[index + 1] + pixelBuffer[index + 2]) / 3.0;

pixelBuffer[index] = (int)intensity;
pixelBuffer[index + 1] = (int)intensity;  
pixelBuffer[index + 2] = (int)intensity;