CIArea直方图输入比例因子 [英] CIAreaHistogram inputScale factor

查看:114
本文介绍了CIArea直方图输入比例因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用CIAreaHistogram Core Image过滤器的应用程序.我将inputCount值(存储桶数)设置为10,将inputScale值设置为1.

I'm building an application that uses the CIAreaHistogram Core Image filter. I use an inputCount value (number of buckets) of 10 for testing, and an inputScale value of 1.

我得到直方图本身的CIImage,然后运行自定义内核(请参阅文章末尾)以将alpha值设置为1(因为否则直方图计算中的alpha值将被预乘),然后进行转换到NSBitmapImageRep.

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.

然后我浏览图像表示的缓冲区并打印RGB值(跳过alpha值).但是,当我这样做时,R,G和B值在10个值中的总和不一定等于255.

I then scan through the image rep's buffer and print the RGB values (skipping the alpha values). However, when I do this, the sum of the R, G, and B values across the 10 do not necessarily add up to 255.

例如,对于全黑图像,我应用直方图,然后应用自定义内核,并获得以下输出:

For example, with a fully black image, I apply the histogram, then the custom kernel, and get the following output:

RGB: 255 255 255
RGB: 0 0 0
RGB: 0 0 0
RGB: 0 0 0
RGB: 0 0 0
RGB: 0 0 0
RGB: 0 0 0
RGB: 0 0 0
RGB: 0 0 0
RGB: 0 0 0

这是我所期望的,因为所有像素均为黑色,所以所有内容都在第一个存储桶中.但是,如果我对彩色图像运行相同的算法,则会得到以下信息:

This is as I expect, since all pixels are black, so everything is in the first bucket. However, if I run the same algorithm with a color image, I get the following:

RGB: 98 76 81
RGB: 164 97 87
RGB: 136 161 69
RGB: 100 156 135
RGB: 80 85 185
RGB: 43 34 45
RGB: 31 19 8
RGB: 19 7 3
RGB: 12 5 2
RGB: 16 11 11

将R,G和B的值相加-它们的总和不等于255.这会引起问题,因为我需要比较两个直方图,并且我的算法期望总和在0到255之间.我显然可以缩放这些值,但是出于性能原因,我想避免执行该额外步骤.

Add up the values for R, G, and B - they don't add up to 255. This causes problems because I need to compare two of these histograms, and my algorithm expects the sums to be between 0 and 255. I could obviously scale these values, but I want to avoid that extra step for performance reasons.

我注意到了一些有趣的事情,这些事情可能会提供一些有关为什么发生这种情况的线索.在我的自定义内核中,我只是将alpha值设置为1.我尝试了第二个内核(请参阅文章末尾),将所有像素设置为红色.显然,绿色和蓝色值为零.但是,当从位图表示中检查值时,会得到以下结果:

I noticed something else interesting that might give some clue as to why this is happening. In my custom kernel, I simply set the alpha value to 1. I tried a second kernel (see end of post) that sets all pixels to red. Clearly, green and blue values are zero. However, I get this result when checking the values from the bitmap rep:

RGB: 255 43 25

但是我只是将G和B设置为零!这似乎是问题的一部分,表明存在颜色管理.但是,由于我在内核中明确设置了值,因此只有一小段代码可以发生-从过滤器的CIImage到NSBitmapImageRep的转换:

But I just set G and B to zero! This seems to be part of the problem, which indicates color management. But since I explicitly set the values in the kernel, there's only one block of code where this can be happening - the conversion to an NSBitmapImageRep from the CIImage from the filter:

NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCIImage:kernelOutput];
unsigned char *buf = [bitmapRep bitmapData];

一旦我将像素设置为RGB 255 0 0,然后执行这些行,然后读取缓冲区,则RGB值全部为255 4325.我进一步尝试设置原始CGImageRef的色彩空间,整个工作流程都在该色彩空间上进行是基于kCGColorSpaceGenericRGB的,认为颜色配置文件可能正在进行,但无济于事.

Once I set the pixels to RGB 255 0 0, then execute those lines, then read the buffer, the RGB values are all 255 43 25. I have further tried setting the color space of the original CGImageRef on which the entire workflow is based to kCGColorSpaceGenericRGB, thinking the color profile may be carrying through, but to no avail.

谁能告诉我为什么CIFilter内核会表现出这种方式,以及如何解决呢?

Can anyone tell me why a CIFilter kernel would behave this way, and how I could solve it?

如前所述,这是我使用的CIFilter内核函数的副本.首先,将alpha设置为1的那个:

As mentioned before, here are copies of the CIFilter kernel functions I use. First, the one that sets alpha to 1:

kernel vec4 adjustHistogram(sampler src)
{
    vec4 pix = sample(src, destCoord());
    pix.a = 1.0;
    return pix;
}

然后,将所有像素设置为RGB 255 0 0,但一旦转换为NSBitmapImageRep,最终以255 43 25结束的像素:

And next, the one that sets all pixels to RGB 255 0 0 but that ends up 255 43 25 once it converts to NSBitmapImageRep:

kernel vec4 adjustHistogram(sampler src)
{
    vec4 pix = sample(src, destCoord());
    pix.r = 1.0; pix.g = 0.0; pix.b = 0.0;
    pix.a = 1.0;
    return pix;
}

预先感谢您的帮助.

推荐答案

在使用自定义Core Image过滤器时(或者在创建新的CIImage对象或替换时),只需要一行代码即可生成并显示直方图现有的):

You only need one line of code to generate and display a histogram when using a custom Core Image filter (or whenever you are creating a new CIImage object or are replacing an existing one):

return [CIFilter filterWithName:@"CIHistogramDisplayFilter" keysAndValues:kCIInputImageKey, self.inputImage, @"inputHeight", @100.0, @"inputHighLimit", @1.0, @"inputLowLimit", @0.0, nil].outputImage;

这篇关于CIArea直方图输入比例因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆