在使用GPUImage框架的过滤器链之间切换 [英] Switching between filter chains using GPUImage Framework

查看:210
本文介绍了在使用GPUImage框架的过滤器链之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的代码在 case 1 case 2 之间切换两个过滤器链。当我最初选择任何一种情况下,输出显示正确。但是,当我切换到另一个滤波器链时,输出在当前和之前的滤波器链之间闪烁。什么是推荐的方式来切换滤波器链?
$ b $

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


解决方案

根据您的情况您可能还想考虑GPUImageFilterPipeline类。



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



如果这些过滤器的重复设置/拆卸对于您来说是有问题的,但是将它们留在您的课程的内存中并不是,那么您可能会欣赏一个管道。 b
$ b

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

   - ( void)configureSomeArraysOfFilters {
_setNumberOne = [[NSMutableArray alloc] init]; //确保这些数组至少是作用域的范围,如果不是实际的@properties
GPUImageKuwaharaFilter * kuwahara = [[GPUImageKuwaharaFilter alloc] init];
[kuwahara setRadius:5];
GPUImageGrayscaleFilter * grey = [[GPUImageGrayscaleFilter alloc] init];
[_setNumberOne addObject:kuwahara];
[_setNumberOne addObject:grey];


_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];

$ b - (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];


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

情况2:
[_samplePipeline replaceAllFilters:_setNumberTwo];

//添加更多的情况下,你已经定义数组
$ b的过滤器默认:
break;






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



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


I would like to switch between two filter chains as shown in case 1 and case 2 with the code below. When I initially select either cases, the output appears correct. However, when I switch to another the filter chain, the output flickers between current and prior filter chain. What is the recommended way to switch filter chains?

-(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];
}  

解决方案

Depending on the circumstances of your app, you may also want to consider the GPUImageFilterPipeline class.

It will take care of adding and removing all the intervening targets that Brad is referencing.

If the repeated setup/teardown of these filters is problematic for you, but keeping them around in memory for the life of your class is not, then you may appreciate a pipeline.

Based roughly off what you provided, it might look something like this:

- (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;
    }
}

My favorite use-case for pipelines, however, is when I'm dynamically creating sets of filters at runtime, and then switching them into action. It allows me to simply store the filters in order, and then just pass them to the pipeline, without having to specify all the targeting between each filter, every time.

It's not right for every situation, but GPUImageFilterPipeline can be very useful in some circumstances.

这篇关于在使用GPUImage框架的过滤器链之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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