filter - 使用 GPUImage 框架在过滤器链之间切换

标签 filter gpuimage chain

我想使用下面的代码在案例 1案例 2 中所示的两个过滤器链之间切换。当我最初选择这两种情况时,输出看起来都是正确的。然而,当我切换到另一个过滤器链时,输出在当前和先前的过滤器链之间闪烁。切换过滤器链的推荐方法是什么?

-(void) updateFilter:(NSInteger) style {
switch (style) {
    case 1:
        [kuwahara setRadius:5];
        [videoCamera addTarget:kuwahara];
        [kuwahara addTarget:grayscale];
        [grayscale addTarget:filteredVideoView];
        break;
    case 2:    
        [videoCamera addTarget:grayscale];
        [blur setBlurSize:3];
        [grayscale addTarget:blur];
        [blur addTarget:colorinvert];
        [colorinvert addTarget:filteredVideoView];
        break;
    default:
        [videoCamera addTarget:filteredVideoView];
        break;
}
[videoCamera startCameraCapture];
}  

最佳答案

根据您应用的情况,您可能还需要考虑 GPUImageFilterPipeline 类。

它将负责添加和删除 Brad 引用的所有干预目标。

如果这些过滤器的重复设置/拆卸对您来说是个问题,但在您的类(class)中将它们保存在内存中不是问题,那么您可能会喜欢管道。

根据您提供的大致内容,它可能看起来像这样:

- (void)configureSomeArraysOfFilters {
    _setNumberOne = [[NSMutableArray alloc] init]; //make sure these arrays are at least scoped to the class, if not actual @properties
    GPUImageKuwaharaFilter* kuwahara = [[GPUImageKuwaharaFilter alloc] init];
    [kuwahara setRadius:5];
    GPUImageGrayscaleFilter* gray = [[GPUImageGrayscaleFilter alloc] init];
    [_setNumberOne addObject:kuwahara];
    [_setNumberOne addObject:gray];


    _setNumberTwo = [[NSMutableArray alloc] init];
    GPUImageGrayscaleFilter* otherGray = [[GPUImageGrayscaleFilter alloc] init];
    GPUImageGaussianBlurFilter* blur = [[GPUImageGaussianBlurFilter alloc] init];
    [blur setBlurSize:3];
    GPUImageColorInvertFilter* invert = [[GPUImageColorInvertFilter alloc] init];
    [_setNumberTwo addObject:otherGray];
    [_setNumberTwo addObject:blur];
    [_setNumberTwo addObject:invert];
}

- (void)configureAnEmptyPipeline {
    if (_samplePipeline == nil) {
        GPUImageFilter* passthrough = [[GPUImageFilter alloc] init];
        NSArray* empty = [NSArray arrayWithObjects:passthrough, nil];
        _samplePipeline = [[GPUImageFilterPipeline alloc] initWithOrderedFilters:empty input:videoCamera output:_filteredVideoView];
        [videoCamera startCameraCapture];
    }
}

- (void)updateFilterPipeline:(NSInteger)style {
    switch (style) {
        case 1:
            [_samplePipeline replaceAllFilters:_setNumberOne];
            break;

        case 2:
            [_samplePipeline replaceAllFilters:_setNumberTwo];

        //add as many more cases as you have defined Arrays full of filters for

        default:
            break;
    }
}

然而,我最喜欢的管道用例是在运行时动态创建过滤器集,然后将它们切换为 Action 。它允许我简单地按顺序存储过滤器,然后将它们传递给管道,而不必每次都指定每个过滤器之间的所有目标。

它并不适用于所有情况,但 GPUImageFilterPipeline 在某些情况下可能非常有用。

关于filter - 使用 GPUImage 框架在过滤器链之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19254210/

相关文章:

javascript - 过滤多个表列

python - 基于局部变量的Django条件过滤器

php - WordPress 画廊问题

ios - 如何在 UIImage (GPUImage) 的不同滤镜之间正确切换

python - 使用另一个 DataFrame 过滤 Pandas DataFrame

ios - 在相机捕获和图像编辑时使用 GPUImage GaussianSelective Blur Filter

javascript - 如何从迭代器本地链接 JavaScript 方法?

javascript - NodeJS : Chain functions automatically in a promise?

Javascript( typescript )Chrome 扩展,函数回调如 promise ?

ios - GPU图像性能