CIColorMatrix偏向于alpha

时间:2013-11-17 02:49:30

标签: ios core-image ciimage

所以我试图从图像中删除alpha通道。我想我会试试CIColorMatrix。现在,这就是问题所在。 filter是先前(成功)应用的过滤器。以下代码的工作方式与您认为的相同:

ciImage = [filter valueForKey:kCIOutputImageKey];
filter = [CIFilter filterWithName:@"CIColorMatrix"];
[filter setValue:ciImage forKey:kCIInputImageKey];
[filter setValue:[CIVector vectorWithX:1 Y:0 Z:0 W:0] forKey:@"inputRVector"];
[filter setValue:[CIVector vectorWithX:0 Y:1 Z:0 W:0] forKey:@"inputGVector"];
[filter setValue:[CIVector vectorWithX:0 Y:0 Z:1 W:0] forKey:@"inputBVector"];
[filter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:0.75] forKey:@"inputAVector"];
[filter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:0.0] forKey:@"inputBiasVector"];
image = [UIImage imageWithCIImage:[filter valueForKey:kCIOutputImageKey]];

它将alpha乘以0.75。但是,如果我将alpha偏差改为0.01:

[filter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:0.01] forKey:@"inputBiasVector"];

生成的CIImage具有荒谬的extent,例如大约(-1.7e38, -1.7e38), (3.4e38 x 3.4e38)。我还没有尝试重新定位它以查看实际图像是否正确。

任何人都知道为什么会这样,以及如何解决它?

2 个答案:

答案 0 :(得分:0)

好的,我想我已经明白了。显然,某些过滤器无限扩展extent并不常见。解决方案是做

CGRect origExtent = ciImage.extent;

到原始ciImage,进行过滤(每次都将ciImage设置为结果),然后调用

ciImage = [ciImage imageByCroppingToRect:origExtent];
在获得最终UIImage或其他任何内容之前

答案 1 :(得分:0)

要移除Alpha通道,正确的滤镜是色卡,将alpha值范围设置为1..1,将其他颜色通道设置为0..1:

    CIFilter *clamp = [CIFilter filterWithName:@"CIColorClamp"];
    [clamp setValue:ciImage forKey:kCIInputImageKey];
    [clamp setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputMinComponents"];
    [clamp setValue:[CIVector vectorWithX:1 Y:1 Z:1 W:1] forKey:@"inputMaxComponents"];
    image = [clamp valueForKey:kCIOutputImageKey];