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

Iphone将彩色图像转换为2比特图像(黑白),iphone,objective-c,core-graphics,Iphone,Objective C,Core Graphics,我需要帮助将彩色图像转换为“黑白”,而不是灰度 我知道如何使用iPhone SDK和核心图形来实现这一点,因为我确信这是可能的。这是可能的,我以前尝试过两种方法: 转换为灰度,然后对黑白进行逐像素转换。 --这方面的问题是,我不能用透明的图像获得好的效果 如果你不是很严格,给一个RGBA图像,得到像素RGB平均值,用提供的阈值转换成黑白,并保持其透明度。从技术上讲,这仍然是RGBA,但更多的黑色,白色和透明度 e、 g 下面是获取像素数据的代码 + (unsigned char *) getPi

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


我知道如何使用iPhone SDK和核心图形来实现这一点,因为我确信这是可能的。

这是可能的,我以前尝试过两种方法:

  • 转换为灰度,然后对黑白进行逐像素转换。
    --这方面的问题是,我不能用透明的图像获得好的效果

  • 如果你不是很严格,给一个RGBA图像,得到像素RGB平均值,用提供的阈值转换成黑白,并保持其透明度。从技术上讲,这仍然是RGBA,但更多的黑色,白色和透明度

  • e、 g

    下面是获取像素数据的代码

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

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


    看看他的链接:

    我想我会把它扔出去,如果你不想对图像设置阈值,代码看起来会像==>

    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;
    

    黑白难道不是1位图像而不是2位图像吗?2位可以为您提供四种不同的颜色。嗨,Manny,我尝试了这个答案。我得到的错误是“使用未声明的标识符图形”导入的必要包是什么。。请引导我。。我需要这个提前感谢嗨,GraphicUtils是一个自定义的实用程序,它应该很容易获得像素数据只是搜索网络的解决方案。这将是很好的,如果你使用的方法也可用。我不确定该方法返回数据的格式。添加了获取像素数据的代码@阿伦布拉彻
    + (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;
    }
    
    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;