如何将GPUImageAlphaBlendFilter添加到GPUImageFilterGroup

时间:2014-03-25 21:36:17

标签: ios gpuimage

我正在开发一个需要一组效果的项目。

我按照FilterShowcase中的示例成功使用了filterGroup,如下所示:

filter = [[GPUImageFilterGroup alloc] init];

GPUImageSepiaFilter *sepiaFilter = [[GPUImageSepiaFilter alloc] init];
[(GPUImageFilterGroup *)filter addFilter:sepiaFilter];

GPUImagePixellateFilter *pixellateFilter = [[GPUImagePixellateFilter alloc] init];
[(GPUImageFilterGroup *)filter addFilter:pixellateFilter];

[sepiaFilter addTarget:pixellateFilter];
[(GPUImageFilterGroup *)filter setInitialFilters:[NSArray arrayWithObject:sepiaFilter]];
[(GPUImageFilterGroup *)filter setTerminalFilter:pixellateFilter]; code here

但是现在我想向像GPUIMAGE_HARRISCORNERDETECTION这样的群组添加一个新的过滤器,这个过滤器也需要混合。

这是过滤器初始化:

filter = [[GPUImageHarrisCornerDetectionFilter alloc] init];
[(GPUImageHarrisCornerDetectionFilter *)filter setThreshold:0.20];     

然后需要混合如下:

GPUImageCrosshairGenerator *crosshairGenerator = [[GPUImageCrosshairGenerator alloc] init];
crosshairGenerator.crosshairWidth = 15.0;
[crosshairGenerator forceProcessingAtSize:CGSizeMake(480.0, 640.0)];

[(GPUImageHarrisCornerDetectionFilter *)filter setCornersDetectedBlock:^(GLfloat* cornerArray, NSUInteger cornersDetected, CMTime frameTime) {
    [crosshairGenerator renderCrosshairsFromArray:cornerArray count:cornersDetected frameTime:frameTime];
}];

GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
[blendFilter forceProcessingAtSize:CGSizeMake(480.0, 640.0)];
GPUImageGammaFilter *gammaFilter = [[GPUImageGammaFilter alloc] init];
[videoCamera addTarget:gammaFilter];
[gammaFilter addTarget:blendFilter];

[crosshairGenerator addTarget:blendFilter];

[blendFilter addTarget:filterView];

有没有办法添加GPUImageCrosshairGeneratorGPUImageAlphaBlendFilter和& GPUImageGammaFilter到过滤器组?

谢谢

更具体的细节如下:

=============================================

基于FilterShowcase示例的代码: 测试类GPUImageDrawTriangleTest只是在实时视频源上绘制随机三角形

self.title = @"DRAWING TRIANGLES TESTING";

triangleFilter = [[GPUImageDrawTriangleTest alloc] init];
[((GPUImageDrawTriangleTest *)particleFilter) setDrawColorRed:1.0 green:0.0 blue:1.0];

filter = [[GPUImageContrastFilter alloc] init];

__unsafe_unretained GPUImageDrawTriangleTest *weakGPUImageTestCust = (GPUImageDrawTriangleTest *)triangleFilter;

[filter  setFrameProcessingCompletionBlock:^(GPUImageOutput * filter, CMTime frameTime){
    [weakGPUImageTestCust update:frameTime];
}];

blendingFilters = TRUE;

blendFilter = nil;
blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
[blendFilter forceProcessingAtSize:CGSizeMake(640.0, 480.0)];

GPUImageGammaFilter *gammaFilter = [[GPUImageGammaFilter alloc] init];

[videoCamera addTarget:gammaFilter];
[gammaFilter addTarget:blendFilter];

blendFilter.mix = 1.0;

[triangleFilter  addTarget:blendFilter];
[filter  addTarget:blendFilter];

[blendFilter addTarget:filterView];

[filter  addTarget:filterView];

[videoCamera addTarget:filter];

基于FilterShowcase示例和我创建的GPUImageUnsharpMaskFilter组的模板GPUImageParticleGroupTest

#import "GPUImageParticleGroupTest.h"

#import "GPUImageFilter.h"
#import "GPUImageGammaFilter.h"
#import "GPUImageDrawTriangleTest.h"
#import "GPUImageContrastFilter.h"
#import "GPUImageAlphaBlendFilter.h"

#import "GPUImageDrawTriangleTest.h"

@implementation GPUImageParticleGroupTest
    - (id)init;
{
    if (!(self = [super init]))
    {
        return nil;
    }

    contrastFilter = [[GPUImageContrastFilter alloc] init];
    [self addFilter:contrastFilter];

    gammaFilter = [[GPUImageGammaFilter alloc] init];
    [self addFilter:gammaFilter];

    triangleFilter = [[GPUImageDrawTriangleTest alloc] init];
    [((GPUImageDrawTriangleTest *)triangleFilter) setDrawColorRed:1.0 green:0.0 blue:1.0];

    //[self addFilter:triangleFilter];

    __unsafe_unretained GPUImageDrawTriangleTest *weakGPUImageTestCust = (GPUImageDrawTriangleTest *)triangleFilter;

    [ contrastFilter  setFrameProcessingCompletionBlock:^(GPUImageOutput * contrastfilter, CMTime frameTime){
        [weakGPUImageTestCust update:frameTime];
    }];

    theblendFilter = [[GPUImageAlphaBlendFilter alloc] init];
    theblendFilter.mix = 1.0;
    [self addFilter:theblendFilter];

    [gammaFilter addTarget:theblendFilter  atTextureLocation:1];

    [triangleFilter  addTarget:theblendFilter  atTextureLocation:1];

    [contrastFilter  addTarget:theblendFilter  atTextureLocation:1];


    self.initialFilters = [NSArray arrayWithObjects:contrastFilter,gammaFilter, nil];
    self.terminalFilter = theblendFilter;

    return self;
}
@end

意图是当这个组类实例化如下:

filter=  [[GPUImageParticleGroupTest alloc] init];
[filter addTarget:filterView];
[videoCamera addTarget:filter];

我会得到相同的结果,并在实时视频上绘制相同的随机三角形。该应用程序不会崩溃,但我不再获得任何实时视频或三角形。

我哪里出错了?

1 个答案:

答案 0 :(得分:1)

当处理需要将输入图像与组内生成的内容混合在一起的GPUImageFilterGroup时,您只需要处理另一件事,并且确保将目标添加到按正确的顺序混合。

以GPUImageUnsharpMaskFilter为例。它将输入帧接收到该组,将它们传递给模糊滤镜,然后将该模糊滤镜的输出与输入图像混合。要进行此设置,请使用以下代码:

// First pass: apply a variable Gaussian blur
blurFilter = [[GPUImageGaussianBlurFilter alloc] init];
[self addFilter:blurFilter];

// Second pass: combine the blurred image with the original sharp one
unsharpMaskFilter = [[GPUImageTwoInputFilter alloc] initWithFragmentShaderFromString:kGPUImageUnsharpMaskFragmentShaderString];
[self addFilter:unsharpMaskFilter];

// Texture location 0 needs to be the sharp image for both the blur and the second stage processing
[blurFilter addTarget:unsharpMaskFilter atTextureLocation:1];

self.initialFilters = [NSArray arrayWithObjects:blurFilter, unsharpMaskFilter, nil];
self.terminalFilter = unsharpMaskFilter;

此处使用的一种新方法是-addTarget:atTextureLocation:,可确保将输入图像添加为模糊滤镜和后期混合的第一个输入。另请注意,此组有两个initialFilters,以确保输入图像转到这两个过滤器。

您需要执行与上述代码类似的操作,将输入输入Harris角点检测器以及您的混合物。基于您已经从FilterShowcase示例和GPUImageUnsharpMaskFilter组的模板获得的代码,这应该是相当简单的。