11

关于如何获取给定点的图像像素颜色有许多问题/答案。但是,对于大图像(例如,即使小到 1000 x 1300),所有这些答案都非常慢(100-500 毫秒)。

那里的大多数代码示例都绘制到图像上下文。当实际抽签发生时,所有这些都需要时间:

CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage)

在 Instruments 中检查这一点会发现,绘制是通过从源图像中复制数据来完成的:

在此处输入图像描述

我什至尝试了另一种获取数据的方法,希望获取字节本身实际上会更有效。

NSInteger pointX = trunc(point.x);
NSInteger pointY = trunc(point.y);
CGImageRef cgImage = CGImageCreateWithImageInRect(self.CGImage, 
                           CGRectMake(pointX * self.scale, 
                                      pointY * self.scale, 
                                      1.0f, 
                                      1.0f));

CGDataProviderRef provider = CGImageGetDataProvider(cgImage);
CFDataRef data = CGDataProviderCopyData(provider);

CGImageRelease(cgImage);

UInt8* buffer = (UInt8*)CFDataGetBytePtr(data);

CGFloat red   = (float)buffer[0] / 255.0f;
CGFloat green = (float)buffer[1] / 255.0f;
CGFloat blue  = (float)buffer[2] / 255.0f;
CGFloat alpha = (float)buffer[3] / 255.0f;

CFRelease(data);

UIColor *pixelColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

return pixelColor;

此方法在数据复制上花费时间:

CFDataRef data = CGDataProviderCopyData(provider);

它似乎也在从磁盘读取数据,而不是CGImage我正在创建的实例:

在此处输入图像描述

现在,这种方法在一些非正式的测试中确实表现得更好,但它仍然没有我想要的那么快。 有谁知道获取底层像素数据的更快方法???

4

4 回答 4

6

如果您可以通过 OpenGL ES 将此图像绘制到屏幕上,那么您可以通过该版本中引入的纹理缓存获得对 iOS 5.0 中底层像素的极快随机访问。它们允许对存储在 OpenGL ES 纹理(图像所在的位置)中的底层 BGRA 像素数据进行直接内存访问,并且您几乎可以立即从该纹理中挑选出任何像素。

我用它来读回甚至是大(2048x2048)图像的原始像素数据,读取时间最差是在 10-20 毫秒的范围内,以拉低所有这些像素。同样,随机访问单个像素几乎不需要时间,因为您只是从字节数组中的位置读取。

当然,这意味着您必须解析您的特定图像并将其上传到 OpenGL ES,这将涉及从磁盘读取和与 Core Graphics 的交互(如果通过 UIImage),您会看到如果您尝试从磁盘上的随机 PNG 读取像素数据,但听起来您只需要渲染一次并从中多次采样。如果是这样,OpenGL ES 和 iOS 5.0 上的纹理缓存将是读取此像素数据以获取屏幕上显示的内容的绝对最快方法。

我将这些过程封装在我的开源GPUImage框架内的 GPUImagePicture(图像上传)和 GPUImageRawData(快速原始数据访问)类中,如果你想看看类似的东西是如何工作的。

于 2012-05-02T04:11:35.237 回答
4

我还没有找到一种方法来访问绘制的(在帧缓冲区中)像素。我测量过的最快的方法是:

  1. 通过在创建图像时指定 kCGImageSourceShouldCache 来指示您希望图像被缓存。
  2. (可选)通过强制渲染来预缓存图像。
  3. 将图像绘制为 1x1 位图上下文。

这种方法的代价是缓存的位图,它的生命周期可能与它关联的 CGImage 一样长。代码最终看起来像这样:

  1. 创建带有 ShouldCache 标志的图像

    NSDictionary *options = @{ (id)kCGImageSourceShouldCache: @(YES) };
    CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
    CGImageRef cgimage = CGImageSourceCreateImageAtIndex(imageSource, 0, (__bridge CFDictionaryRef)options);
    UIImage *image = [UIImage imageWithCGImage:cgimage];
    CGImageRelease(cgimage);
    
  2. 预缓存图像

    UIGraphicsBeginImageContext(CGSizeMake(1, 1));
    [image drawAtPoint:CGPointZero];
    UIGraphicsEndImageContext();
    
  3. 将图像绘制到 1x1 位图上下文

    unsigned char pixelData[] = { 0, 0, 0, 0 };
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixelData, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGImageRef cgimage = image.CGImage;
    int imageWidth = CGImageGetWidth(cgimage);
    int imageHeight = CGImageGetHeight(cgimage);
    CGContextDrawImage(context, CGRectMake(-testPoint.x, testPoint.y - imageHeight, imageWidth, imageHeight), cgimage);
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    

pixelData 具有 testPoint 处像素的 R、G、B 和 A 值。

于 2012-09-11T17:24:08.070 回答
1

CGImage 上下文可能几乎是空的,并且在您尝试读取第一个像素或绘制它之前不包含实际的像素数据,因此尝试加快从图像中获取像素的速度可能无法让您到达任何地方。还没有什么可以得到的。

您是否尝试从 PNG 文件中读取像素?您可以尝试直接跟踪文件并对其进行映射并自己解码PNG格式。从存储中提取数据仍需要一段时间。

于 2012-05-02T00:08:26.320 回答
-2
- (BOOL)isWallPixel: (UIImage *)image: (int) x :(int) y {

CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
const UInt8* data = CFDataGetBytePtr(pixelData);

int pixelInfo = ((image.size.width  * y) + x ) * 4; // The image is png

//UInt8 red = data[pixelInfo];         // If you need this info, enable it
//UInt8 green = data[(pixelInfo + 1)]; // If you need this info, enable it
//UInt8 blue = data[pixelInfo + 2];    // If you need this info, enable it
UInt8 alpha = data[pixelInfo + 3];     // I need only this info for my maze game
CFRelease(pixelData);

//UIColor* color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f]; // The pixel color info

if (alpha) return YES;
else return NO;
}
于 2012-07-14T06:10:49.713 回答