如何获得有意义的CIAreaHistogram输出?

时间:2014-12-01 21:05:22

标签: objective-c cocoa core-image cifilter

我想计算CGImage的直方图。 我使用的是CIAreaHistogram内置CoreImage过滤器。

Justin Mrkva完成了something along similar lines。他说:

  

我得到直方图本身的CIImage,然后我通过自定义内核运行(参见结尾)将alpha值设置为1(因为否则直方图计算的alpha值被预乘)然后将其转换为NSBitmapImageRep。

我的问题是:是否可以在不创建自定义内核的情况下获取直方图数据?如果是这样,怎么样?

以下代码只是尝试渲染直方图而不会修改alpha值:

- (void)printHistogram:(CGImageRef)img {

    NSNumber* buckets = @10;

    CIImage* img_ = [CIImage imageWithCGImage:img];

    CIFilter* histF = [CIFilter filterWithName:@"CIAreaHistogram"];
    [histF setValue:img_ forKey:@"inputImage"];
    [histF setValue:[CIVector vectorWithX:0.0
                                        Y:0.0
                                        Z:CGImageGetWidth(img)
                                        W:CGImageGetHeight(img)]
             forKey:@"inputExtent"];
    [histF setValue:buckets forKey:@"inputCount"];
    [histF setValue:@1.0 forKey:@"inputScale"];

    CIImage* histImg = [histF valueForKey:@"outputImage"];

    int rowBytes = [buckets intValue] * 4; // ARGB has 4 components
    uint8_t byteBuffer[rowBytes]; // Buffer to render into
    CGColorSpaceRef cspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);

    CIContext* ctx = [[CIContext alloc] init];
    [ctx render:histImg
       toBitmap:byteBuffer
       rowBytes:rowBytes
         bounds:[histImg extent]
         format:kCIFormatARGB8
     colorSpace:cspace];

    CGColorSpaceRelease(cspace);

    for (int i=0; i<[buckets intValue]; i++) {
        const uint8_t* pixel = &byteBuffer[i*4];
        printf("%d:%u-%u-%u-%u\n",i,pixel[0],pixel[1],pixel[2],pixel[3]);
    }

}   

给出输出(当在彩色照片上运行时):

0:0-0-0-0
1:0-0-0-0
2:0-0-0-0
3:0-0-0-0
4:0-0-0-0
5:0-0-0-0
6:0-0-0-0
7:0-0-0-0
8:0-0-0-0
9:255-33-6-7

我尝试使用CIColorMatrix在呈现前将alpha值设置为1.0:

CIFilter* biasF = [CIFilter filterWithName:@"CIColorMatrix"];
[biasF setDefaults];
[biasF setValue:histImg forKey:@"inputImage"];
[biasF setValue:[CIVector vectorWithX:0.0 Y:0.0 Z:0.0 W:1.0] forKey:@"inputBiasVector"];

即使输出格式是ARGB,根据我对Core Image Filter Reference的理解,alpha分量是向量中的最后一个值(因此W:1.0)。

但是这产生了以下输出:

0:255-255-255-255
1:255-255-255-255
2:255-255-255-255
3:255-255-255-255
4:255-255-255-255
5:255-255-255-255
6:255-255-255-255
7:255-255-255-255
8:255-255-0-255
9:255-66-11-15

非常感谢所有帮助和建议!


编辑:我知道this question似乎相似。但是,接受的答案规定:

  

缺点是:您需要将值读取为浮点数,而不是整数,这意味着您必须将CGBitmapContext连接到blit。或者,如果您将所有内容保留在CI域中,则需要另一个过滤器来读取数据并使用它打印出来。

然而,看Justin Mrkva's question让我觉得应该可以获得整数值...如果我的想法出错,请告诉我。

再次感谢!


编辑2:所有人的拳头,感谢David和jstn的评论。对不起,我花了很长时间才回到这里。我在一个项目上昼夜不停地工作(实际上是那个导致我遇到这个问题的项目,但我最终使用了一种完全不同的方法,不再使用CIAreaHistogram)。现在我终于有了一些时间,我想回到这一点。即使我本身并不需要,我仍然想了解这件事是如何运作的!

根据David Hayward的建议,我做了以下修改。

- (void)printHistogram:(CGImageRef)img {

    NSNumber* buckets = @10;

    CIImage* img_ = [CIImage imageWithCGImage:img];

    CIFilter* histF = [CIFilter filterWithName:@"CIAreaHistogram"];
    [histF setValue:img_ forKey:@"inputImage"];
    [histF setValue:[CIVector vectorWithX:0.0
                                        Y:0.0
                                        Z:CGImageGetWidth(img)
                                        W:CGImageGetHeight(img)]
             forKey:@"inputExtent"];
    [histF setValue:buckets forKey:@"inputCount"];
    [histF setValue:@1.0 forKey:@"inputScale"];

    CIImage* histImg = [histF valueForKey:@"outputImage"];

    NSUInteger arraySize = [buckets intValue] * 4; // ARGB has 4 components

    // CHANGE 1: Since I will be rendering in float values, I set up the
    //           buffer using CGFloat
    CGFloat byteBuffer[arraySize]; // Buffer to render into

    // CHANGE 2: I wasn't supposed to call [[CIContext alloc] init]
    //           this is a more proper way of getting the context
    CIContext* ctx = [[NSGraphicsContext currentContext] CIContext];

    // CHANGE 3: I use colorSpace:NULL to use the output cspace of the ctx
    // CHANGE 4: Format is now kCIFormatRGBAf
    [ctx render:histImg
       toBitmap:byteBuffer
       rowBytes:arraySize
         bounds:[histImg extent]
         format:kCIFormatRGBAf
     colorSpace:NULL]; // uses the output cspace of the contetxt

    // CHANGE 5: I print the float values
    for (int i=0; i<[buckets intValue]; i++) {
        const CGFloat* pixel = &byteBuffer[i*4];
        printf("%d: %0.2f , %0.2f , %0.2f , %0.2f\n", i,pixel[0],pixel[1],pixel[2],pixel[3]);
    }
}   

这给出了以下输出:

0: 0.00 , 0.00 , 0.00 , 0.00
1: 0.00 , 0.00 , 0.00 , 0.00
2: 0.00 , 0.00 , 0.00 , 0.00
3: 0.00 , 0.00 , 0.00 , 0.00
4: 0.00 , 0.00 , 0.00 , 0.00
5: 0.00 , 0.00 , 0.00 , 0.00
6: 0.00 , 0.00 , 1.00 , 0.00
7: 0.00 , 0.00 , 0.00 , 0.00
8: 0.00 , 0.00 , 0.00 , 0.00
9: 3.00 , 0.00 , 0.00 , 0.00

使用各种格式以及如何解析信息会产生截然不同且荒谬的输出。

我很确定问题在于没有正确理解位图数据的表示方式。

还有其他建议吗?

6 个答案:

答案 0 :(得分:0)

三点建议:

  • 使用inputScale获取直方图计数。如果inputScale为1,则生成的直方图bin值将为1.0(如果整个区域具有该bin值,则为255(如果呈现为ARGB8))
  • 传递CI的工作色彩空间来渲染:toBitmap:。在Mavericks上,工作空间是kCGColorSpaceGenericRGBLinear。在约塞米蒂,它是线性的sRGB。
  • 在操作系统X上
  • ,您可以使用kCIFormatRGBAf来获取浮动数据
  • 在iOS上,您可以使用kCIFormatRGBAh获取半浮点数据

答案 1 :(得分:0)

虽然在iOS上,但我自己也在努力解决这个问题。我想从实时视频中获取直方图数据以用于我自己的可视化,但是遇到了same problem as Justin Mrkva

即使使用CIHistogramDisplayFilter输出对我来说也不太合适(太平了?),加上CIAreaHistogram就会消耗25-30%的CPU。这是在绘制到EAGLContext之前,甚至还没有从GPU中恢复数据......

所以我完全取消了Core Image并转向了vImage,这非常有效。它仍然是CPU密集型的,但令我惊讶的是不如CIAreaHistogram,输出与我在Photoshop中看到的相匹配。如果您尝试完全停留在GPU上,这并不理想,但根据您的应用程序,它可能是更好的选择(当然更精确)。

它并没有完全回答你的问题,但是这里是如何使用vImage获取CGImage的直方图。除了使用CVPixelBuffer并锁定基地址之外,我基本上做了同样的事情。渠道订单不同,但无关紧要:

#import <Accelerate/Accelerate.h>

CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image));

vImage_Buffer vImageBuffer;
vImageBuffer.data = (UInt8 *)CFDataGetBytePtr(imageData);
vImageBuffer.width = CGImageGetWidth(image);
vImageBuffer.height = CGImageGetHeight(image);
vImageBuffer.rowBytes = CGImageGetBytesPerRow(image);

vImagePixelCount red[256];
vImagePixelCount green[256];
vImagePixelCount blue[256];
vImagePixelCount alpha[256];
vImagePixelCount *histogram[4] = { red, green, blue, alpha };

vImage_Error error = vImageHistogramCalculation_ARGB8888(&vImageBuffer, histogram, 0);

if (error != kvImageNoError)
    NSLog(@"vImage error: %ld", error);

CFRelease(imageData);

// histogram now contains unsigned long counts for each channel

我仍然想知道如何在iOS上正确使用CIAreaHistogram

答案 2 :(得分:0)

这里是如何做你原本想做的事情;事实证明,改变上下文中的格式可以为您提供所有不同的结果。

顺便说一下,这个示例代码使用的是EAGLContext,据说速度更快,但根据我的经验,到目前为止,它并非如此。不过,这里是:

    CIFilter* histogram = [CIFilter filterWithName:@"CIAreaHistogram"];
[histogram setValue:inputImage forKey:@"inputImage"];
[histogram setValue:[CIVector vectorWithX:0.0 Y:0.0 Z:self.inputImage.extent.size.width W:self.inputImage.extent.size.height] forKey:@"inputExtent"];
[histogram setValue:@256 forKey:@"inputCount"];
[histogram setValue:@1.0 forKey:@"inputScale"];
/*id histogramData = [histogram valueForKey:@"outputData"];
if (histogramData)
    NSLog(@"outputData: %@", histogramData);*/

@autoreleasepool {
    CIImage* histogramImage = [histogram valueForKey:@"outputImage"];
    int rowBytes = 256 * 4; // ARGB has 4 components
    uint8_t byteBuffer[rowBytes]; // Buffer to render into

    EAGLContext *myEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    NSDictionary *options = @{ kCIContextWorkingColorSpace : [NSNull null] };
    CIContext *ctx = [CIContext contextWithEAGLContext:myEAGLContext options:options];
    //CIContext* ctx = [[CIContext alloc] init];
    [ctx render:histogramImage toBitmap:byteBuffer
       rowBytes:rowBytes
         bounds:[histogramImage extent]
         format:kCIFormatRGBAf
     colorSpace:nil];

    for (int i = 0; i < 256; i++)
    {
        const uint8_t* pixel = &byteBuffer[i*4];
        printf("%u, %u, %u\n", pixel[0], pixel[1], pixel[2]);
    }
}

答案 3 :(得分:0)

顺便说一句,这是从CIAreaAverage Core Image过滤器获取值的工作(和准确)代码:

- (CIImage *)outputImage
    CGRect inputExtent = [self.inputImage extent];
    CIVector *extent = [CIVector vectorWithX:inputExtent.origin.x
                                           Y:inputExtent.origin.y
                                           Z:inputExtent.size.width
                                           W:inputExtent.size.height];
    CIImage* inputAverage = [CIFilter filterWithName:@"CIAreaAverage" keysAndValues:@"inputImage", self.inputImage, @"inputExtent", extent, nil].outputImage;

    //CIImage* inputAverage = [self.inputImage imageByApplyingFilter:@"CIAreaMinimum" withInputParameters:@{@"inputImage" : inputImage, @"inputExtent" : extent}];
    EAGLContext *myEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    NSDictionary *options = @{ kCIContextWorkingColorSpace : [NSNull null] };
    CIContext *myContext = [CIContext contextWithEAGLContext:myEAGLContext options:options];

    size_t rowBytes = 32 ; // ARGB has 4 components
    uint8_t byteBuffer[rowBytes]; // Buffer to render into

    [myContext render:inputAverage toBitmap:byteBuffer rowBytes:rowBytes bounds:[inputAverage extent] format:kCIFormatRGBA8 colorSpace:nil];

    const uint8_t* pixel = &byteBuffer[0];
    float red   = pixel[0] / 255.0;
    float green = pixel[1] / 255.0;
    float blue  = pixel[2] / 255.0;
    NSLog(@"%f, %f, %f\n", red, green, blue);


    return outputImage;

示例输出:

2015-05-23 15:58:10.854 CIFunHouse[2400:489896] BSXPCMessage received error for message: Connection interrupted
2015-05-23 15:58:15.470 CIFunHouse[2400:489913] 0.000000, 0.000000, 0.305882
2015-05-23 15:58:15.582 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:15.622 CIFunHouse[2400:489913] 0.564706, 0.917647, 0.831373
2015-05-23 15:58:15.665 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:15.706 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:15.748 CIFunHouse[2400:489913] 0.564706, 0.917647, 0.831373
2015-05-23 15:58:15.792 CIFunHouse[2400:489913] 0.564706, 0.917647, 0.831373
2015-05-23 15:58:15.834 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:15.874 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:15.915 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:15.956 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:15.998 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:16.040 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:16.079 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:16.121 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:16.163 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:16.205 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:16.247 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:16.288 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:16.330 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:16.372 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:16.413 CIFunHouse[2400:489895] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:16.455 CIFunHouse[2400:489895] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:16.496 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:16.543 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:16.579 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:16.623 CIFunHouse[2400:489913] 0.564706, 0.917647, 0.831373
2015-05-23 15:58:16.667 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:16.705 CIFunHouse[2400:489913] 0.564706, 0.917647, 0.831373
2015-05-23 15:58:16.746 CIFunHouse[2400:489913] 0.564706, 0.917647, 0.831373
2015-05-23 15:58:16.788 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:16.829 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:16.873 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:16.912 CIFunHouse[2400:489970] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:16.955 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:16.996 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:17.038 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:17.080 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:17.122 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:17.163 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.205 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.246 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.288 CIFunHouse[2400:489970] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.330 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:17.371 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:17.415 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.456 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:17.496 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.539 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.580 CIFunHouse[2400:489895] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:17.622 CIFunHouse[2400:489895] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.664 CIFunHouse[2400:489895] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.705 CIFunHouse[2400:489895] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.746 CIFunHouse[2400:489895] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:17.788 CIFunHouse[2400:489895] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:17.830 CIFunHouse[2400:489895] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:17.873 CIFunHouse[2400:489895] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.914 CIFunHouse[2400:489895] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:17.956 CIFunHouse[2400:489895] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:17.997 CIFunHouse[2400:489913] 0.501961, 0.941176, 0.831373
2015-05-23 15:58:18.038 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.080 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.122 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.163 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:18.206 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:18.248 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.289 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.330 CIFunHouse[2400:489895] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:18.372 CIFunHouse[2400:489895] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.413 CIFunHouse[2400:489895] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:18.459 CIFunHouse[2400:489895] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.497 CIFunHouse[2400:489895] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.538 CIFunHouse[2400:489895] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:18.580 CIFunHouse[2400:489895] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:18.622 CIFunHouse[2400:489895] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.663 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:18.706 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:18.747 CIFunHouse[2400:489895] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:18.789 CIFunHouse[2400:489895] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.830 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:18.868 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:18.907 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.949 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:18.990 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:19.044 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:19.091 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:19.134 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:19.173 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:19.213 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:19.254 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:19.298 CIFunHouse[2400:489913] 0.564706, 0.917647, 0.831373
2015-05-23 15:58:19.337 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:19.377 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:19.415 CIFunHouse[2400:489913] 0.564706, 0.917647, 0.831373
2015-05-23 15:58:19.455 CIFunHouse[2400:489913] 0.564706, 0.917647, 0.831373
2015-05-23 15:58:19.498 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:19.540 CIFunHouse[2400:489913] 0.564706, 0.917647, 0.831373
2015-05-23 15:58:19.588 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:19.630 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:19.674 CIFunHouse[2400:489913] 0.501961, 0.380392, 0.898039
2015-05-23 15:58:19.719 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:19.763 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:19.806 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:19.851 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:19.902 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:19.960 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:20.016 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:20.073 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:20.139 CIFunHouse[2400:489913] 0.000000, 0.882353, 0.898039
2015-05-23 15:58:20.198 CIFunHouse[2400:489913] 0.000000, 0.882353, 0.898039
2015-05-23 15:58:20.253 CIFunHouse[2400:489913] 0.000000, 0.882353, 0.898039
2015-05-23 15:58:20.316 CIFunHouse[2400:489913] 0.313726, 0.894118, 0.831373
2015-05-23 15:58:20.389 CIFunHouse[2400:489913] 0.313726, 0.894118, 0.831373
2015-05-23 15:58:20.472 CIFunHouse[2400:489913] 0.313726, 0.894118, 0.831373
2015-05-23 15:58:20.554 CIFunHouse[2400:489913] 0.313726, 0.894118, 0.831373
2015-05-23 15:58:20.631 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:20.714 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:20.784 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:20.839 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:20.887 CIFunHouse[2400:489913] 0.690196, 0.309804, 0.819608
2015-05-23 15:58:20.935 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196
2015-05-23 15:58:20.981 CIFunHouse[2400:489913] 0.752941, 0.858824, 0.890196

答案 4 :(得分:0)

我已经工作了一段时间,但从未让kCIFormatARGB8工作。另一张海报说kCIFormatARGBf在iOS上不起作用,所以我尝试了kCIFormatRGBAh

kCIFormatRGBAh很奇怪:它的RGBA数据打包为16位浮点数(“半浮点数”)。 C没有本机半浮点类型,您必须依赖特定于编译器的数据类型。幸运的是,gcc和clang似乎都支持类型为__fp16

// img is input
histogramFilter = [CIFilter filterWithName:@"CIAreaHistogram" keysAndValues:@"inputScale", @1, @"inputCount", @256, nil];
CIVector *extent = [[CIVector alloc] initWithCGRect:img.extent];
[histogramFilter setValue:extent forKey:@"inputExtent"];
[histogramFilter setValue:img forKey:kCIInputImageKey];
CIImage *histImg = histogramFilter.outputImage;

int rowBytes = 256 * 4; // ARGB has 4 components.
__fp16 byteBuffer[rowBytes]; // Buffer to render into
rowBytes *= 2; //__fp16 is 2 bytes

EAGLContext *myEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
NSDictionary *options = @{ kCIContextWorkingColorSpace : [NSNull null] };
CIContext *ctx = [CIContext contextWithEAGLContext:myEAGLContext options:options];
[ctx render:histImg toBitmap:byteBuffer
   rowBytes:rowBytes
     bounds:[histImg extent]
     format:kCIFormatRGBAh
 colorSpace:nil];

for (int i = 0; i < 256; i++)
{
    const __fp16* pixel = &byteBuffer[i*4];

    NSLog(@"%d: %f, %f, %f, %f\n", i, (float)pixel[0], (float)pixel[1], (float)pixel[2], (float)pixel[3]);
}

我不认为这会在模拟器上运行,因为模拟器不支持kCIFormatRGBAh。

答案 5 :(得分:0)

CIAreaHistogram调用outputData上有一个未记录的方法可能很有用。