我正在使用CIAreaHistogram内置的CoreImage过滤器.
Justin Mrkva做了something along similar lines.他说:
I get the CIImage for the histogram itself,which I then run through a custom kernel (see end of post) to set Alpha values to 1 (since otherwise the Alpha value from the histogram calculations is premultiplIEd) and then convert it to an 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-01:0-0-0-02:0-0-0-03:0-0-0-04:0-0-0-05:0-0-0-06:0-0-0-07:0-0-0-08:0-0-0-09: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,根据我从核心图像过滤器参考中所理解的,Alpha分量是向量中的最后一个值(因此W:1.0).
但是这产生了以下输出:
0:255-255-255-2551:255-255-255-2552:255-255-255-2553:255-255-255-2554:255-255-255-2555:255-255-255-2556:255-255-255-2557:255-255-255-2558:255-255-0-2559:255-66-11-15
所有的帮助和建议将不胜感激!
编辑:我知道this question似乎相似.但是,接受的答案规定:
The short of it is: you need to read the values as floats,not ints,which means you’ll have to hook up a CGBitmapContext to blit to. Or if you keep everything in CI land,you’ll need another filter to read the data and print something out with it.
但是,看看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\n",pixel[3]); }}
这给出了以下输出:
0: 0.00,0.00,0.001: 0.00,0.002: 0.00,0.003: 0.00,0.004: 0.00,0.005: 0.00,0.006: 0.00,1.00,0.007: 0.00,0.008: 0.00,0.009: 3.00,0.00
使用各种格式以及如何解析信息会产生截然不同且荒谬的输出.
我很确定问题在于没有正确理解位图数据的表示方式.
还有什么建议?
解决方法 三点建议:>使用inputScale获取直方图计数.如果inputScale为1,那么如果整个区域具有该bin值,则得到的直方图bin值将为1.0(如果呈现给ARGB8则为255)>传递CI的工作色彩空间来渲染:toBitmap:.在Mavericks上,工作空间是kCGcolorSpaceGenericRGBlinear.在优胜美地,它是线性sRGB.>在OS X上,您可以使用kCIFormatRGBAf来获取浮点数据>在iOS上,您可以使用kCIFormatRGBAh获取半浮点数据
总结以上是内存溢出为你收集整理的objective-c – 如何获得有意义的CIAreaHistogram输出?全部内容,希望文章能够帮你解决objective-c – 如何获得有意义的CIAreaHistogram输出?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)